WINAPI - 设置组合框下拉菜单的背景和文本颜色
WINAPI - Set background & text color of a combo box dropdown
所以我尝试使用 Common-Controls 和 WINAPI 设置 ComboBox 的背景和文本颜色。
我确实设法设置了组合框本身的背景和文本颜色,但其下拉列表的颜色保持不变。
这就是我所做的:
创建组合框时,我使用了 CBS_DROPDOWNLIST
样式(以及 WS_VISIBLE
和 WS_CHILD
)。
然后在 window 处理函数中,我按以下方式处理 CTLCOLOR_LISTBOX
消息:
SetBkMode(dc, OPAQUE);
SetTextColor(dc, RGB(255, 255, 255));
SetBkColor(dc, 0x383838);
comboBrush = CreateSolidBrush(0x383838); //global var
return (LRESULT)comboBrush;
正如我所说,这只会为组合框本身着色,而不是它的下拉列表。
我怎样才能为下拉列表着色?
组合框的背景色和文字颜色,可以分别处理WM_CTLCOLORLISTBOX and WM_CTLCOLOREDIT条消息。
WM_CTLCOLORLISTBOX : Sent to the parent window of a list box before
the system draws the list box. By responding to this message, the
parent window can set the text and background colors of the list box
by using the specified display device context handle.
一些代码:
// Create Combox control
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,
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);
...
更新:
//Windows Process
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_CTLCOLORLISTBOX:
{
COMBOBOXINFO info;
info.cbSize = sizeof(info);
SendMessage(hWndComboBox, CB_GETCOMBOBOXINFO, 0, (LPARAM)&info);
COMBOBOXINFO info1;
info1.cbSize = sizeof(info1);
SendMessage(hWndComboBox1, CB_GETCOMBOBOXINFO, 0, (LPARAM)&info1);
if ((HWND)lParam == info.hwndList)
{
HDC dc = (HDC)wParam;
SetBkMode(dc, OPAQUE);
SetTextColor(dc, RGB(255, 255, 0));
SetBkColor(dc, 0x383838); //0x383838
HBRUSH comboBrush = CreateSolidBrush(0x383838); //global var
return (LRESULT)comboBrush;
}
if ((HWND)lParam == info1.hwndList)
{
HDC dc = (HDC)wParam;
SetBkMode(dc, OPAQUE);
SetTextColor(dc, RGB(255, 0, 0));
SetBkColor(dc, RGB(0, 0, 255));
HBRUSH comboBrush = CreateSolidBrush(RGB(0, 0, 255));
return (LRESULT)comboBrush;
}
}
case WM_CTLCOLOREDIT:
{
HWND hWnd = (HWND)lParam;
HDC dc = (HDC)wParam;
if (hWnd == hWndComboBox)
{
SetBkMode(dc, OPAQUE);
SetTextColor(dc, RGB(255, 0, 255));
SetBkColor(dc, 0x383838); //0x383838
HBRUSH comboBrush = CreateSolidBrush(0x383838); //global var
return (LRESULT)comboBrush;
}
else if (hWnd == hWndComboBox1)
{
SetBkMode(dc, OPAQUE);
SetTextColor(dc, RGB(255, 255, 0));
SetBkColor(dc, RGB(0, 255, 0));
HBRUSH comboBrush = CreateSolidBrush(RGB(0, 255, 0));
return (LRESULT)comboBrush;
}
}
...
通过比较 window 返回的编辑句柄和列表框句柄来更改背景颜色。
调试:
所以我尝试使用 Common-Controls 和 WINAPI 设置 ComboBox 的背景和文本颜色。 我确实设法设置了组合框本身的背景和文本颜色,但其下拉列表的颜色保持不变。
这就是我所做的:
创建组合框时,我使用了 CBS_DROPDOWNLIST
样式(以及 WS_VISIBLE
和 WS_CHILD
)。
然后在 window 处理函数中,我按以下方式处理 CTLCOLOR_LISTBOX
消息:
SetBkMode(dc, OPAQUE);
SetTextColor(dc, RGB(255, 255, 255));
SetBkColor(dc, 0x383838);
comboBrush = CreateSolidBrush(0x383838); //global var
return (LRESULT)comboBrush;
正如我所说,这只会为组合框本身着色,而不是它的下拉列表。 我怎样才能为下拉列表着色?
组合框的背景色和文字颜色,可以分别处理WM_CTLCOLORLISTBOX and WM_CTLCOLOREDIT条消息。
WM_CTLCOLORLISTBOX : Sent to the parent window of a list box before the system draws the list box. By responding to this message, the parent window can set the text and background colors of the list box by using the specified display device context handle.
一些代码:
// Create Combox control
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,
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);
...
更新:
//Windows Process
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_CTLCOLORLISTBOX:
{
COMBOBOXINFO info;
info.cbSize = sizeof(info);
SendMessage(hWndComboBox, CB_GETCOMBOBOXINFO, 0, (LPARAM)&info);
COMBOBOXINFO info1;
info1.cbSize = sizeof(info1);
SendMessage(hWndComboBox1, CB_GETCOMBOBOXINFO, 0, (LPARAM)&info1);
if ((HWND)lParam == info.hwndList)
{
HDC dc = (HDC)wParam;
SetBkMode(dc, OPAQUE);
SetTextColor(dc, RGB(255, 255, 0));
SetBkColor(dc, 0x383838); //0x383838
HBRUSH comboBrush = CreateSolidBrush(0x383838); //global var
return (LRESULT)comboBrush;
}
if ((HWND)lParam == info1.hwndList)
{
HDC dc = (HDC)wParam;
SetBkMode(dc, OPAQUE);
SetTextColor(dc, RGB(255, 0, 0));
SetBkColor(dc, RGB(0, 0, 255));
HBRUSH comboBrush = CreateSolidBrush(RGB(0, 0, 255));
return (LRESULT)comboBrush;
}
}
case WM_CTLCOLOREDIT:
{
HWND hWnd = (HWND)lParam;
HDC dc = (HDC)wParam;
if (hWnd == hWndComboBox)
{
SetBkMode(dc, OPAQUE);
SetTextColor(dc, RGB(255, 0, 255));
SetBkColor(dc, 0x383838); //0x383838
HBRUSH comboBrush = CreateSolidBrush(0x383838); //global var
return (LRESULT)comboBrush;
}
else if (hWnd == hWndComboBox1)
{
SetBkMode(dc, OPAQUE);
SetTextColor(dc, RGB(255, 255, 0));
SetBkColor(dc, RGB(0, 255, 0));
HBRUSH comboBrush = CreateSolidBrush(RGB(0, 255, 0));
return (LRESULT)comboBrush;
}
}
...
通过比较 window 返回的编辑句柄和列表框句柄来更改背景颜色。
调试: