Thread: How to change the Background of window

  1. #1
    Registered User
    Join Date
    May 2005
    Posts
    19

    How to change the Background of window

    Hi,

    How to change the Background of window creted by us
    can I use RBG to have my own color combination.How could I

    Regards

  2. #2
    Tropical Coder Darryl's Avatar
    Join Date
    Mar 2005
    Location
    Cayman Islands
    Posts
    503
    You can use this when you initially register your window class.

    CreateSolidBrush(RGB(255,255,255))

    or

    SetClassLong(hwnd,GCL_HBRBACKGROUND,CreateSolidBru sh(RGB(255,255,255)))

    after the window has been registered
    Last edited by Darryl; 06-22-2005 at 07:48 AM.

  3. #3
    Registered User
    Join Date
    May 2005
    Posts
    19

    Thnx buddy

    Thnx.

  4. #4
    Registered User Joelito's Avatar
    Join Date
    Mar 2005
    Location
    Tijuana, BC, México
    Posts
    310
    Just remember that brushes that aren't stocked need to be deleted with DeleteObject.

  5. #5
    Tropical Coder Darryl's Avatar
    Join Date
    Mar 2005
    Location
    Cayman Islands
    Posts
    503
    Quote Originally Posted by Joelito
    Just remember that brushes that aren't stocked need to be deleted with DeleteObject.
    Good advice in most circumstance, however, not always applicable...per MS
    The system automatically deletes class background brushes when the class is unregistered by using UnregisterClass. An application should not delete these brushes.

  6. #6
    Registered User Joelito's Avatar
    Join Date
    Mar 2005
    Location
    Tijuana, BC, México
    Posts
    310
    UnregisterClass!

    Good point

  7. #7
    Registered User
    Join Date
    May 2005
    Posts
    19
    since i use theCretesolidbrush while difining the WNDCLASS
    do i need to delete brush using DeleteObject()

  8. #8
    Tropical Coder Darryl's Avatar
    Join Date
    Mar 2005
    Location
    Cayman Islands
    Posts
    503
    If you call unregisterclass at the end of your program, which is good practice, then no, you should not delete it. It will get deleted by unregisterclass.

    However, and I can't find a clear answer on this, if you don't call Unregisterclass, I am not sure if you need to delete the background brush because even when you don't call unregisterclass, windows still unregisters your class when the program ends.

  9. #9
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    use GetStockObject() and not worry about it......

    ie
    GetStockObject(WHITE_BRUSH)
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  10. #10
    Shibby willc0de4food's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    378
    i dont mean to intrude but now that we have the brush, how do you change the background color and text?

    and then, how do you change it to something else, say change it back to the original colors?
    Registered Linux User #380033. Be counted: http://counter.li.org

  11. #11
    Tropical Coder Darryl's Avatar
    Join Date
    Mar 2005
    Location
    Cayman Islands
    Posts
    503
    Quote Originally Posted by novacain
    use GetStockObject() and not worry about it......

    ie
    GetStockObject(WHITE_BRUSH)
    What if we want a nice green felt color as our background for our blackjack game. WHITE_BRUSH is not going to cut it. None of the 16 stock brushes will.
    Last edited by Darryl; 06-27-2005 at 10:55 AM.

  12. #12
    Tropical Coder Darryl's Avatar
    Join Date
    Mar 2005
    Location
    Cayman Islands
    Posts
    503
    Quote Originally Posted by willc0de4food
    i dont mean to intrude but now that we have the brush, how do you change the background color and text?

    and then, how do you change it to something else, say change it back to the original colors?
    I modified the example from winprog.net to demonstrate changing background colors. As far as text goes, you can use textout() for simple text outputs. But depending on your needs it can get pretty involed as there are a lot of sizing issues that must be considered. Especially if you are doing multiple lines and differnt fonts per line.

    Code:
    #include <windows.h>
    
    const char g_szClassName[] = "myWindowClass";
    HBRUSH oldBrush = 0;
    HBRUSH newBrush = 0;
    bool isChanged = false;
    
    LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
    {
        switch(msg)
        {
            case WM_LBUTTONDOWN:
            {
    			if (!isChanged)
    			{
    				newBrush = CreateSolidBrush(RGB(0,255,0)); // green
    				oldBrush = (HBRUSH)SetClassLong(hwnd,GCL_HBRBACKGROUND, (LONG)newBrush);
    				InvalidateRect(hwnd,0,TRUE);
    				isChanged = true;
    			}
            }
    		break;
    
    		case WM_RBUTTONDOWN:
            {
    			if (isChanged)
    			{
    				SetClassLong(hwnd,GCL_HBRBACKGROUND,(LONG)oldBrush);
    				DeleteObject (newBrush);
    				InvalidateRect(hwnd,0,TRUE);
    				isChanged = false;
    			}
            }
    
            break;
    
            case WM_CLOSE:
                DestroyWindow(hwnd);
            break;
            case WM_DESTROY:
    			if (isChanged)
    			{
    				SetClassLong(hwnd,GCL_HBRBACKGROUND,(LONG)oldBrush);
    				DeleteObject (newBrush);
    				isChanged = false;
    			}
                PostQuitMessage(0);
            break;
            default:
                return DefWindowProc(hwnd, msg, wParam, lParam);
        }
        return 0;
    }
    
    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
        LPSTR lpCmdLine, int nCmdShow)
    {
        WNDCLASSEX wc;
        HWND hwnd;
        MSG Msg;
    
        wc.cbSize        = sizeof(WNDCLASSEX);
        wc.style         = 0;
        wc.lpfnWndProc   = WndProc;
        wc.cbClsExtra    = 0;
        wc.cbWndExtra    = 0;
        wc.hInstance     = hInstance;
        wc.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
        wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
        wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
        wc.lpszMenuName  = NULL;
        wc.lpszClassName = g_szClassName;
        wc.hIconSm       = LoadIcon(NULL, IDI_APPLICATION);
    
        if(!RegisterClassEx(&wc))
        {
            MessageBox(NULL, "Window Registration Failed!", "Error!",
                MB_ICONEXCLAMATION | MB_OK);
            return 0;
        }
    
        hwnd = CreateWindowEx(
            WS_EX_CLIENTEDGE,
            g_szClassName,
            "The title of my window",
            WS_OVERLAPPEDWINDOW,
            CW_USEDEFAULT, CW_USEDEFAULT, 240, 120,
            NULL, NULL, hInstance, NULL);
    
        if(hwnd == NULL)
        {
            MessageBox(NULL, "Window Creation Failed!", "Error!",
                MB_ICONEXCLAMATION | MB_OK);
            return 0;
        }
    
        ShowWindow(hwnd, nCmdShow);
        UpdateWindow(hwnd);
    
        while(GetMessage(&Msg, NULL, 0, 0) > 0)
        {
            TranslateMessage(&Msg);
            DispatchMessage(&Msg);
        }
        return Msg.wParam;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 6 measly errors
    By beene in forum Game Programming
    Replies: 11
    Last Post: 11-14-2006, 11:06 AM
  2. how i create a window whith all it's elements
    By rasheed in forum Windows Programming
    Replies: 1
    Last Post: 05-31-2006, 06:53 PM
  3. Pong is completed!!!
    By Shamino in forum Game Programming
    Replies: 11
    Last Post: 05-26-2005, 10:50 AM
  4. opengl help
    By heat511 in forum Game Programming
    Replies: 4
    Last Post: 04-05-2004, 01:08 AM
  5. Winamp Vis if anyone can help
    By Unregistered in forum Windows Programming
    Replies: 6
    Last Post: 01-27-2002, 12:43 AM