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

问题描述

我有一个自绘组合框,我想在其中重新创建标准行为,即当前所选项目上的蓝色背景色。我试过在收到 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;
      }

我所指的行为示例:

enter image description here

它目前的样子:

enter image description here

完整代码:

#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)) \
    X(L"Red",RGB(255,0)) \
    X(L"Blue",255)) \
    X(L"Green",128,0)) \
    X(L"Yellow",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,270,170,hInstance,0);  


    while (GetMessage(&msg,NULL,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",WS_CHILD | WS_VISIBLE | CBS_DROPDOWNLIST |
                CBS_OWNERDRAWFIXED | CBS_AUTOHSCROLL | WS_VSCROLL | WS_HSCROLL,10,110,hwnd,g_hinst,NULL);
            SendMessage(hwndCombo,CB_SETMINVISIBLE,5,0);
            SetDefaultFont(hwndCombo);
            for (int i = 0; i < COUNTOF(items); ++i)
            {
                SendMessage(hwndCombo,CB_ADDSTRING,(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,DT_CENTER | DT_VCENTER | DT_SINGLELINE);
            }
            else
            {
                //drawColorRect(b->hDC,0);
                DrawText(b->hDC,L"Select",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,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);
    DrawBorder(dc,&rt);
    //FrameRect(dc,&rt,getBrushAt(0));
    FillRect(dc,getBrushAt(colorIndex));
}

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

解决方法

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

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

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