繁体   English   中英

使用窗口的句柄设置子窗口的背景色

[英]setting background color of child window using window's handle

我已经使用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; ChangeChildBackgroundColor(handle_to_this_window[7], R_color_value, G_color_value, B_color_value);

任何人都可以为我指出实现该目标的正确方向吗?

尝试这样的事情:

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);
}

您必须将画笔声明为全局变量:

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值以不同方式处理编辑框。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM