zadam
zadam

Reputation: 2456

Change a Window background color, given a IntPtr handle

In c#, I have a handle to a window ( an IntPtr ), I want to change the background color of that window. How can I do this?

I can get the GDI graphics object for that handle like so:

Graphics graphics = Graphics.FromHwnd(theHandle);

So I should somehow be able to change the background color from this?

I also want to ensure the background color remains even after the window moves,resizes,repaints etc.

Upvotes: 2

Views: 4442

Answers (2)

Charlie
Charlie

Reputation: 45072

I don't think there's a way to do this directly with a native (C/C++) window (i.e. there is no native GDI analogue to Control.BackColor).

From looking in Reflector it appears that Control uses the BackColor property to respond to the various WM_CTLCOLOR* messages (e.g. WM_CTLCOLOREDIT). So, if you want to change the background color of a native control, you may need to subclass that window and respond to that same message. If the native window is not a control, you'll still need to subclass the window, but you'd have to handle WM_PAINT or WM_ERASEBKGND instead.

Try this thread on programmersheaven.com for a suggestion on how to subclass a native window from C#.

Upvotes: 2

Kepboy
Kepboy

Reputation: 3823

Create a control class with the Control.FromHandle method and then set the property.

Something like...

    Control someControl = Control.FromHandle(myHandle);
    someControl.BackColor = SystemColors.Black;

Upvotes: 0

Related Questions