win32-改变Combox的编辑框和下拉列表的背景颜色和文本字体颜色

Posted strive-sun

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了win32-改变Combox的编辑框和下拉列表的背景颜色和文本字体颜色相关的知识,希望对你有一定的参考价值。

只需要调用WM_CTLCOLORLISTBOX和WM_CTLCOLOREDIT来分别处理编辑框和下拉列表。

Combox的创建:

int xpos = 100;            // Horizontal position of the window.
   int ypos = 100;            // Vertical position of the window.
   int nwidth = 200;          // Width of the window
   int nheight = 200;         // Height of the window
   HWND hwndParent = hWnd; // Handle to the parent window

   hWndComboBox = CreateWindow(WC_COMBOBOX, TEXT(""),
       CBS_DROPDOWNLIST | WS_CHILD |  WS_VISIBLE, // CBS_HASSTRINGS | WS_OVERLAPPED |
       xpos, ypos, nwidth, nheight, hwndParent, NULL, hInstance,
       NULL);

   // load the combobox with item list.  
   // Send a CB_ADDSTRING message to load each item

   TCHAR Planets[9][10] =
   {
       TEXT("Mercury"), TEXT("Venus"), TEXT("Terra"), TEXT("Mars"),
       TEXT("Jupiter"), TEXT("Saturn"), TEXT("Uranus"), TEXT("Neptune"),
       TEXT("Pluto??")
   };

   TCHAR A[16];
   int  k = 0;

   memset(&A, 0, sizeof(A));
   for (k = 0; k <= 8; k += 1)
   {
       wcscpy_s(A, sizeof(A) / sizeof(TCHAR), (TCHAR*)Planets[k]);

       // Add string to combobox.
       SendMessage(hWndComboBox, (UINT)CB_ADDSTRING, (WPARAM)0, (LPARAM)A);
   }

   // Send the CB_SETCURSEL message to display an initial item 
   //  in the selection field  
   SendMessage(hWndComboBox, CB_SETCURSEL, (WPARAM)2, (LPARAM)0);

这部分可以参考: How to Create a Simple Combo Box

接下来处理窗口处理过程:

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch (message)
    {
    
    case WM_CTLCOLORLISTBOX:
    {
        HDC dc = (HDC)wParam;
        SetBkMode(dc, OPAQUE);
        SetTextColor(dc, RGB(255, 255, 255));
        SetBkColor(dc, 0x383838); //0x383838
        HBRUSH comboBrush = CreateSolidBrush(0x383838); //global var
        return (LRESULT)comboBrush;      
    }
    case WM_CTLCOLOREDIT:
    {
        HDC dc = (HDC)wParam;
        SetBkMode(dc, OPAQUE);
        SetTextColor(dc, RGB(255, 255, 255));
        SetBkColor(dc, 0x383838); //0x383838
        HBRUSH comboBrush = CreateSolidBrush(0x383838); 
        return (LRESULT)comboBrush;
    }
    ...

最后的调试结果:

技术图片

以上是关于win32-改变Combox的编辑框和下拉列表的背景颜色和文本字体颜色的主要内容,如果未能解决你的问题,请参考以下文章

C# winform combox 下拉框选项过长,显示不全,怎么解决

pyqt5 combox控件下拉框里添加字典

winform解决datagridview里放combox,combox不能按下键快速选择的问题

C#使用 ComboBox 控件

c#里面ComBox下拉框里面比较里面是不是有这个值

combox是啥