我做了一个控件,其中绘制一幅位图。用的是有窗口的控件。  放入ActiveX Control Test Container测试时能正确绘制。把控件插入word时则出错!CreateCompatibleDC就失败了。很奇怪! 请各位高手指教!  主要代码如下:
加入了一个位图资源IDB_BITMAP1 : void CMfcBitmapCtlCtrl::OnDraw(
CDC* pDC, const CRect& rcBounds, const CRect& rcInvalid)
{
CBitmap bitmap;
bitmap.LoadBitmap(IDB_BITMAP1);
    
BITMAP bm;
    bitmap.GetBitmap (&bm);
    CPoint size (bm.bmWidth, bm.bmHeight);
    pDC->DPtoLP (&size);    CPoint org (0, 0);
    pDC->DPtoLP (&org);    CDC dcMem;
int status = dcMem.CreateCompatibleDC (pDC);
if (status == 0) {
AfxMessageBox("CreateCompatibleDC failed!");
return;
}
if (dcMem.m_hDC == NULL) {
AfxMessageBox("hDC is NULL!");
return;
}
    CBitmap* pOldBitmap = (CBitmap*)dcMem.SelectObject (bitmap);
    dcMem.SetMapMode (pDC->GetMapMode ());    pDC->BitBlt (0, 0, size.x, size.y, &dcMem, org.x, org.y, SRCCOPY);
//pDC->BitBlt (x, y, size.x, size.y, &dcMem, org.x, org.y, SRCAND);
//pDC->TransparentBlt (x, y, size.x, size.y, &dcMem, org.x, org.y, size.x, size.y, RGB(255, 255, 255));//没有这个函数!

    dcMem.SelectObject (pOldBitmap);
if (dcMem.m_hDC == NULL)
AfxMessageBox("after hDC is NULL!");
}

解决方案 »

  1.   

    我在word中,从工具-》宏,VB编辑器,在normal中新建一个模块,其中用以下代码把控件插入到word文档:Sub test()
        Dim ocxCtrl As Object
        Set ocxCtrl = ActiveDocument.Shapes.AddOLEControl("MfcBitmapCtl.MfcBitmapCtlCtrl.1", , , , , Selection.Range)
        ocxCtrl.WrapFormat.Type = wdWrapNone
        
        Debug.Print ActiveDocument.Name
    End Sub
      

  2.   

    我试过用无窗口控件,加入以下函数,还是不行DWORD CMfcBitmapCtlCtrl::GetControlFlags() 
    {
    return COleControl::GetControlFlags() | windowlessActivate ;
    }很郁闷,请大家救我!如果不检测返回值也不弹对话框,则Debug版本会assert失败,而release版本好像是正常的,至少能画出图,但这肯定隐藏着问题。实际上当我想绘制透明控件时,放入word中就没效果。实际上也是CreateCompatibleDC就失败了!
      

  3.   

    那是你的design mode 和 user mode的问题.
    在design mode 中CreateCompatibleDC失败
      

  4.   

    design mode和user mode确实有影响,但都还没有成功。
    design mode时CreateCompatibleDC失败,而user mode时word直接崩溃了!该怎么改呢?能说的具体些吗?我的程序哪里有问题呢?我用的插入到word的代码如下(先是design mode,后来切换到user mode):
    Sub test()
        If ActiveDocument.FormsDesign = False Then
            ActiveDocument.ToggleFormsDesign    '切换到FormDesign
            CommandBars("Control Toolbox").Visible = False
        End If    Dim ocxCtrl As Object
        Set ocxCtrl = ActiveDocument.Shapes.AddOLEControl("MfcBitmapCtl.MfcBitmapCtlCtrl.1", , , , , Selection.Range)
        '这时是design mode
        ocxCtrl.Visible = msoFalse
        ocxCtrl.ZOrder msoSendBehindText
        ocxCtrl.WrapFormat.Type = wdWrapNone
        
        If ActiveDocument.FormsDesign = True Then
            ActiveDocument.ToggleFormsDesign
        End If
        CommandBars("Control Toolbox").Visible = False
        ocxCtrl.Visible = msoTrue
        '这时是user mode
     End Sub