码迷,mamicode.com
首页 > Windows程序 > 详细

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

时间:2020-06-29 11:30:37      阅读:89      评论:0      收藏:0      [点我收藏+]

标签:osi   ext   背景   ase   simple   接下来   ica   inf   cal   

只需要调用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的编辑框和下拉列表的背景颜色和文本字体颜色

标签:osi   ext   背景   ase   simple   接下来   ica   inf   cal   

原文地址:https://www.cnblogs.com/strive-sun/p/13206712.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!