问题描述
我想我在这里缺少一些小东西.
I think I am missing something small here.
我正在尝试创建一个从CStatic继承并具有透明背景的类.我已设法创建该类的实例,并在父CView
中显示它.但是,当我添加一个OnCtlColor
消息处理程序通过Visual Studio上的类视图以使背景透明时,它永远不会触发.
I am trying to make a class which inherits from CStatic with a transparent background. I have managed create an instance of the class and it is displayed in the parent CView
. However when I add a OnCtlColor
message handler going through the class view on Visual Studio to make the background transparent, it never fires.
这是一个代码段:
Foo.h
class Foo: public CStatic
{
DECLARE_DYNAMIC(Foo)
public:
Foo();
virtual ~Foo();
virtual void CreateCtrl(CWnd * Parent, POINT TopLeft, SIZE sz);
protected:
DECLARE_MESSAGE_MAP()
public:
afx_msg HBRUSH CtlColor(CDC* pDC, UINT nCtlColor);
afx_msg BOOL OnEraseBkgnd(CDC* pDC);
};
Foo.cpp
void Foo::CreateCtrl(CWnd * Parent, POINT TopLeft, SIZE sz)
{
CRect Rect(TopLeft, sz);
Create(pItem->Value->GetBuffer(), WS_CHILD | WS_VISIBLE | SS_CENTER | SS_NOTIFY, Rect, Parent);
ShowWindow(SW_SHOW);
}
BEGIN_MESSAGE_MAP(Foo, CStatic)
ON_WM_CTLCOLOR_REFLECT()
ON_WM_ERASEBKGND()
END_MESSAGE_MAP()
HBRUSH Foo::CtlColor(CDC* pDC, UINT nCtlColor)
{
pDC->SetBkMode(TRANSPARENT);
return (HBRUSH)GetStockObject(NULL_BRUSH);
}
BOOL Foo::OnEraseBkgnd(CDC* pDC)
{
return FALSE;
}
谁能建议我可能做错了什么?
Can anyone suggest what I might be doing wrong?
推荐答案
WM_CTLCOLOR
发送到父窗口,而不是静态控件.
WM_CTLCOLOR
is sent to the parent window, not to the static control.
要在静态控件类中捕获消息,您需要在消息映射中使用ON_WM_CTLCOLOR_REFLECT
,请参阅 MSDN文档并使用HBRUSH Foo::CtlColor(CDC* pDC, UINT nCtlColor)
.
To catch the message in the static control class, you need to use ON_WM_CTLCOLOR_REFLECT
in your message map, see MSDN Docs and use HBRUSH Foo::CtlColor(CDC* pDC, UINT nCtlColor)
.
这篇关于CStatic没有收到WM_CTLCOLOR的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!