how to set win32 api c++ button background color and text color?

18,091

Solution 1

You can't, or at least you can't do so simply. If you want a coloured button then you need to set the owner-draw style and draw it yourself. Plain old buttons don't have customisable colours.

Solution 2

There are several approaches you could take for this:

  • Use bitmaps as your buttons
  • Ownerdraw the buttons
  • Handle NM_CUSTOMDRAW

The easiest way is just to handle WM_CTLCOLORBTN.

Solution 3

You can do it as described by the Forger. He has a very good tutorial for other bits of Windows Programmering as well.

Check it out here

Share:
18,091
user63898
Author by

user63898

developer

Updated on June 04, 2022

Comments

  • user63898
    user63898 almost 2 years

    im using simple button in win32 application and i like to change its color and text but i can't find from all searching in google how to do it. i have this code that represent a button: this is in the rc file :

    LANGUAGE LANG_NEUTRAL, SUBLANG_NEUTRAL
    IDD_DIALOG1 DIALOG 0, 0, 273, 209
    STYLE DS_3DLOOK | DS_CENTER | DS_MODALFRAME | DS_SHELLFONT | WS_CAPTION | WS_VISIBLE | WS_POPUP | WS_SYSMENU
    CAPTION "Win32  demo"
    FONT 8, "Ms Shell Dlg"
    {
        DEFPUSHBUTTON   "My Button1 ", IDOK, 59, 176, 69, 14
        PUSHBUTTON      "Log Off", IDC_BUTTON1, 155, 175, 54, 14
     }
    

    and in the main cpp file i only triger simple Massegebox alerts

    case WM_COMMAND:
                switch(LOWORD(wParam))
                {
                    case IDOK:
    
                        MessageBox(NULL, L"IDOK button pressed!", L"Pinky says...", MB_OK | MB_ICONEXCLAMATION);
                        break;
                    case IDC_BUTTON1:
                        MessageBox(NULL, L"IDC_BUTTON1 button pressed!",L"Pinky says...", MB_OK | MB_ICONEXCLAMATION);
                        break;
                    break;
                }
            break;
    

    where and how i change the buttons background colors and text?

  • user63898
    user63898 almost 13 years
    Thanks for the replay , can you please point me to some king of beginner tutorial ?
  • Mike Kwan
    Mike Kwan almost 13 years
    That tutorial describes handling WM_CTLCOLORSTATIC. He needs to handle WM_CTLCOLORBTN.
  • default
    default almost 13 years
    It sure does, and at the end describes What about all the other controls! which describes what is about all the other controls. Guilty as charged for not giving him the link to WM_CTLCOLORBTN.
  • RobH
    RobH almost 13 years
    You can start here.