Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 744
user63898 Avatar asked Jun 10 '11 06:06

user63898


2 Answers

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.

like image 118
Mike Kwan Avatar answered Nov 04 '22 21:11

Mike Kwan


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.

like image 1
RobH Avatar answered Nov 04 '22 23:11

RobH