如何使用Windows API在C ++中更改组合框的背景颜色?
答案 0 :(得分:0)
通常,您可以在堆中创建画笔,例如使用
static HBRUSH hbrush;
...
hbrush = CreateSolidBrush(RGB(255,0,0));
然后在Window或Dialog过程中,处理WM_CTL***
消息并返回画笔句柄,例如
case WM_CTLCOLORBTN:
case WM_CTLCOLORSTATIC:
case WM_CTLCOLOREDIT:
case WM_CTLCOLORLISTBOX:
{
HDC hdc = (HDC)wParam;
SetTextColor(hdc, RGB(0, 0, 255)); //change text color
SetBkMode(hdc, TRANSPARENT); //change text-background color, or set it to transparent
return (INT_PTR)hbrush;
}
这也适用于ComboBox。如果您想单独处理不同的控件,可以针对控件的句柄检查lParam
。
对于ComboBox来说,它有点复杂。你必须获得ComboBox的EditBox和ListBox的句柄。您可以使用GetComboBoxInfo
case WM_CTLCOLOREDIT:
case WM_CTLCOLORLISTBOX:
{
HWND hTest = (HWND)lParam;
COMBOBOXINFO ci = { sizeof(COMBOBOXINFO) };
GetComboBoxInfo(hComboBox, &ci);
if (
hTest == ci.hwndItem ||
hTest == ci.hwndList
)
{
HDC hdc = (HDC)wParam;
SetTextColor(hdc, RGB(0, 0, 255));
SetBkMode(hdc, TRANSPARENT);
return (INT_PTR)hbrush;
}
}