在子类CStatic控件中处理WM_PAINT

Rag*_*ath 0 c++ mfc ownerdrawn wm-paint

我创建了一个自定义控件,其类具有CStatic基类.目前我使用WM_PAINT事件处理绘图.但是有一种奇怪的行为.当我使用CWnd::EnableWindow函数禁用它后重新启用窗口时,它拒绝绘制我在OnPaint函数中写的内容.它会绘制静态控件.

我同意有这种覆盖DrawItem和使用SS_OWNERDRAW样式的标准方法.但是怎么了WM_PAINT

void XXControl::OnPaint()
{
    CPaintDC PaintDC( this );
    // ** draw the control to PaintDC**
}
Run Code Online (Sandbox Code Playgroud)

Aja*_*jay 5

这正是我写的:

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));
}
Run Code Online (Sandbox Code Playgroud)

子类:

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


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

   my_static.SubclassDlgItem(IDC_DRAW, this);

   return true;
}
Run Code Online (Sandbox Code Playgroud)

IDC_DRAW此对话框的资源静态控制在哪里.我写了两个按钮处理程序:

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

void CMyDlg::OnBnClickedOk2()
{
    my_static.EnableWindow();
    my_static.Invalidate();
}
Run Code Online (Sandbox Code Playgroud)

它完美无瑕!删除Invalidate呼叫,它将失败.