静态控制背景颜色与 C++

发布于 2024-10-08 13:15:43 字数 1233 浏览 3 评论 0原文

我正在使用 Windows API 创建一个基本的 GUI,但遇到了一个问题。它从一个主窗口开始,该窗口以我设置的自定义背景颜色(RGB(230,230,230))打开。然后,它使用静态控件在左上角显示文本。

settingstext = CreateWindow("STATIC",
                             "SETTINGS",
                             SS_LEFT | WS_CHILD,
                             12,
                             20,
                             100,
                             20,
                             hwnd,
                             NULL,
                             proginstance,
                             NULL);
ShowWindow(settingstext, 1);

这是可行的,但是当显示文本时,我需要一种方法来更改它的背景以匹配主窗口,否则它看起来就像没有融入其中。

我的问题是,我该怎么做?我目前使用下面的方法并且它有效,但我想知道,有没有一种方法可以在静态控件的 CreateWindow 函数之后以某种方式永久设置背景颜色而不更改系统颜色,以及只需将其应用于该一个控件,而不应用于发送 WM_CTLCOLORSTATIC 消息的任何控件。我尝试过在消息循环之外使用 GetDC 函数和 SetBkColor 函数,但没有任何效果。

    case WM_CTLCOLORSTATIC:
    {
    HDC hdcStatic = (HDC) wParam;
    SetTextColor(hdcStatic, RGB(0,0,0));
    SetBkColor(hdcStatic, RGB(230,230,230));
    return (INT_PTR)CreateSolidBrush(RGB(230,230,230));
    }

我想这样做是因为...

  • 我不想用每次重新绘制窗口时都需要调用的函数来填充我的消息循环。
  • 让更改仅应用于此静态控件。

我将非常感谢您提供的任何帮助,至少为我指明了正确的方向,谢谢。

I am creating a basic GUI with the Windows API and I have run into an issue. It starts with a main window that opens with a custom background color I set (RGB(230,230,230)). It then displays text in the upper left corner with the static control.

settingstext = CreateWindow("STATIC",
                             "SETTINGS",
                             SS_LEFT | WS_CHILD,
                             12,
                             20,
                             100,
                             20,
                             hwnd,
                             NULL,
                             proginstance,
                             NULL);
ShowWindow(settingstext, 1);

This works, but when the text is displayed I need a way to change the background of it to match the main window or else it just looks like it doesn't blend in.

My question is, how do I do this? I currently use the method below and it works, but I wanted to know, is there a way to permanently set the background color somehow, right after the CreateWindow function for the static control without changing system colors, and just have it apply to that one control and not anything that sends the WM_CTLCOLORSTATIC message. I have experimented around with using the GetDC function and SetBkColor function outside of the message loop but nothing works.

    case WM_CTLCOLORSTATIC:
    {
    HDC hdcStatic = (HDC) wParam;
    SetTextColor(hdcStatic, RGB(0,0,0));
    SetBkColor(hdcStatic, RGB(230,230,230));
    return (INT_PTR)CreateSolidBrush(RGB(230,230,230));
    }

I want to do this because...

  • I don't want to fill up my message loop with functions that need to be called every time the window repaints.
  • Have the changes apply to only this static control.

I would be very thankful for any help that could be provided, at least pointing me in the right direction, thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

桜花祭 2024-10-15 13:15:43

对于静态文本控件,没有永久的方法来设置文本颜色或其背景。即使您想将更改应用到单个静态控件;当控件即将被绘制时,您仍然需要在父 dlgproc 中处理 WM_CTLCOLORSTATIC 通知消息。

这是由于 DefWindowProc 每次处理 WM_CTLCOLORSTATIC 时都会覆盖您对设备上下文的更改,如 MSDN

默认情况下,DefWindowProc 函数为静态控件选择默认系统颜色。

static HBRUSH hBrush = CreateSolidBrush(RGB(230,230,230));

case WM_CTLCOLORSTATIC:
{
    if (settingstext == (HWND)lParam)

              //OR if the handle is unavailable to you, get ctrl ID

    DWORD CtrlID = GetDlgCtrlID((HWND)lParam); //Window Control ID
    if (CtrlID == IDC_STATIC1) //If desired control
    {
       HDC hdcStatic = (HDC) wParam;
       SetTextColor(hdcStatic, RGB(0,0,0));
       SetBkColor(hdcStatic, RGB(230,230,230));
       return (INT_PTR)hBrush;
    }
}

如果您希望使控件的背景在父对话框上透明,可以使用 SetBkMode(hdcStatic, TRANSPARENT)

For static text controls there's no permanent way to set the text color or their background. Even if you want to apply the changes to a single static control; you would still have to handle WM_CTLCOLORSTATIC notification message in parent dlgproc just when the control is about to be drawn.

This is due to the DefWindowProc overwriting your changes to the device context each time it handles WM_CTLCOLORSTATIC as stated in the MSDN:

By default, the DefWindowProc function selects the default system colors for the static control.

static HBRUSH hBrush = CreateSolidBrush(RGB(230,230,230));

case WM_CTLCOLORSTATIC:
{
    if (settingstext == (HWND)lParam)

              //OR if the handle is unavailable to you, get ctrl ID

    DWORD CtrlID = GetDlgCtrlID((HWND)lParam); //Window Control ID
    if (CtrlID == IDC_STATIC1) //If desired control
    {
       HDC hdcStatic = (HDC) wParam;
       SetTextColor(hdcStatic, RGB(0,0,0));
       SetBkColor(hdcStatic, RGB(230,230,230));
       return (INT_PTR)hBrush;
    }
}

If you're looking to make the control's background transparent over a parent dialog you could use SetBkMode(hdcStatic, TRANSPARENT).

紫南 2024-10-15 13:15:43

您是否考虑过对静态窗口进行子类化并进行所有者绘制?

Have you considered subclassing the static window and doing owner draw?

喜爱皱眉﹌ 2024-10-15 13:15:43

我认为有一个永久的方法可以做到这一点。

创建标签后,使用 GetDC() 函数获取设备上下文。
然后使用:

SetTextColor(hdcStatic, RGB(0,0,0));
SetBkColor(hdcStatic, RGB(230,230,230)); // Code Copied from the above answer by cpx.

它应该可以。

I think there is a permanent way to do it.

Just after you create the label,use GetDC() function to get the Device Context.
Then use:

SetTextColor(hdcStatic, RGB(0,0,0));
SetBkColor(hdcStatic, RGB(230,230,230)); // Code Copied from the above answer by cpx.

And it should do .

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文