开发者

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

开发者 https://www.devze.com 2023-03-12 02:13 出处:网络
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.

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:

                    Messa开发者_StackOverflow社区geBox(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?


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.


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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消