简体   繁体   English

在子类CStatic控件中处理WM_PAINT

[英]Handling WM_PAINT in a Subclassed CStatic Control

I created a custom control whose class has CStatic as base class. 我创建了一个自定义控件,其类具有CStatic作为基类。 Currently I handle the drawing using WM_PAINT event. 目前,我使用WM_PAINT事件处理图形。 But there is a strange behavior. 但是有一个奇怪的行为。 When I re-enable the window after disabling it using CWnd::EnableWindow function, it refuses to draw what I written in OnPaint function. 当我使用CWnd::EnableWindow函数禁用窗口后重新启用窗口时,它拒绝绘制我在OnPaint函数中编写的内容。 It draws the static control instead. 它绘制静态控件。

I agree that there is this standard method of overriding DrawItem and using SS_OWNERDRAW style. 我同意存在这种覆盖DrawItem并使用SS_OWNERDRAW样式的标准方法。 But what's wrong with WM_PAINT ? 但是WM_PAINT什么问题?

void XXControl::OnPaint()
{
    CPaintDC PaintDC( this );
    // ** draw the control to PaintDC**
}

Here is exactly what I have written: 这正是我写的:

class CMyStatic : public CStatic
{
    DECLARE_MESSAGE_MAP()
public:
    void OnPaint(void);
};

BEGIN_MESSAGE_MAP(CMyStatic, CStatic)
    ON_WM_PAINT()
END_MESSAGE_MAP()

void CMyStatic::OnPaint(void)
{
    CPaintDC dc(this);
    CRect rect;
    GetClientRect(&rect);

    dc.FillSolidRect(&rect, RGB(120,255,0));
}

And subclassed: 并细分为:

class CMyDlg : public CDialog
{
// Construction
    CMyStatic my_static;
...
};


BOOL CCMyDlg::OnInitDialog()
{
   CDialog::OnInitDialog();

   my_static.SubclassDlgItem(IDC_DRAW, this);

   return true;
}

Where IDC_DRAW is static control on resource for this dialog. 其中IDC_DRAW是此对话框的资源静态控件。 I wrote two button handlers: 我写了两个按钮处理程序:

void CMyDlg::OnBnClickedOk()
{
    my_static.EnableWindow(FALSE);
    my_static.Invalidate();
}

void CMyDlg::OnBnClickedOk2()
{
    my_static.EnableWindow();
    my_static.Invalidate();
}

And it works flawlessly! 而且它可以完美地工作! Remove Invalidate call and it would fail. 删除Invalidate呼叫,它将失败。

Try turning off Aero. 尝试关闭Aero。 I'm having a similiar problem where I'm drawing a static control and when it goes from disabled to enabled the WM_PAINT message is never received, but if I turn Aero off it works fine. 我遇到了一个类似的问题:正在绘制一个静态控件,当它从禁用状态变为启用状态时,永远不会收到WM_PAINT消息,但是如果我关闭Aero,它就可以正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM