优化校准背景为灰色,我希望它为白色 [英] Picture Control background is gray and I want it white
问题描述
我的应用程序有一个图片控件,其背景是灰色,我希望它是白色.我已经尝试了几件事,例如:派生一个类并覆盖OnPaint
方法;响应WM_CTLCOLOR
消息(添加ON_WM_CTLCOLOR()
并对其进行处理),但没有成功.
My application has a picture control, whose background is gray and I want it to be white. I already tried several things, like: derive a class and override OnPaint
method; respond to WM_CTLCOLOR
message (adding ON_WM_CTLCOLOR()
and processing it), but no success.
邮件地图上的条目:
ON_WM_CTLCOLOR()
实施:
BOOL CMyDialog::OnInitDialog()
{
__super::OnInitDialog();
white.CreateSolidBrush(RGB(255,255,255));
//...
return TRUE; // return TRUE unless you set the focus to a control
}
HBRUSH CMyDialog::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
if (pWnd == GetDlgItem(IDC_PICTURE))
{
return white;
}
return __super::OnCtlColor(pDC, pWnd, nCtlColor);
}
头文件类定义:
CBrush white;
HBRUSH OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor);
推荐答案
在使用Spy ++进行一些调试之后,我意识到优化校准是静态控件,必须处理的消息不是WM_CTLCOLOR
而是WM_CTLCOLORSTATIC
After some debugging using Spy++, I've realized the Picture Control is a Static Control and the message I have to process is not WM_CTLCOLOR
but WM_CTLCOLORSTATIC
因此,在包含它的对话框中,我将消息映射从更改为
So, on the dialog that contains it I changed the message map from
ON_WM_CTLCOLOR()
到
ON_MESSAGE(WM_CTLCOLORSTATIC, OnCtlColorStatic)
在实现中,将方法从更改为
On the implementation, change the method from
HBRUSH CMyDialog::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
if (pWnd == GetDlgItem(IDC_PICTURE))
{
return white;
}
return __super::OnCtlColor(pDC, pWnd, nCtlColor);
}
到
LRESULT CMyDialog::OnCtlColorStatic(WPARAM wParam, LPARAM lParam)
{
HWND hWnd = (HWND)lParam;
if (::GetDlgCtrlID(hWnd) == IDC_PICTURE)
return (LRESULT)white.GetSafeHandle();
return DefWindowProc(WM_CTLCOLORSTATIC, wParam, lParam);
}
然后在头文件中,也将方法从更改为
And in the header file, change also the method from
HBRUSH OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor);
到
LRESULT OnCtlColorStatic(WPARAM wParam, LPARAM lParam);
请注意一个细微的区别:旧的通过直接返回 HBRUSH 刷柄而起作用;新的期望一个函数返回一个 LRESULT 类型的变量,因此强制转换.
Notice a subtle difference: the old one worked by returning the HBRUSH brush handle directly; the new one expects a function returning a LRESULT typed variable, so the cast.
结果就是我所期望的:
UPDATE :最近,我需要做一件事,我必须设置静态"的文本颜色",并且发现我也可以获取其设备上下文:>
HDC hDC= (HDC)wParam;
HWND hWnd = (HWND)lParam;
因此,在我可以通过以下方式轻松完成此操作之后:
So, after I could easily do it by:
SetBkMode(hDC, TRANSPARENT);
SetTextColor(hDC, RGB(0,127,127));
这篇关于优化校准背景为灰色,我希望它为白色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!