Click here to Skip to main content
15,886,724 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I created the window something like this. And I need to set the red color of the window, for example:

C++
WNDCLASSEX MyClassStruct;
const char MyClassName[] = "MyClass";
MyClassStruct.style = CS_HREDRAW | CS_VREDRAW;
MyClassStruct.cbSize = sizeof(WNDCLASSEX);
MyClassStruct.lpfnWndProc = MyWndProc;
MyClassStruct.cbClsExtra = 0;
MyClassStruct.cbWndExtra = 0;
MyClassStruct.hInstance = hInstance;
MyClassStruct.hIcon = NULL;
MyClassStruct.hCursor = LoadCursor(NULL, IDC_ARROW);
HBRUSH redbru = CreateSolidBrush(RGB(255,0,0));
MyClassStruct.hbrBackground=redbru;
MyClassStruct.lpszMenuName = NULL;
MyClassStruct.lpszClassName = MyClassName;

RegisterClassEx (&MyClassStruct);


CtrlsWnd = CreateWindowEx(WS_EX_TOPMOST,MyClassName,TEXT("title"),WS_VISIBLE,0,0,200,
100,NULL,NULL,hInstance,NULL);


But as a result the newly created window has standard color - COLOR_WINDOW.
Why does not red background set up?
Posted
Updated 17-Mar-21 5:24am
v2
Comments
nv3 14-Jun-13 9:56am    
You should test the return value of RegisterClassEx. If that call fails, your window could have been created with a standard window class.
Richard MacCutchan 14-Jun-13 13:29pm    
I would agree with nv3's comment. I just added the brush creation code to my test program, and the result was a red background. Of course, it could be that you are doing something else with you window that erase the background and/or repaints it white.

Thank you all for your help! I solved the problem. The problem was in the garbage, which was written in the window procedure. I cleaned and fixed it. And it works perfectly.
 
Share this answer
 
Comments
CPallini 14-Jun-13 16:44pm    
Well done.
You are assigning redbru to ButClassStruct.hbrBackground while you are passing MyClassStruct to the CreateWindowEx call.
 
Share this answer
 
Comments
Igor-84 14-Jun-13 9:10am    
CPallini, soory, I fix this. But it don't help - the window is not painted red anyway.
I know I am late to comment, however, I just ran across this problem and found a solution. I suspect there are many ways for this problem to occur; I found and fixed just one.

I created, a default GUI Window project using Code:: Blocks version 17.12 on Windows 7. I changed the background color, similar to what is presented in the OP's question. I built my project and voila, everything worked. I had my red window.

Then I decided to customize the program by adding handlers for the wm_paint and wm_erasebkgnd messages and this is where things got interesting.

I found that adding the wm_erasebkgnd handler caused the window screen to become white, no more red color. I tried three versions of wm_erasebkgnd and they all caused the program to break.

C++
case WM_ERASEBKGND:{
break;//this broke the program (white window)
return 0;//this broke the program (break was commented out)
return 1;//this broke the program (break and return 0 were commented out)
}


It turned out that the default message handler was the problem. The default, created by code::blocks 17.12 looked like this:

C++
LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch (message)                  /* handle the messages */
    {
        case WM_DESTROY:
            PostQuitMessage (0);       /* send a WM_QUIT to the message queue */
            break;
        default:                      /* for messages that we don't deal with */
            return DefWindowProc (hwnd, message, wParam, lParam);
    }

    return 0;
}


Notice that DefWindowProc is NOT called after a WM_DESTROY message is received, as the break statement takes the program to the first line of code after the switch statement.

I created the same circumstance for myself by handling the wm_erasebkgnd message. Regardless what I did (break, return 0 or return 1) I never called DefWindowProc.

According to Microsoft, it is the wm_erasebkgnd message that causes DefWindowProc to erase the screen with the color specified in the wndclassex structure (hbrbackground).

I fixed my problem by changing the message handler to look like this:

C++
LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch (message)                  /* handle the messages */
    {
       case WM_PAINT:{
        //ok to paint something cool between beginpaint and endpaint
       return 0;//don't call defwindowproc
       }
       case WM_ERASEBKGND:{
       break;//return 0 or return 1 will break the program and a white window will display
/*normally when using wm_paint I return 1 from wm_erasebkgnd 
because I don't want windows to erase the screen with the color 
defined in my wndclassex structure.  that means I have to erase 
myself using the hdc sent with wparam when the wm_erasebkgnd message 
is sent OR handle screen erasing as part of my wm_paint processing. 
Why? screen flicker is often caused by failing to prevent an extra
erase command (and that is what happens if you don't handle the 
wm_erasebkgnd message)
*/
       }
        case WM_DESTROY:
            PostQuitMessage (0);       /* send a WM_QUIT to the message queue */
            break;
        default:                      /* for messages that we don't deal with */
            break; //return DefWindowProc (hwnd, message, wParam, lParam);
    }

    return DefWindowProc (hwnd, message, wParam, lParam); //return 0;
}


I hope this post helps someone else.
 
Share this answer
 
Comments
Richard MacCutchan 17-Mar-21 12:10pm    
A one line change to the WNDCLASSEX structure is all that is needed.
KenLevin 17-Mar-21 16:14pm    
that is the point of my earlier post. If you make the one line change (change .hbrBackground) in wndclassex the window color is changed until you decide to add a handler for wm_erasebkgnd, then the one line change breaks IF the message handler (wndproc) isn't setup correctly. I pointed out that the default code created by code::blocks needed a simple adjustment to preserve the custom back color if a wm_erasebkgnd message handler is used.

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