如何设置 GroupBox 的标题颜色?

问题描述

我是这样处理WM_CTLCOLORSTATIC的:

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;

它使用返回的画笔作为背景颜色,但 SetTextColor() 根本没有效果。我错过了什么?

这是我所有的代码

#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,hInstance,NULL);

    while (GetMessage(&msg,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);
        CreateWindowW(L"Static",L"This is label 2...",40,(HMENU) 19,NULL);
        hGroupBox =
        CreateWindowW(L"button",L"Pick a city",WS_VISIBLE | WS_CHILD | WS_TABSTOP | BS_GROUPBox,75,200,150,(HMENU) 20,NULL);
      break;
      
    case WM_CTLCOLORSTATIC:
    {
      HWND fromHwnd = (HWND) lParam;
      if(fromHwnd == hGroupBox)
      {
        HDC hdcStatic = (HDC) wParam;
        SetTextColor(hdcStatic,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);
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)