使用C ++的静态控制背景色 [英] Static Control Background Color with C++

查看:99
本文介绍了使用C ++的静态控制背景色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Windows API创建基本的GUI,但是遇到了问题.它从一个主窗口开始,该窗口以我设置为(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.

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

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 would be very thankful for any help that could be provided, at least pointing me in the right direction, thanks.

推荐答案

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

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.

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

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

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

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

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

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

这篇关于使用C ++的静态控制背景色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆