我使用-20px
在主窗口中定义并创建了一堆子窗口
电话类型。
我现在需要创建一个允许我设置任何一个窗口的背景颜色的函数。 类似的东西:
handle_to_this_window[i] = CreateWindow(L"EDIT",...)
R_color_value = 0, G_color_value = 200, B_color_value = 0;
ChangeChildBackgroundColor(handle_to_this_window[6], R_color_value, G_color_value, B_color_value);
R_color_value = 200, G_color_value = 0, B_color_value = 0;
有人能指出我如何实现这个目标的正确方向吗?
答案 0 :(得分:2)
尝试这样的事情:
struct ChildControlInfo
{
HWND Wnd;
HBRUSH BkGndBrush;
COLORREF BkGndColor;
};
ChildControlInfo ChildControls[...];
...
void ChangeChildBackgroundColor(int ChildIndex, COLORREF NewColor)
{
ChildControlInfo &Child = ChildControls[ChildIndex];
if ((Child.BkGndColor != NewColor) || (!Child.BkGndBrush))
{
if (Child.BkGndBrush) DeleteObject(Child.BkGndBrush);
Child.BkGndBrush = CreateSolidBrush(NewColor);
Child.BkGndColor = NewColor;
if (Child.Wnd) InvalidateRect(Child.Wnd, NULL, TRUE);
}
}
// in the parent window procedure...
case WM_CTLCOLOREDIT:
{
HDC hDc = reinterpret_cast<HDC>(wParam);
HWND hEdit = reinterpret_cast<HWND>(lParam);
for(int i = 0; i < ARRAYSIZE(ChildControls); ++i)
{
ChildControlInfo &Child = ChildControls[i];
if (Child.Wnd == hEdit)
{
SetBkColor(hDc, Child.BkGndColor);
return reinterpret_cast<LRESULT>(Child.BkGndBrush);
}
}
break;
}
memset(&ChildControls[i], 0, sizeof(ChildControlInfo));
ChildControls[i].Wnd = CreateWindow(L"EDIT",...);.
ChangeChildBackgroundColor(i, RGB(...));
...
ChangeChildBackgroundColor(6, RGB(0, 200, 0));
ChangeChildBackgroundColor(7, RGB(200, 0, 0));
...
// during cleanup...
for(int i = 0; i < ARRAYSIZE(ChildControls); ++i)
{
ChildControlInfo &Child = ChildControls[i];
DestroyWindow(Child.Wnd);
DeleteObject(Child.BkGndBrush);
}
答案 1 :(得分:1)
您必须将画笔声明为全局变量:
HBRUSH gbrush;
在代码中的某处,您必须定义画笔。例如,在主窗口过程中:
case WM_CREATE:
{
gbrush = CreateSolidBrush(RGB(200, 255, 255));
//...
}
然后更改WM_CTLCOLOREDIT
中的“背景颜色”和“背景画笔”:
case WM_CTLCOLOREDIT:
{
HDC hdc = (HDC)wParam;
HWND hedit = (HWND)lParam;
SetBkColor(hdc, RGB(200, 255, 255));
return (LRESULT)gbrush;
}
这将更改所有编辑控件的背景颜色。您可以使用hedit
值以不同方式处理编辑框。