Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
204 views
in Technique[技术] by (71.8m points)

winapi - How do I set the GroupBox's Caption color?

I'm handling WM_CTLCOLORSTATIC like this:

case WM_CTLCOLORSTATIC:
{
  HWND fromHwnd = (HWND) lParam;
  if(fromHwnd == hGroupBox)
  {
    HDC hdcStatic = (HDC) wParam;
    SetTextColor(hdcStatic, RGB(255, 255, 0));
    SetBkMode(hdcStatic, TRANSPARENT);
    return (LRESULT) bckBrush;
  }
}
break;

It uses the returned brush as background color but the SetTextColor() has no effect at all. What am I missing?

Here's all my code:

#pragma comment(lib, "user32.lib")
#pragma comment(lib, "Comctl32.lib")
#pragma comment(lib, "Gdi32.lib")

#define WIN32_LEAN_AND_MEAN
#define UNICODE
#define _UNICODE

#include <windows.h>
#include <Commctrl.h>
#include <crtdbg.h>
#include <strsafe.h>
#include <string.h>
#include <assert.h>

LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);

HWND hGroupBox;
HBRUSH bckBrush;

int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
                    PWSTR pCmdLine, int nCmdShow)
{

    MSG  msg = {0};
    HWND hwnd;
    WNDCLASSW wc = {0};

    wc.lpszClassName = L"Window";
    wc.hInstance     = hInstance;
    wc.hbrBackground = GetSysColorBrush(COLOR_3DFACE);
    wc.lpfnWndProc   = WndProc;
    wc.hCursor = LoadCursor(0, IDC_ARROW);

    if(!RegisterClass(&wc)) {
        assert(!"register class error");
    }

    bckBrush = CreateSolidBrush(RGB(0, 128, 0));

    int width = 500;
    int height = 350;
    int screenWidth = GetSystemMetrics(SM_CXSCREEN);
    int screenHeight = GetSystemMetrics(SM_CYSCREEN);
    int cx = (screenWidth - width) / 2;
    int cy = (screenHeight - height) / 2;
    hwnd = CreateWindowW(wc.lpszClassName, L"Window",
                        WS_OVERLAPPEDWINDOW | WS_VISIBLE,
                        cx, cy, width, height, NULL, NULL, 
                        hInstance, NULL);

    while (GetMessage(&msg, NULL, 0, 0))
    {
        if (!IsDialogMessage(hwnd, &msg))
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
    }

    return (int) msg.wParam;
}

LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
  switch(msg)
  {
      case WM_CREATE:
        CreateWindowW(L"Static", L"This is label 1...",
          WS_VISIBLE | WS_CHILD | WS_TABSTOP,
          50, 10, 130, 25, hwnd, (HMENU) 18, NULL, NULL);
        CreateWindowW(L"Static", L"This is label 2...",
          WS_VISIBLE | WS_CHILD | WS_TABSTOP,
          50, 40, 130, 25, hwnd, (HMENU) 19, NULL, NULL);
        hGroupBox =
        CreateWindowW(L"button", L"Pick a city",
            WS_VISIBLE | WS_CHILD | WS_TABSTOP | BS_GROUPBOX,
            50, 75, 200, 150, hwnd,
            (HMENU) 20, NULL, NULL);
      break;
      
    case WM_CTLCOLORSTATIC:
    {
      HWND fromHwnd = (HWND) lParam;
      if(fromHwnd == hGroupBox)
      {
        HDC hdcStatic = (HDC) wParam;
        SetTextColor(hdcStatic, RGB(255, 255, 0));
        SetBkMode(hdcStatic, TRANSPARENT);
        return (LRESULT) bckBrush;
      }
    }
    break;

    case WM_DESTROY:
      DeleteObject(bckBrush);
      bckBrush = NULL;
      PostQuitMessage(0);
      return 0;
  }

  return DefWindowProc(hwnd, msg, wParam, lParam);
}
question from:https://stackoverflow.com/questions/65851922/how-do-i-set-the-groupboxs-caption-color

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...