在cwnd中绘制mfc组件

时间:2016-11-09 18:30:43

标签: mfc cwnd

我创建了一个cwnd类,它显示了一个内部有一个按钮的转换,但是我不想自己绘制一个按钮,而是希望委托给按钮组件。

原样......

class ExampleControl : public CWnd
{
    void ExampleControl::OnPaint()
    {
        CPaintDC dc(this);

        CRect rc(this);
        CDC memDC;
        memDC.CreateCompatibleDC(&dc);

        m_bmpCache.DeleteObject();
        m_bmpCache.CreateCompatibleBitmap(&dc, rc.Width(), rc.Height());

        OnDraw(&memDC);
    }

    void ExampleControl::OnDraw(CDC* pDC)
    {
        CRect rcClient(this);

        // draw background
        pDC->FillSolidRect(rcClient, GetSysColor(COLOR_WINDOW));

        // draw border
        COLORREF borderColor = RGB(0,0,255);
        pDC->Draw3dRect(0, 0, rcClient.Width(), rcClient.Height(), borderColor, borderColor);

        **//draw button
        //OK this draw a button ... but I would like to write 
        //CRect rect(10,10,25,15);
        //pDC->DrawFrameControl(rect, DFC_BUTTON, DFCS_BUTTONPUSH);**
    }

}

我想成为......

class ExampleControl : public CWnd
{
    //instantiate and call myButton.Create(...)
    CButton myButton;

    void ExampleControl::OnPaint()
    {
        CPaintDC dc(this);

        CRect rc(this);
        CDC memDC;
        memDC.CreateCompatibleDC(&dc);

        m_bmpCache.DeleteObject();
        m_bmpCache.CreateCompatibleBitmap(&dc, rc.Width(), rc.Height());

        OnDraw(&memDC);
    }

    void ExampleControl::OnDraw(CDC* pDC)
    {
        CRect rcClient(this);

        // draw background
        pDC->FillSolidRect(rcClient, GetSysColor(COLOR_WINDOW));

        // draw border
        COLORREF borderColor = RGB(0,0,255);
        pDC->Draw3dRect(0, 0, rcClient.Width(), rcClient.Height(), borderColor, borderColor);

        //draw button, using the mfc component
        //!!!! myButton.OnPaint() !!!!!!!

    }
}

拜托,我该怎么办?

Ps。:遗憾的是我无法使用Dialog类

1 个答案:

答案 0 :(得分:0)

您不想调用按钮绘制方法。

为WM_CREATE创建一个处理程序(ON_WM_CREATE(),OnCreate(LPCREATESTRUCT lpcs)...)

在OnCreate处理程序中,创建按钮...

BEGIN_MESSAGE_MAP(CExampleControl, CWnd) // in your .cpp implementation file
// ... other handlers
   ON_WM_CREATE()
END_MESSAGE_MAP()

int CExampleControl::OnCreate(LPCREATESTRUCT lpcs)
{
   __super::OnCreate(lpcs);
    myButton.Create(_T("My caption"), WS_CHILD|WS_VISIBLE, CRect(0, 0, 100, 100),  this, 101);
   return 0;
}

显然更改了按钮的标题,坐标和ID。

之后,你不需要做任何事情。该按钮将自己绘制为父窗口的子项。

相关问题