Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/142.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ MFC,内存上下文中的绘图文本(打印)_C++_Mfc_Drawtext - Fatal编程技术网

C++ MFC,内存上下文中的绘图文本(打印)

C++ MFC,内存上下文中的绘图文本(打印),c++,mfc,drawtext,C++,Mfc,Drawtext,我遇到了一个问题-我需要在内存中创建一个位图,在其中绘制一些文本,并将其保存为BMP文件,然后用物理打印机打印出位图。我可以在对话框窗口上下文中绘制此图形-效果很好。但当我尝试在打印机上下文中绘制相同的图形时,文本不会出现。我真的不明白为什么会这样。请帮帮我,伙计们。提前谢谢。代码如下: void CMy2Dlg::OnButton1() { // TODO: Add your control notification handler code here CPrintDialo

我遇到了一个问题-我需要在内存中创建一个位图,在其中绘制一些文本,并将其保存为BMP文件,然后用物理打印机打印出位图。我可以在对话框窗口上下文中绘制此图形-效果很好。但当我尝试在打印机上下文中绘制相同的图形时,文本不会出现。我真的不明白为什么会这样。请帮帮我,伙计们。提前谢谢。代码如下:

void CMy2Dlg::OnButton1() 
{
    // TODO: Add your control notification handler code here
    CPrintDialog pd(false); 

    if (pd.DoModal()==IDOK) 
    {
        CDC PrintDC;    
        HDC hdc = pd.CreatePrinterDC();
        PrintDC.Attach(hdc);    
        DOCINFO infStru;                    
        ::ZeroMemory (&infStru, sizeof (DOCINFO));
        CString title="Print test";
        infStru.cbSize = sizeof (DOCINFO);      
        infStru.lpszDocName=title;
        infStru.lpszOutput=NULL;


        PrintDC.StartDoc(&infStru);     

        PrintDC.StartPage();

        {           
            CRect r, r2;
            CBitmap memBMP, * pOldBitmap;
            CPaintDC dc(this); 
            CDC memDC, *pDC = &memDC;
            CFont font, * pOldFont;
            int width = 2000;
            int height = 1500;
            int textwidth = 300;
            int textheight = 150;
            int oldMapMode = 0;
            int oldbkmode = 0;
            int i, j;

            LOGFONT logFont, lf;
            COLORREF oldTextColor;

            memset(&logFont, 0, sizeof(logFont));
            logFont.lfHeight = 16;
            logFont.lfWidth = 0; 
            logFont.lfEscapement = 0;
            logFont.lfOrientation = 0;
            logFont.lfWeight = FW_NORMAL; 
            logFont.lfItalic = FALSE; 
            logFont.lfUnderline = FALSE; 
            logFont.lfStrikeOut = 0; 
            logFont.lfCharSet = ANSI_CHARSET; 
            logFont.lfOutPrecision = OUT_DEFAULT_PRECIS; 
            logFont.lfClipPrecision = CLIP_DEFAULT_PRECIS; 
            logFont.lfQuality = DEFAULT_QUALITY; 
            logFont.lfPitchAndFamily = DEFAULT_PITCH | FF_SWISS; 
            strcpy(logFont.lfFaceName, "Arial");

            if(memDC.CreateCompatibleDC(&PrintDC)) {
                if (memBMP.CreateCompatibleBitmap(&PrintDC, width, height)) {

                    pOldBitmap = pDC->SelectObject(&memBMP);
                    pDC->FillSolidRect(0, 0, width, height, RGB(200, 200, 200));

                    oldTextColor = pDC->SetTextColor(RGB(255,0,0));
                    oldMapMode = pDC->SetMapMode(MM_LOMETRIC);
                    oldbkmode = pDC->SetBkMode(TRANSPARENT);


                    lf = logFont;
                    lf.lfHeight = -MulDiv(lf.lfHeight, GetDeviceCaps(pDC->GetSafeHdc(), LOGPIXELSY), 72);
                    //lf.lfHeight = 100;

                    font.CreateFontIndirect(&lf);
                    pOldFont = pDC->SelectObject(&font);


                    r.left = 10;
                    r.top = 10;
                    r.right = r.left + textwidth;
                    r.bottom = r.top + textheight;

                    r.top *= -1;
                    r.bottom *= -1;

                    pDC->MoveTo(r.left,  r.top);
                    pDC->LineTo(r.right, r.top);
                    pDC->LineTo(r.right, r.bottom);
                    pDC->LineTo(r.left,  r.bottom);
                    pDC->LineTo(r.left,  r.top);

                    pDC->DrawText("qwerty", &r, DT_CENTER | DT_SINGLELINE | DT_VCENTER);                    

                    pDC->SetMapMode(oldMapMode);
                    pDC->SetTextColor(oldTextColor);
                    pDC->SetBkMode(oldbkmode);

                    PrintDC.BitBlt(10, 10, width, height, pDC, 0, 0, SRCCOPY);                  

                    pDC->SelectObject(pOldBitmap);
                    pDC->SelectObject(pOldFont);                    

                    font.DeleteObject();
                    memBMP.DeleteObject();
                    pDC->DeleteDC();
                }
            }
        }

        PrintDC.EndPage();

        PrintDC.EndDoc();

        PrintDC.Detach();       
        DeleteDC(hdc);
    }
}
如果在内存DC上使用
SetMapMode(MM_LOMETRIC)
,则在使用
BitBlt
复制到打印机/显示DC时,内存DC必须颠倒绘制。宽度/高度也必须调整。只需使用默认的映射模式(
MM\u TEXT
)。在打印机DC上绘图时,如果需要特定的测量单位,请使用
SetMapMode(MM_LOMETRIC)

void CMy2Dlg::OnButton1() 
{
    //create the bitmap
    int w = 600, h = 400;
    CClientDC dc(this);
    CBitmap bmp;
    CDC memdc;
    memdc.CreateCompatibleDC(&dc);
    bmp.CreateCompatibleBitmap(&dc, w, h);
    auto oldbmp = memdc.SelectObject(bmp);

    //draw on bitmap
    memdc.FillSolidRect(0, 0, w, h, RGB(200, 200, 200));
    memdc.SetTextColor(RGB(255, 0, 0));
    CRect rc(0, 0, w, h);
    memdc.DrawText(L"qwerty", &rc, 0);

    dc.BitBlt(0, 0, w, h, &memdc, 0, 0, SRCCOPY);//optional: draw the bitmap on dialog

    CPrintDialog pd(false);
    if(pd.DoModal() == IDOK)
    {
        CDC PrintDC;
        HDC hdc = pd.GetPrinterDC();
        PrintDC.Attach(hdc);
        DOCINFO docinfo = { sizeof(docinfo) };
        docinfo.lpszDocName = L"Print test";
        PrintDC.StartDoc(&docinfo);
        PrintDC.StartPage();
        PrintDC.BitBlt(0, 0, w, h, &memdc, 0, 0, SRCCOPY);
        PrintDC.EndPage();
        PrintDC.EndDoc();
    }
    dc.SelectObject(oldbmp);
}

矩形出现了吗?(顺便说一句,还有其他函数可以绘制矩形而不是绘制直线,但这是另一回事)你想用
r.top*=-1做什么;r、 底部*=-1?不要把
CPaintDC
放在那里。你的代码到处都是。创建一个单独的函数
void paint(CDC*dc)
,该函数可以独立于打印对话框和位图进行绘制,然后将其打印到位图,然后单独打印位图。是的,它确实会出现。我需要负的垂直坐标值,因为使用的是MM_LOMETRIC贴图模式(-正x在右边;正y在上面)我知道代码很乱,但现在它没有打扰我,它只是一个快速而肮脏的实验。但感谢您的评论更改地图模式通常是在打印机dc上绘图时完成的。但是您在内存DC上使用的是
SetMapMode(MM_LOMETRIC)
,这将使
BitBlt
内存DC打开以显示/打印DC变得更加复杂。非常感谢!看来我需要的是用CClientDC绘图!