Click here to Skip to main content
15,887,853 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want my main window to lunch with my own chosen colour. I have done extensive google search on this but I am yet to resolve it.

I changed the background brush of my main window to GRAY by selecting it with GetStockObject during the window class registration process, but assigning the new brush to the background brush member of the window class structure did not work. Next, I got a dc and a grey brush during erase background and changed the colour. The colour changed only after initially displaying the default white background colour first.

Next, I changed the colour during WM_PAIMNT, it worked only after initially showing white background.

I painted a memory dc with the desired colour during WM_CREATE using typical double buffering :using BitBlt to transfer the content of the memory dc to the window dc on WM_PAINT. Again the colour changed only after initially showing white. How can I make the main window to display my desired background colour at the very first lunch?

What I have tried:

I have done extensive googling on the issue.
Posted
Comments
Rick York 21-Jan-24 15:55pm    
I use MFC and when I call its AfxRegisterWndClass function and provide a brush from GetStockObject it works correctly. Are you using MFC? In another program, this non-MFC one : https://www.codeproject.com/Articles/2151/Windows-Fortune-Application, I have it do nothing in the WM_ERASEBKGND handler and the background is drawn in the WM_PAINT handler.

I think you are just not doing things quite right because these tactics can work. I can only guess because there was no code posted.

I have the following code in my Window class creation:
C++
HBRUSH hBrush = (HBRUSH)CreateSolidBrush(RGB(255,0,0));
wndClassEx.hbrBackground = hBrush;

and the Window is created with a red background.

You need to show your actual code, and explain where it fails.
 
Share this answer
 
With the typical double buffering with BitBlt(), no background is usually drawn, as you usually do not want to see the flickering. Therefore WM_ERASEBKGND is terminated prematurely with return TRUE. Accordingly, no specific background color is required and everything is drawn under WM_PAINT.
At the beginning, the window is created with CreateWindow() and displayed with ShowWindow() and UpdateWindow().
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900