How do you set the background color of an existing window?
If the window has an application defined class you can use SetClassLong to change the background brush for the class.
invoke CreateSolidBrush, 0FF0000h
mov , eax
invoke SetClassLong, , GCL_HBRBACKGROUND,
The brush cannot be destroyed for the life of the window or until it is replaced.
invoke CreateSolidBrush, 0FF0000h
mov , eax
invoke SetClassLong, , GCL_HBRBACKGROUND,
The brush cannot be destroyed for the life of the window or until it is replaced.
Hi Donkey,
Thanks as usual.
Is it necessary to destroy the brush before ending the program.
Thanks as usual.
Is it necessary to destroy the brush before ending the program.
It is good programming practice however Windows will destroy it for you when your process is terminated.
Hi Donkey
How do I know "If the window has an application defined class"?
How do I know "If the window has an application defined class"?
I think he means that you register your class with RegisterClass.
Hi msmisth,
Generally it just means that it was created with RegisterClassEx and CreateWindowEx. Also the Dialog as main type windows fall into this category. If you have defined a class for the window within your program it is an application defined class. In Windows NT you can also change the brushes for global classes as they are copied to your process and you are using copies, however I generally don't like to modify global classes.
Generally it just means that it was created with RegisterClassEx and CreateWindowEx. Also the Dialog as main type windows fall into this category. If you have defined a class for the window within your program it is an application defined class. In Windows NT you can also change the brushes for global classes as they are copied to your process and you are using copies, however I generally don't like to modify global classes.
Hi Donkey,
Thanks again, but still no colored window.
I tried your code and the olny error I got was due to the fact that there is no CreateSolidBrush function, just CreateSolidBrushA, and CreateSolidBrushW. So I tried CreateSolidBrushA.
No errors now, but no change in color.
Do I need WM_PAINT or something.
Thanks,
Mike
Thanks again, but still no colored window.
I tried your code and the olny error I got was due to the fact that there is no CreateSolidBrush function, just CreateSolidBrushA, and CreateSolidBrushW. So I tried CreateSolidBrushA.
No errors now, but no change in color.
Do I need WM_PAINT or something.
Thanks,
Mike
You generally should destroy various objects (file handles, GDI stuff, etc) even if windows "will" destroy it at application termination. On NT most of this is "whatever", but it's still good programming practice (and will make it easier if you later need to encapsulate your application and make it "multiple windows from a single instance").
Also, 9x is _very_ sensitive to GDI leaks. I can't remember how good it is at cleaning up GDI objects on application termination, but GDI leaks inside your app can lead to VERY nasty crashes, including BSODs (first everything looks win3.x style rather than 9x style, then you get BSOD)
Also, 9x is _very_ sensitive to GDI leaks. I can't remember how good it is at cleaning up GDI objects on application termination, but GDI leaks inside your app can lead to VERY nasty crashes, including BSODs (first everything looks win3.x style rather than 9x style, then you get BSOD)
Hi Mike,
It should work. Remember to invalidate and update the window after you change the brush...
CreateSolidBrush is found in GDI32.DLL, there is no CreateSolidBrushA (it doesn't accept strings so no need for ANSI/UNICODE), perhaps you meant SetClassLongA
It should work. Remember to invalidate and update the window after you change the brush...
invoke CreateSolidBrush,0080FFFFh
invoke SetClassLongA,[hwnd],GCL_HBRBACKGROUND,eax
invoke InvalidateRect,[hwnd],NULL,TRUE
invoke UpdateWindow,[hwnd]
CreateSolidBrush is found in GDI32.DLL, there is no CreateSolidBrushA (it doesn't accept strings so no need for ANSI/UNICODE), perhaps you meant SetClassLongA
Hi Donkey,
You're right, I meant SetClassLongA.
This still does nothing... does not change the window to blue.
You're right, I meant SetClassLongA.
invoke CreateSolidBrush, 0FF0000h
mov [hClassBrush], eax
invoke SetClassLong, [OBMain], GCL_HBRBACKGROUND, [hClassBrush]
invoke InvalidateRect,[OBMain],NULL,TRUE
invoke UpdateWindow,[OBMain]
This still does nothing... does not change the window to blue.
Hi Mike,
I am not sure what's happening with yours, here is a test project I threw together to demo it, I tested under 95 NT4 and 2K...
I am not sure what's happening with yours, here is a test project I threw together to demo it, I tested under 95 NT4 and 2K...
Hi Donkey,
It tried yours and it works.
The real difference I can see is that yours uses RegisterClassExA while mine uses RegisterClass.
The docs say that the only difference concerns small icons.
It tried yours and it works.
The real difference I can see is that yours uses RegisterClassExA while mine uses RegisterClass.
The docs say that the only difference concerns small icons.
Hi Mike,
That is correct, it only affects the small icon, the problem is that SetClassLongA requires the WNDCLASSEX structure only available through RegisterClassExA
That is correct, it only affects the small icon, the problem is that SetClassLongA requires the WNDCLASSEX structure only available through RegisterClassExA
The SetClassLong function replaces the specified 32-bit (long) value at the specified offset into the extra class memory or the WNDCLASSEX structure for the class to which the specified window belongs.
Hi Donkey,
It sounds pretty plain from what you are saying that my program is not working because I am not using RegisterClassExA.
If this is true (and it seems to be), then the remark in the docs about only affecting the small icon is simply false (imagine that!). It seems it affects my ability to color the window background.
If I need to use RegisterClassExA in order to have the window accept SetClassLongA messages then there is clearly an additional difference.
Do you you know if I switch all my code to generate RegisterClassExA instead of RegisterClass I will experience any bad surprises?
Thanks,
Mike
It sounds pretty plain from what you are saying that my program is not working because I am not using RegisterClassExA.
If this is true (and it seems to be), then the remark in the docs about only affecting the small icon is simply false (imagine that!). It seems it affects my ability to color the window background.
If I need to use RegisterClassExA in order to have the window accept SetClassLongA messages then there is clearly an additional difference.
Do you you know if I switch all my code to generate RegisterClassExA instead of RegisterClass I will experience any bad surprises?
Thanks,
Mike
Hi Mike,
I have never used RegisterClass, it is a deprecated function left over from Win16 and I generally use the ~Ex versions of functions. However I know of no other side effect from the change.
I have never used RegisterClass, it is a deprecated function left over from Win16 and I generally use the ~Ex versions of functions. However I know of no other side effect from the change.
Hi Mike,
I just tried with RegisterClassA and it seems to work fine...
I just tried with RegisterClassA and it seems to work fine...