如何在所选项目上设置组合框的背景颜色?

How do I set combobox's background color on selected item?

我有一个所有者绘制的组合框,我想在其中重新创建标准行为,即当前所选项目上方的蓝色背景色。我试过在收到 WM_DRAWITEM 消息时设置 X,但没有像这样:

    case WM_DRAWITEM:
    {
      LPDRAWITEMSTRUCT b = (LPDRAWITEMSTRUCT) lParam;
      if(b->itemAction & ODA_FOCUS)
      {
        SetBkMode(b->hDC, TRANSPARENT);
        SetBkColor(b->hDC, GetSysColor(COLOR_HIGHLIGHT));
        DrawFocusRect(b->hDC, &b->rcItem);
        return (INT_PTR) TRUE;
      }

我所指的行为示例:

目前的情况:

完整代码:

#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 <assert.h>

LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
void SetDefaultFont(HWND hwnd);
HFONT LoadSystemDefaultFont();
void DrawBorder(HDC hdc, RECT *rect);
void DrawLine(HDC hdc, LONG x1, LONG y1, LONG x2, LONG y2);
void freeBrushes();
HBRUSH getBrushAt(int index);
void drawColorRect(HDC dc, RECT *editRect, int colorIndex);

HINSTANCE g_hinst;
HFONT hDefaultSystemFont;

#define COUNTOF(a) (sizeof(a)/sizeof(a[0]))
#define LIST \
    X(L"Black", RGB(0, 0, 0)) \
    X(L"Red", RGB(255, 0, 0)) \
    X(L"Blue", RGB(0, 0, 255)) \
    X(L"Green", RGB(0, 128, 0)) \
    X(L"Yellow", RGB(255, 255, 0))

#define X(a, b) a,
const wchar_t *items[] = { LIST };
#undef X

#define X(a, b) b,
const int colorCodes[] = { LIST };
#undef X

HBRUSH brushes[COUNTOF(items)];

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

  
    HWND hwnd;
    MSG  msg;
    WNDCLASSW wc = {0};
    wc.lpszClassName = L"Application";
    wc.hInstance     = hInstance ;
    wc.hbrBackground = GetSysColorBrush(COLOR_3DFACE);
    wc.lpfnWndProc   = WndProc;
    wc.hCursor       = LoadCursor(0,IDC_ARROW);

    g_hinst = hInstance;

    RegisterClassW(&wc);
    hwnd = CreateWindowW(wc.lpszClassName, L"Combo box",
                  WS_OVERLAPPEDWINDOW | WS_VISIBLE,
                  100, 100, 270, 170, 0, 0, hInstance, 0);  


    while (GetMessage(&msg, NULL, 0, 0)) {
        DispatchMessage(&msg);
    }
    
    DeleteObject(hDefaultSystemFont);
    freeBrushes();

    return (int) msg.wParam;
}

LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, 
        WPARAM wParam, LPARAM lParam) {

    static HWND hwndCombo;

    switch(msg)
    {
        case WM_CREATE:
        {
            hwndCombo = CreateWindow(L"Combobox", NULL, 
                WS_CHILD | WS_VISIBLE | CBS_DROPDOWNLIST |
                CBS_OWNERDRAWFIXED | CBS_AUTOHSCROLL | WS_VSCROLL | WS_HSCROLL,
                10, 10, 100, 110, hwnd, NULL, g_hinst, NULL);
            SendMessage(hwndCombo, CB_SETMINVISIBLE, 5, 0);
            SetDefaultFont(hwndCombo);
            for (int i = 0; i < COUNTOF(items); ++i)
            {
                SendMessage(hwndCombo, CB_ADDSTRING, 0, (LPARAM) items[i]);
            }
        }
        break;

        case WM_DRAWITEM:
        {
          LPDRAWITEMSTRUCT b = (LPDRAWITEMSTRUCT) lParam;
          if(b->itemAction & ODA_FOCUS)
          {
            SetBkMode(b->hDC, TRANSPARENT);
            SetBkColor(b->hDC, GetSysColor(COLOR_HIGHLIGHT));
            DrawFocusRect(b->hDC, &b->rcItem);
            return (INT_PTR) TRUE;
          }
          else if(b->itemAction & ODA_DRAWENTIRE)
          {
            if(b->itemAction & ODA_FOCUS)
            {
                DrawFocusRect(b->hDC, &b->rcItem);
            }

            if(b->itemID != -1)
            {
                drawColorRect(b->hDC, &b->rcItem, b->itemID);
                DrawText(b->hDC, items[b->itemID], -1, &b->rcItem, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
            }
            else
            {
                //drawColorRect(b->hDC, &b->rcItem, 0);
                DrawText(b->hDC, L"Select", -1, &b->rcItem, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
            }

            return (INT_PTR) TRUE;
          }
        }
        break;

        case WM_DESTROY:
            PostQuitMessage(0);
            break; 
    }

  
    return DefWindowProc(hwnd, msg, wParam, lParam);
}


HFONT LoadSystemDefaultFont()
{
  if(hDefaultSystemFont == NULL) {
    NONCLIENTMETRICS ncm;
    ncm.cbSize = sizeof(NONCLIENTMETRICS);
    SystemParametersInfo(SPI_GETNONCLIENTMETRICS, sizeof(NONCLIENTMETRICS), &ncm, 0);
    hDefaultSystemFont = CreateFontIndirect(&ncm.lfMessageFont);
  }
  return hDefaultSystemFont;
}


void SetDefaultFont(HWND hwnd)
{
    SendMessage(hwnd, WM_SETFONT, (LPARAM) LoadSystemDefaultFont(), TRUE);
}

void drawColorRect(HDC dc, RECT *editRect, int colorIndex)
{
    assert(colorIndex >= 0 && colorIndex < COUNTOF(brushes));
    assert(editRect != NULL);

    RECT rt = {0};
    rt.left = editRect->left + 2;
    rt.top = editRect->top - 2;
    rt.right = editRect->right / 5;
    rt.bottom = editRect->bottom - 2;
    InflateRect(&rt, -1, -1);
    DrawBorder(dc, &rt);
    //FrameRect(dc, &rt, getBrushAt(0));
    FillRect(dc, &rt, getBrushAt(colorIndex));
}

void DrawLine(HDC hdc, LONG x1, LONG y1, LONG x2, LONG y2)
{
    MoveToEx(hdc, x1, y1, NULL);
    LineTo(hdc, x2, y2);
}
    
void DrawBorder(HDC hdc, RECT *rect)
{
    DrawLine(hdc, rect->left, rect->top, rect->left, rect->bottom);
    DrawLine(hdc, rect->left, rect->top, rect->right, rect->top);
    DrawLine(hdc, rect->right, rect->top, rect->right, rect->bottom);
    DrawLine(hdc, rect->left, rect->bottom, rect->right, rect->bottom);
}

HBRUSH getBrushAt(int index)
{
    assert(index >= 0 && index < COUNTOF(brushes));
    HBRUSH b = brushes[index];
    if(b == NULL) {
        int code = colorCodes[index];
        brushes[index] = CreateSolidBrush(code);
        b = brushes[index];
    }
    return b;
}

void freeBrushes()
{
    for(int i = 0; i < COUNTOF(brushes); ++i){
        DeleteObject(brushes[i]);
        brushes[i] = NULL;
    }
}

要用不同的背景和文本颜色绘制所选项目,您需要检查给定 DRAWITEMSTRUCT (lParam) 参数的 itemState 成员 [=14] =]旗帜。

类似于以下内容:

    case WM_DRAWITEM:
    {
        LPDRAWITEMSTRUCT b = (LPDRAWITEMSTRUCT) lParam;
        SetBkMode(b->hDC, TRANSPARENT);
        if(b->itemState & ODS_SELECTED) // Selected ...
        {
            SetTextColor(b->hDC, GetSysColor(COLOR_HIGHLIGHTTEXT));
            FillRect(b->hDC, &b->rcItem, (HBRUSH)(COLOR_HIGHLIGHT+1));
        }
        else // Not selected ...
        {
            SetTextColor(b->hDC, GetSysColor(COLOR_WINDOWTEXT));
            FillRect(b->hDC, &b->rcItem, (HBRUSH)(COLOR_WINDOW+1));
        }
        DrawText(b->hDC, items[b->itemID], -1, &b->rcItem, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
        // Check for focus ...
        if(b->itemAction & ODA_FOCUS)
        {
            DrawFocusRect(b->hDC, &b->rcItem);
        }
        return (I
NT_PTR) TRUE;

注意:当为您的 FillRect 调用的 hbr 参数使用系统颜色时,您 需要在值 上加一。来自 documentation for FillRect:

...If specifying a color value for the hbr parameter, it must be one of the standard system colors (the value 1 must be added to the chosen color).