用MFC生成的文档应该怎样打印出来? 我用list视图创建了一个SDI程序,往里面添加了一些项后,想打印出来,可是按"文件"->"打印预览"后却发现是一片空白,请问怎样才能"所见即所得"呢? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 是的。网上有个ListCtrl的打印程序,可以找找。我也正好做过一个,需要可以提供。 int PrintListCtrl(CDC *pDC, CListCtrl *pPrintList, int nPage, CRect *prcPrint){ static CArray<int, int> aPageItem; if (!pDC) { aPageItem.RemoveAll(); aPageItem.Add(0); return 0; } CFont *pFont = pPrintList->GetFont(); LOGFONT lgFont; pFont->GetLogFont(&lgFont); //lstrcpy(lgFont.lfFaceName, _T("黑体")); lgFont.lfWeight = FW_BOLD; pFont = new CFont; pFont->CreateFontIndirect(&lgFont); CFont *pFontOld = pDC->SelectObject(pFont); CPoint ptOrg = pDC->GetWindowOrg(); CHeaderCtrl *pHeader = pPrintList->GetHeaderCtrl(); TEXTMETRIC /*tmCtl, */tmDC; //pPrintList->GetDC()->GetTextMetrics(&tmCtl); pDC->GetTextMetrics(&tmDC); int nWidth = 0, nMargin = tmDC.tmHeight, nHeight = tmDC.tmHeight + tmDC.tmExternalLeading + nMargin; //double dRatio = (double)tmDC.tmHeight / (double)tmCtl.tmHeight; HDITEM hiItem = {0}; TCHAR lpBuffer[256] = {0}; hiItem.mask = HDI_TEXT | HDI_WIDTH; hiItem.pszText = lpBuffer; hiItem.cchTextMax = 250; int nColumn = pHeader->GetItemCount(); CRect *prcItem = new CRect[nColumn]; CString *pstrItem = new CString[nColumn]; for (int i = 0; i < nColumn; i++) { pHeader->GetItem(i, &hiItem); prcItem[i].SetRect(nWidth, 0, nWidth + hiItem.cxy, tmDC.tmHeight); nWidth += hiItem.cxy; pstrItem[i] = hiItem.pszText; prcItem[i].bottom = prcItem[i].top + pDC->DrawText(pstrItem[i], prcItem[i], DT_CENTER | DT_WORDBREAK | DT_CALCRECT); prcItem[i].right = prcItem[i].left + hiItem.cxy; if (prcItem[i].Height() > nHeight) nHeight = prcItem[i].Height() + nMargin; } CSize szArea = pDC->GetWindowExt(); int nWidthArea = abs(szArea.cx); if (nWidth > nWidthArea) { double dRatioWidth = (double)szArea.cx / (double)nWidth; prcItem[0].right = prcItem[0].left + int(prcItem[0].Width() * dRatioWidth); nWidth = prcItem[0].Width(); for (int i = 1; i < nColumn; i++) { prcItem[i].right = prcItem[i - 1].right + int(prcItem[i].Width() * dRatioWidth); prcItem[i].left = prcItem[i - 1].right; nWidth += prcItem[i].Width(); } } pDC->SetWindowOrg((nWidth - nWidthArea) / 2, ptOrg.y); for (int iCol = 0; iCol < nColumn; iCol++) { CRect rcBorder(prcItem[iCol]); rcBorder.bottom = rcBorder.top + nHeight; pDC->Rectangle(rcBorder); prcItem[iCol].OffsetRect(0, (nHeight - prcItem[iCol].Height()) / 2); pDC->DrawText(pstrItem[iCol], prcItem[iCol], DT_CENTER | DT_WORDBREAK); } pDC->SelectObject(pFontOld); pFont->DeleteObject(); delete pFont; pDC->GetTextMetrics(&tmDC); nMargin = tmDC.tmHeight; nHeight = tmDC.tmHeight + tmDC.tmExternalLeading + nMargin; int nTop = nHeight; int iRow = 0; for (iRow = aPageItem[nPage - 1]; iRow < pPrintList->GetItemCount(); iRow++) { nHeight = tmDC.tmHeight + nMargin; for (int iCol = 0; iCol < nColumn; iCol++) { pstrItem[iCol] = pPrintList->GetItemText(iRow, iCol); prcItem[iCol].top = nTop; prcItem[iCol].bottom = nTop + tmDC.tmHeight; nWidth = prcItem[iCol].Width(); prcItem[iCol].bottom = prcItem[iCol].top + pDC->DrawText(pstrItem[iCol], prcItem[iCol], DT_CENTER | DT_WORDBREAK | DT_CALCRECT); prcItem[iCol].right = prcItem[iCol].left + nWidth; if (prcItem[iCol].Height() > nHeight) nHeight = prcItem[iCol].Height() + nMargin; } nTop += nHeight; nWidth = prcItem[iCol - 1].right; for (int iCol = 0; iCol < nColumn; iCol++) { CRect rcBorder(prcItem[iCol]); rcBorder.bottom = rcBorder.top + nHeight; pDC->Rectangle(rcBorder); prcItem[iCol].OffsetRect(0, (nHeight - prcItem[iCol].Height()) / 2); pDC->DrawText(pstrItem[iCol], prcItem[iCol], DT_CENTER | DT_WORDBREAK); } if (nTop + ptOrg.y - pDC->GetWindowOrg().y > szArea.cy - 160) break; } aPageItem.Add(iRow); delete []pstrItem; delete []prcItem; if (prcPrint) prcPrint->SetRect(ptOrg.x - pDC->GetWindowOrg().x, ptOrg.y - pDC->GetWindowOrg().y, nWidth + ptOrg.x - pDC->GetWindowOrg().x, nTop + ptOrg.y - pDC->GetWindowOrg().y); pDC->SetWindowOrg(ptOrg); return iRow;} 上面这段程序是不是要在classwizard里面添加的? 本人也是這個問題..建立了個SDI程序,..建工程時,已經加入了打印功能,我加入個畫圖事件,每次畫的圖都不一樣, 可是在我打印預覽時,是空白,---我又將畫圖的代碼加到OnDraw(CDC* pDC)中去,印預覽時,也是空白,...... PrintListCtrl是一个全局函数。当然也可以作为成员函数使用。 VC中控件组合框、编辑框中数据以文件形式保存调用 MFC中开机自启动顺序如何设置 mdc中实现把文件拖到对话框中 如何讲ACCESS里文本型的日期取出来放到日期控件里啊?? 请问如何向文件输出换行? 创建工程时选Use MFC in a static library 是不是生成的EXE就不需要VC的DLL了? 关于P2P之UDP穿透NAT的原理与实现源代码的测试问题 菜鸟关于MDI子窗口的问题 有谁教教我:在一个上下屏的分割窗口中,如何动态改变分割窗口大小,如何限制分割窗口最大和最小尺寸? 如何把一个EXE里的图标存到另一个EXE里? 可以通过回调函数传递值吗?如果可以怎么传递 tips
int PrintListCtrl(CDC *pDC, CListCtrl *pPrintList, int nPage, CRect *prcPrint)
{
static CArray<int, int> aPageItem;
if (!pDC)
{
aPageItem.RemoveAll();
aPageItem.Add(0);
return 0;
} CFont *pFont = pPrintList->GetFont();
LOGFONT lgFont;
pFont->GetLogFont(&lgFont);
//lstrcpy(lgFont.lfFaceName, _T("黑体"));
lgFont.lfWeight = FW_BOLD;
pFont = new CFont;
pFont->CreateFontIndirect(&lgFont); CFont *pFontOld = pDC->SelectObject(pFont);
CPoint ptOrg = pDC->GetWindowOrg(); CHeaderCtrl *pHeader = pPrintList->GetHeaderCtrl();
TEXTMETRIC /*tmCtl, */tmDC;
//pPrintList->GetDC()->GetTextMetrics(&tmCtl);
pDC->GetTextMetrics(&tmDC);
int nWidth = 0, nMargin = tmDC.tmHeight, nHeight = tmDC.tmHeight + tmDC.tmExternalLeading + nMargin;
//double dRatio = (double)tmDC.tmHeight / (double)tmCtl.tmHeight; HDITEM hiItem = {0};
TCHAR lpBuffer[256] = {0};
hiItem.mask = HDI_TEXT | HDI_WIDTH;
hiItem.pszText = lpBuffer;
hiItem.cchTextMax = 250;
int nColumn = pHeader->GetItemCount();
CRect *prcItem = new CRect[nColumn];
CString *pstrItem = new CString[nColumn];
for (int i = 0; i < nColumn; i++)
{
pHeader->GetItem(i, &hiItem);
prcItem[i].SetRect(nWidth, 0, nWidth + hiItem.cxy, tmDC.tmHeight);
nWidth += hiItem.cxy;
pstrItem[i] = hiItem.pszText;
prcItem[i].bottom = prcItem[i].top +
pDC->DrawText(pstrItem[i], prcItem[i], DT_CENTER | DT_WORDBREAK | DT_CALCRECT);
prcItem[i].right = prcItem[i].left + hiItem.cxy;
if (prcItem[i].Height() > nHeight)
nHeight = prcItem[i].Height() + nMargin;
}
CSize szArea = pDC->GetWindowExt();
int nWidthArea = abs(szArea.cx);
if (nWidth > nWidthArea)
{
double dRatioWidth = (double)szArea.cx / (double)nWidth;
prcItem[0].right = prcItem[0].left + int(prcItem[0].Width() * dRatioWidth);
nWidth = prcItem[0].Width();
for (int i = 1; i < nColumn; i++)
{
prcItem[i].right = prcItem[i - 1].right + int(prcItem[i].Width() * dRatioWidth);
prcItem[i].left = prcItem[i - 1].right;
nWidth += prcItem[i].Width();
}
}
pDC->SetWindowOrg((nWidth - nWidthArea) / 2, ptOrg.y);
for (int iCol = 0; iCol < nColumn; iCol++)
{
CRect rcBorder(prcItem[iCol]);
rcBorder.bottom = rcBorder.top + nHeight;
pDC->Rectangle(rcBorder);
prcItem[iCol].OffsetRect(0, (nHeight - prcItem[iCol].Height()) / 2);
pDC->DrawText(pstrItem[iCol], prcItem[iCol], DT_CENTER | DT_WORDBREAK);
}
pDC->SelectObject(pFontOld);
pFont->DeleteObject();
delete pFont; pDC->GetTextMetrics(&tmDC);
nMargin = tmDC.tmHeight;
nHeight = tmDC.tmHeight + tmDC.tmExternalLeading + nMargin; int nTop = nHeight;
int iRow = 0;
for (iRow = aPageItem[nPage - 1]; iRow < pPrintList->GetItemCount(); iRow++)
{
nHeight = tmDC.tmHeight + nMargin;
for (int iCol = 0; iCol < nColumn; iCol++)
{
pstrItem[iCol] = pPrintList->GetItemText(iRow, iCol);
prcItem[iCol].top = nTop;
prcItem[iCol].bottom = nTop + tmDC.tmHeight;
nWidth = prcItem[iCol].Width();
prcItem[iCol].bottom = prcItem[iCol].top +
pDC->DrawText(pstrItem[iCol], prcItem[iCol], DT_CENTER | DT_WORDBREAK | DT_CALCRECT);
prcItem[iCol].right = prcItem[iCol].left + nWidth;
if (prcItem[iCol].Height() > nHeight)
nHeight = prcItem[iCol].Height() + nMargin;
}
nTop += nHeight;
nWidth = prcItem[iCol - 1].right; for (int iCol = 0; iCol < nColumn; iCol++)
{
CRect rcBorder(prcItem[iCol]);
rcBorder.bottom = rcBorder.top + nHeight;
pDC->Rectangle(rcBorder);
prcItem[iCol].OffsetRect(0, (nHeight - prcItem[iCol].Height()) / 2);
pDC->DrawText(pstrItem[iCol], prcItem[iCol], DT_CENTER | DT_WORDBREAK);
}
if (nTop + ptOrg.y - pDC->GetWindowOrg().y > szArea.cy - 160)
break;
}
aPageItem.Add(iRow); delete []pstrItem;
delete []prcItem; if (prcPrint)
prcPrint->SetRect(ptOrg.x - pDC->GetWindowOrg().x, ptOrg.y - pDC->GetWindowOrg().y,
nWidth + ptOrg.x - pDC->GetWindowOrg().x, nTop + ptOrg.y - pDC->GetWindowOrg().y); pDC->SetWindowOrg(ptOrg); return iRow;
}
我加入個畫圖事件,每次畫的圖都不一樣, 可是在我打印預覽時,是空白,---我又將畫圖的代碼加到OnDraw(CDC* pDC)中去,印預覽時,也是空白,......