How do I change background color?

This is a discussion on How do I change background color? within the Windows Programming forums, part of the Platform Specific Boards category; I've tried this: Code: HDC h = hwnd; // hwnd is my window SetBkColor(h,RGB(233,233,233)); I put this in "WM_CREATE:", but ...

  1. #1
    Registered User kinghajj's Avatar
    Join Date
    Jun 2003
    Posts
    218

    How do I change background color?

    I've tried this:

    Code:
    HDC h = hwnd; // hwnd is my window
    SetBkColor(h,RGB(233,233,233));
    I put this in "WM_CREATE:", but this doesn't work

    I've checked MSDN, but it doesn't give good details on how to use SetBkColor.

    Can anyone please help?

  2. #2
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    SetBkColor() will only take effect when you draw to the HDC. What you need to do is place the color specification in your window class registration.
    Code:
    hBrush=CreateBrushIndirect(...);
    
    wcx.hBrush=hBrush;
    Look up the CreateBrushIndirect() function for the parameters. You'll have to remember to delete the brush when you're done with it, which will likely be at the termination of the application.
    benforbes@optusnet.com.au
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  3. #3
    Registered User kinghajj's Avatar
    Join Date
    Jun 2003
    Posts
    218

    Huh?

    My window class doesn't have "hBrush" in it.

  4. #4
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    Oops hbrBackground. Sorry.
    benforbes@optusnet.com.au
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  5. #5
    Registered User kinghajj's Avatar
    Join Date
    Jun 2003
    Posts
    218
    My code:

    Code:
    	HBRUSH hBrush;
    	LOGBRUSH *logBrush;
    	logBrush->lbStyle = BS_SOLID;
    	logBrush->lbColor = RGB(233,233,233);
    	
    	// Define our mainw's class
        mainw.wc.cbSize        = sizeof(WNDCLASSEX);
        mainw.wc.style         = 0;
        mainw.wc.lpfnWndProc   = mainw_proc;
        mainw.wc.cbClsExtra    = 0;
        mainw.wc.cbWndExtra    = 0;
        mainw.wc.hInstance     = hInstance;
        mainw.wc.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
        mainw.wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
        mainw.wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
        mainw.wc.lpszMenuName  = NULL;
        mainw.wc.lpszClassName = g_szClassName;
        mainw.wc.hIconSm       = LoadIcon(NULL, IDI_APPLICATION);
    	
    	hBrush = CreateBrushIndirect(logBrush);
    	mainw.wc.hbrBackground = hBrush;
    It compile's fine, but I get the error message from Windows.

  6. #6
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    What error message do you get?

    Anyway, you're not doing things right.
    Code:
    HBRUSH hBrush;
    LOGBRUSH logBrush; //You have to declare the variable, not a pointer
    logBrush.lbStyle = BS_SOLID;
    logBrush.lbColor = RGB(233,233,233);
    logBrush.lbHatch= NULL; //You have to make sure this member is NULL if you're not using it
    
    hBrush=CreateBrushIndirect(&logBrush);
    
    //Now fill the WNDCLASSEX members
    There you go.
    benforbes@optusnet.com.au
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  7. #7
    Registered User kinghajj's Avatar
    Join Date
    Jun 2003
    Posts
    218
    (I declared a pointer because that's what MSDN showed me).
    Code:
    HBRUSH CreateBrushIndirect(
      CONST LOGBRUSH *lplb   // brush information
    );
    See?

    Anyways. Now the background is different, but it's BLACK! I don't see how it gets that color from "RBG(233,233,233)", but I'll figure this one out for myself for the time being.

    EDIT:
    Nevermind! I found my error.

  8. #8
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    (I declared a pointer because that's what MSDN showed me).
    MSDN told you to pass the address of a LOGBRUSH instance to CreateBrushIndirect(). You have to first create a LOGBRUSH instance and fill it. You then use the & operator to pass its address. Capeesh?

    It works fine for me. Post the code you're using.
    benforbes@optusnet.com.au
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  9. #9
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    What was your error?
    benforbes@optusnet.com.au
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  10. #10
    Registered User kinghajj's Avatar
    Join Date
    Jun 2003
    Posts
    218
    By Error was:
    Code:
    ...
    logBrush.lbColor = RGB(233,233,233);
    logBrush.lbColor = NULL;
    ...
    I forgot to change the second line to be "logBrush.lbHatch".

  11. #11
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    lol. I'm glad it's solved.
    benforbes@optusnet.com.au
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  12. #12
    Registered User kinghajj's Avatar
    Join Date
    Jun 2003
    Posts
    218

    Yeah...

    Thanks for helping me. Though, I do have another question:

    How do I make my buttons look "normal" (Like a normal Win9x button)? Currently, it looks really weird.

  13. #13
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    Post pictures to show what you mean.
    benforbes@optusnet.com.au
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  14. #14
    Registered User kinghajj's Avatar
    Join Date
    Jun 2003
    Posts
    218

  15. #15
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    Hmmm. Try creating the button with the style WS_EX_CLIENTEDGE, but I'm not too sure about that. Remember to use CreateWindowEx(), not CreateWindow().
    benforbes@optusnet.com.au
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Change text color in C
    By IndioDoido in forum C Programming
    Replies: 9
    Last Post: 04-15-2007, 06:54 AM
  2. Critique my lighting model.
    By psychopath in forum Game Programming
    Replies: 4
    Last Post: 08-12-2006, 07:23 PM
  3. winxp style background color problem
    By johny145 in forum Windows Programming
    Replies: 0
    Last Post: 07-06-2005, 11:22 AM
  4. How to change the Background of window
    By gaurav07 in forum Windows Programming
    Replies: 11
    Last Post: 06-27-2005, 01:48 PM
  5. Setting the background color of my main window
    By Garfield in forum Windows Programming
    Replies: 5
    Last Post: 07-07-2002, 12:25 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21