gpt4 book ai didi

c++ - MFC,在内存上下文中绘制文本(打印)

转载 作者:搜寻专家 更新时间:2023-10-31 02:04:25 24 4
gpt4 key购买 nike

我遇到了一个问题 - 我需要在内存中创建一个位图,在其中绘制一些文本并将其保存为 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);
}
}

最佳答案

如果 SetMapMode(MM_LOMETRIC) 用于内存 DC,则在使用 BitBlt 复制到打印机/显示器 DC 时,内存 DC 必须倒置绘制。宽度/高度也必须调整。只需使用默认 map 模式 (MM_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);
}

关于c++ - MFC,在内存上下文中绘制文本(打印),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53542341/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com