Windows 7 忽略 MFC CButton 背景
要更改 MFC 复选框和单选按钮的外观(背景颜色和文本前景色),我使用了以下实现,该实现在 Windows2000 中运行良好,在 Windows XP 中运行良好,但在 Windows 7 中
BEGIN_MESSAGE_MAP(mycheckbox, CButton)
...
ON_WM_CTLCOLOR_REFLECT()
...
END_MESSAGE_MAP()
HBRUSH mycheckbox::CtlColor(CDC* pDC, UINT nCtlColor)
{
pDC->SetBkColor( RGB( 255, 0, 0 ) );
pDC->SetTextColor( RGB( 0, 255, 0 ) );
return m_brush;
}
运行不佳 :使用 Windows 经典主题。 但是,当使用不同的主题时:
- Windows XP 中的症状:
SetBkColor
有效,但SetTextColor
被忽略 - Windows 7 中的症状:
SetBkColor
和>SetTextColor
被忽略
我尝试 OnEraseBkgnd 用自定义颜色填充背景(pDC->FillSolidRect
),但即使这样也没有效果。
我想避免使用ownerdrawn OnPaint
,以便复选标记和单选标记保持遵循Windows 中活动的主题。正如之前所写,这段代码用于W2000、Windows Xp、Vista和Windows 7...我只想更改背景颜色和文本颜色。
To change the appearance (background color and text foreground color) of a MFC checkbox and a radiobutton, I used the following implementation which worked fine in Windows2000, half OK in Windows XP, but not OK in Windows 7:
BEGIN_MESSAGE_MAP(mycheckbox, CButton)
...
ON_WM_CTLCOLOR_REFLECT()
...
END_MESSAGE_MAP()
HBRUSH mycheckbox::CtlColor(CDC* pDC, UINT nCtlColor)
{
pDC->SetBkColor( RGB( 255, 0, 0 ) );
pDC->SetTextColor( RGB( 0, 255, 0 ) );
return m_brush;
}
This works fine as long as the Windows Classic theme is used.
However, when using a different theme:
- Symptoms in Windows XP:
SetBkColor
works butSetTextColor
is ignored - Symptoms in Windows 7: both
SetBkColor
andSetTextColor
are ignored
I tried OnEraseBkgnd to fill the background with a custom color (pDC->FillSolidRect
) but even this had no effect.
I want to avoid using ownerdrawn OnPaint
so the check and radio marks keep following the theme that is active in Windows. As written before, this code is used in W2000, Windows Xp, Vista and Windows 7... I just want to change the background color and text color.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
CButton 除了调用 Windows 默认窗口过程来绘制按钮之外,什么也不做。您可以重写 OnPaint 代码来完成您自己的事情,但我可以理解为什么您想要避免这种情况 - 在每种不同的情况下获得正确的外观需要做很多工作。
MFC 提供了另一个类 CMFCButton,它有一个可重写的方法
OnFillBackground
,看看它是否适合您。CButton doesn't do anything more than call the Windows default window proc for drawing the button. You can override the OnPaint code to do your own thing, but I can understand why you would want to avoid that - it's a lot of work to get the proper look under every different circumstance.
MFC provides another class CMFCButton that has an overrideable method
OnFillBackground
, see if that works for you.我编写了一个 CButton,当主题在 Windows 中处于活动状态时(使用 Windows Classic 时情况并非如此),它将使用 Ownerdraw,并且会动态地执行此操作。此示例代码并不完整,但它演示了获得结果所需的一切。
困难的部分是您需要表示突出显示和按下的状态,请参阅
DrawCheckBox
的参数。我忽略它们,因为它们在我的申请中不会完全被忽略。我什至尝试过运行时主题切换,但是当从
Windows 7
主题切换到Windows Classic
时,这会产生不良效果(复选框看起来像常规按钮)。所以我没有使用这个,但分享也许很有趣:I have written a CButton that will use ownerdraw when theming is active in Windows (that is not the case when Windows Classic is used), and will do so dynamically. This sample code is not complete but it demonstrates everything needed to get the results.
The difficult part is that you need to represent highlighted and pressed states, see the parameters for
DrawCheckBox
. I am ignoring them as they will not be entirely missed in my application.I even tried runtime theme switching, however that gives undesired effect when switching from
Windows 7
theme toWindows Classic
(checkbox then looks like a regular button). So I am not using this but maybe it is interesting to share:根据这篇 Microsoft 文章:
如果您希望特定控件以不同的方式显示,我认为最好对其进行子类化。
本文将有助于理解子类化。
要更改按钮的背景和文本颜色,您需要从
CButton
派生一个新类,例如MyButton
并重写其DrawItem
函数以添加用于以您的方式绘制特定控件的代码。注意:您需要为这些控件设置所有者绘制属性 (
BS_OWNERDRAW
)。According to this Microsoft article:
If you want specific controls to appear differently, I think it is better to subclass it.
This article will be helpful to understand subclassing.
Here to change the background and text color of a button you will need to derive a new class from
CButton
sayMyButton
and override itsDrawItem
function to add the code for drawing that particular control in your way.Note: you will need to set owner draw property (
BS_OWNERDRAW
) to those controls.相信我,最好的解决方案是使用一个带有文本的 CStatic 和一个仅包含复选框的 CButton。至少可以说,从 Vista 开始,更改复选框的背景是一个问题。
Trust me, the best solution here is to have a CStatic with the text and a CButton containing only the check box. As of Vista, changing the background of checkboxes is a problem, to say the least.