CClientDC和DC没有在ChtmlEditCtrl上绘图

时间:2016-03-01 11:50:16

标签: c++ mfc gdi cdc

大家好我正在使用MFC中的CHtmlEditCtrl。我想在处理右键单击事件的函数中绘制一些随机矩形和线条。

使用此片段从静态创建ChtmlEditCtrl控件:

bool CHtmlEditCtrlEx::CreateFromStatic( UINT nID, CWnd* pParent ) {
    CStatic wndStatic;
    if ( !wndStatic.SubclassDlgItem(nID, pParent)) {
        return false;
    }
    CRect rc;
    wndStatic.GetWindowRect( &rc );
    pParent->ScreenToClient( &rc );
    if (Create( 0, (WS_CHILD | WS_VISIBLE), rc, pParent, nID, 0 )) {
        ...
}

然后我重写CWnd :: pretranslate()函数,因为:

  CClientDC dcc(this);
    switch (pMsg->message) {

        case WM_RBUTTONUP:  // Right-click
            // Just some dummy values
            DrawSquigly(dcc, 600, 240, 20);
            break;

    }

DrawSquigly()函数定义如下:

void CHtmlEditCtrlEx::DrawSquigly(CDC &dcc, int iLeftX, int iWidth, int iY)
{
    CAMTrace trace;
    trace.Trace("Drawing Squiggly");
    //dcc.TextOut(10, 10, CString(_T("I used a client DC!")));

    CPen * oldPen;
    CBrush * oldBrush;
    oldPen = (CPen *) dc.SelectStockObject(WHITE_PEN);
    dcc.MoveTo(5,10);
    dcc.LineTo(80, 10);
    dcc.SelectObject(oldPen);

    //GDI 002_2: Create custom pen with different Line thickness.
    CPen thick_pen(PS_SOLID, 3, RGB(0,255,0));
    oldPen = dc.SelectObject(&thick_pen);
    dcc.MoveTo(5, 20);
    dcc.LineTo(80,20);
    dcc.SelectObject(oldPen);

    //GDI 002_3: Create a Rectangle now
    dcc.Draw3dRect(5,30,80,70, RGB(25,25,255), RGB(120,120,120));

    //GDI 002_4: Create a Brush that we can use for filling the 
    // closed surfaces
    CBrush brush(RGB(255,0,255));
    oldBrush = dc.SelectObject(&brush);
    dcc.Rectangle(5,110,80,140);
    dcc.SelectObject(oldBrush);

    //GDI 002_5: Hatch Brush is useful to apply a pattern in stead 
    //of solid fill color
    CBrush* hatBrush = new CBrush();
    hatBrush->CreateHatchBrush(HS_CROSS, RGB(255,0,255));
    oldBrush = dc.SelectObject(hatBrush);
    dcc.FillRect(new CRect(5,160,80,190), hatBrush);
    dcc.SelectObject(oldBrush);
}

但右键单击时没有画图。我想我错过了一些东西,特别是因为我是MFC的新手。

我已经在事件处理程序的顶部添加了一个跟踪,以确保该函数被调用,并且它是。

任何人都可以指出正确的方向吗?

1 个答案:

答案 0 :(得分:0)

您的代码中实际上有2个设备上下文:一个在调用中作为参数传递(我们不知道它来自哪里),另一个在绘图函数中本地创建。

通常情况下,当系统给你一个DC时,它希望你画一些东西,而不是你画的东西。

如果您正在处理的窗口是分层的,系统会为您提供一个内存上下文,即在清除时 - 通过一些窗口管理器效果对窗口本身进行快速操作。

我的怀疑是 - 通过分配第二个dc-当你从消息处理程序返回时,第一个(你留空)清除了你的绘图。

相关问题