Button Color

This is a discussion on Button Color within the Windows Programming forums, part of the Platform Specific Boards category; I have a button in my window and I want to change itís color using scroll bar. But I have ...

  1. #1
    Registered User alex6852's Avatar
    Join Date
    Sep 2001
    Posts
    43

    Question Button Color

    I have a button in my window and I want to change itís color using scroll bar. But I have no idea how to change colors of a button using pure WinAPI. There must be a function for it somewhere. Please help resolve this tiny problem.
    C++ rulez!!!

  2. #2
    Banned Cgawd's Avatar
    Join Date
    Oct 2002
    Posts
    405
    By-Fordy
    Code:
    #include <windows.h>
    
    char g_szClassName[] = "myWindowClass";
    HINSTANCE hInst;
    
    LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
    
    
    int APIENTRY WinMain(HINSTANCE hInstance,
    HINSTANCE hPrevInstance,
    LPSTR lpCmdLine,
    int nCmdShow)
    {
    WNDCLASSEX wc;
    HWND hwnd;
    MSG Msg;
    
    hInst = hInstance;
    //Step 1: Registering the Window Class
    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) GetStockObject (WHITE_BRUSH);
    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;
    }
    
    // Step 2: Creating the Window
    hwnd = CreateWindowEx(
    WS_EX_CLIENTEDGE,
    g_szClassName,
    "The Password Keeper",
    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);
    
    // Step 3: The Message Loop
    while(GetMessage(&Msg, NULL, 0, 0) > 0)
    {
    TranslateMessage(&Msg);
    DispatchMessage(&Msg);
    }
    return Msg.wParam;
    }
    
    LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
    {
    	switch(msg)
    	{
    		case WM_CLOSE:
    			DestroyWindow(hwnd);
    			break;
    		case WM_CREATE:
    					//create a default push button
    		CreateWindowEx(	0, //more or 'extended' styles
    					   	TEXT("BUTTON"), //'class' of control to create
    					   	TEXT("DEFAULT PUSH BUTTON"), //the control caption
    					   	WS_CHILD|WS_VISIBLE|BS_DEFPUSHBUTTON, //control style: how it looks
    					   	10, //control position: left
    					   	10, //control position: top
    						200, //control width
    						30, //control height
    						hwnd, //parent window handle
    						NULL, //control's ID
    						hInst, //application instance
    						NULL); 
    							//create a push button
    		CreateWindowEx( 0, //more or 'extended' styles
    						TEXT("BUTTON"), //'class' of control to create
    						TEXT("PUSH BUTTON"), //the control caption
    						WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON, //control style: how it looks
    						10, //control position: left
    						50, //control position: top
    						200, //control width
    						30, //control height
    						hwnd, //parent window handle
    						NULL, //control's ID
    						hInst, //application instance
    						NULL);
    		break;
    		case WM_DESTROY:
    			PostQuitMessage(0);
    			break;
    		default:
    		return DefWindowProc(hwnd, msg, wParam, lParam);
    	}
    return 0;
    }
    this is from a thread that fordy replied to

  3. #3
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    There are some suggestions here.

    BTW, Cgawd, the code you posted serves only to create 2 buttons - it won't alter their default appearance at all. Anyway, judging by some of the comments in that code, I have a pretty good idea where some of it originally came from...

    edit: typos

  4. #4
    Banned Cgawd's Avatar
    Join Date
    Oct 2002
    Posts
    405
    it came from fordy, as i said 2 times in that post

  5. #5
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,786
    Originally posted by Ken Fitlike
    BTW, Cgawd, the code you posted serves only to create 2 buttons - it won't alter their default appearance at all. Anyway, judging by some of the comments in that code, I have a pretty good idea where some of it originally came from...


    Often if I do a code example as an answer on this board I use Ken's site for the boilerplate code because its both accessible and reliable and I can quickly add what I want to it.....this is often if I am in work and dont have my templates or the time to do it.....

    So yeah....its Ken's code alright!

    "Plagiarism is the finest form of flattery"

  6. #6
    Registered User alex6852's Avatar
    Join Date
    Sep 2001
    Posts
    43
    Yes that information was very useful but I still can't solve my problem. Bitmap buttons will not help at all, and owner-drawn ones are too mesy to play with. I thought that you can use only brushes to change colors of any window component (buttons in this case) at any time while application is running. Please assist.
    C++ rulez!!!

  7. #7
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    Yes but you would have to use either of these strategies, or subclass the window and handle all/most of its drawing or design a button object of your own - which you would have to draw anyway.

    As I said in the post I originally referred you to regarding WM_CTLCOLORBTN message handling:
    Unfortunately that message doesn't work as described, windows just goes ahead and sets the colour to its default value
    Usually the WM_CTL* messages work just fine for other controls but not buttons.
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  8. #8
    Registered User
    Join Date
    Dec 2002
    Posts
    119
    >Usually the WM_CTL* messages work just fine for other controls but not buttons.
    When you handle the WM_CTLCOLORBTN message, you can change the button's background color, which is hidden behind the button. This is left over from win 3.1 Just FYI

  9. #9
    Refugee face_master's Avatar
    Join Date
    Aug 2001
    Posts
    2,052
    >> So yeah....its Ken's code alright!

    Ken: It's, uh, Bob's code!
    Please direct all complaints regarding this post to the nearest brick wall Have a nice day.

  10. #10
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    >>When you handle the WM_CTLCOLORBTN message, you can change the button's background color, which is hidden behind the button. This is left over from win 3.1 Just FYI<<

    Didn't know that. Thanks. Still can't change the button appearance using that message, though.

    >>Ken: It's, uh, Bob's code!<<

    oh, yeah?!?
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. screen color detection and button pressing
    By cwn11 in forum C Programming
    Replies: 1
    Last Post: 11-23-2008, 09:39 PM
  2. Critique my lighting model.
    By psychopath in forum Game Programming
    Replies: 4
    Last Post: 08-12-2006, 06:23 PM
  3. VC++ setting text color of a button
    By 7stud in forum Windows Programming
    Replies: 5
    Last Post: 10-14-2004, 11:04 AM
  4. MFC: Change the color of static text with button press?
    By BrianK in forum Windows Programming
    Replies: 2
    Last Post: 06-16-2004, 11:03 PM
  5. Just one Question?
    By Irish-Slasher in forum C++ Programming
    Replies: 6
    Last Post: 02-12-2002, 09:19 AM

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