如果希望将一个edit框中的多行文本用打印机打印出来应该怎么实现?

princesshan 2006-01-16 04:59:27
也可能不是一个edit框或者就是一段文字,怎么实现那?
是一个基于对话框的程序。
...全文
158 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
Seu_why 2006-01-17
  • 打赏
  • 举报
回复
测试可以打印了
打印结果: This is a Test
This is a Test 和EDIT上面显示的一样,准备添加页眉,页脚了 ^_^
具体你在自己调~
good luck !
Seu_why 2006-01-17
  • 打赏
  • 举报
回复
void CPrintTestDlg::OnBtnPrint()
{
// TODO: Add your control notification handler code here
CString str;
char *buf;

GetDlgItemText(IDC_EDIT1,str);
buf=(LPSTR)(LPCTSTR)str;

d_PrintString(buf,20,CFGPRINTER);
}

BOOL CPrintTestDlg::d_PrintString(char *p, DWORD pSize, bool bCfgMode)
{
CDC dc;
CDC tempDC;
CPrintDialog printDlg(FALSE);
CRect r;
int nHeight;

if(bCfgMode == CFGPRINTER)
{
if(printDlg.DoModal() == IDCANCEL)
return false;
}
else
{
//get the default printer info
if(!printDlg.GetDefaults())
return false;
}

// Attach a printer DC
dc.Attach(printDlg.GetPrinterDC());
dc.m_bPrinting = TRUE;

// use Text mapping mode
dc.SetMapMode(MM_TEXT);

// Setup font specifics
LOGFONT LogFont;

CFont aFont, *oldFont;

LogFont.lfHeight = -MulDiv(10, GetDeviceCaps(dc,LOGPIXELSY), 72);
LogFont.lfWeight = 0;
LogFont.lfEscapement = 0;
LogFont.lfOrientation = 0;
LogFont.lfWeight = 0;
LogFont.lfItalic = false;
LogFont.lfUnderline = 0;
LogFont.lfStrikeOut = 0;
LogFont.lfCharSet = ANSI_CHARSET;
LogFont.lfOutPrecision = OUT_TT_PRECIS;
LogFont.lfClipPrecision = CLIP_DEFAULT_PRECIS;
LogFont.lfQuality = DEFAULT_QUALITY;
LogFont.lfPitchAndFamily = DEFAULT_PITCH | FF_SWISS;
lstrcpy (LogFont.lfFaceName, "MS Sans Serif");
dc.SetBkMode(OPAQUE);

aFont.CreateFontIndirect( &LogFont );
// use the font
oldFont = dc.SelectObject( &aFont );

// get the application title
CString strTitle;
strTitle.LoadString(AFX_IDS_APP_TITLE);

// Initialise print document details

DOCINFO di;
DOCINFO *pDi = &di; // pointer to a DOCINFO object
::ZeroMemory (pDi, sizeof (DOCINFO));
di.cbSize = sizeof (DOCINFO);
// application title appears in the spooler view
di.lpszDocName = strTitle;

// create a memory DC that is compatible with the printer DC
if(!tempDC.CreateCompatibleDC(& dc))
{
cout<<"failed in creating memory dc"<< endl;
// restore font
dc.SelectObject(oldFont);
//free font memory
aFont.DeleteObject();

//detach the printer DC
dc.Detach();

return false;
}

// Get the printing extents and store in the m_rectDraw field of a
// CPrintInfo object
CPrintInfo Info;
int w = dc.GetDeviceCaps(HORZRES);
int h = dc.GetDeviceCaps(VERTRES);
Info.m_rectDraw.SetRect(0,0, w, h);

char *startAt = p;
int totalDone = 0;
// int lengthToGo = pSize;
tempDC.SelectObject( &aFont );

if((bool)tempDC.StartDoc( &di ))
{
// start a page for mem Dc
tempDC.StartPage();

r = Info.m_rectDraw;

// draw the string to the page
nHeight = tempDC.DrawText(startAt,pSize,r,DT_WORDBREAK|DT_NOCLIP|DT_EXPANDTABS);

// start a job for printing
bool bPrintingOK = dc.StartDoc( &di );
int lengthToGo = 0;
int nBlockOfPage = 0;
int nLineHeight = 0;
for (UINT page = Info.GetMinPage();
bPrintingOK && lengthToGo <= nHeight ; page ++)
{
dc.StartPage();
Info.m_nCurPage = page;

//calc the height of line
nLineHeight = dc.DrawText(startAt,1,r,
DT_CALCRECT|DT_WORDBREAK|DT_NOCLIP|DT_EXPANDTABS);

r.right = Info.m_rectDraw.right;

nBlockOfPage = nLineHeight*75;

dc.SetViewportOrg(0,-lengthToGo);
dc.SetViewportExt(w,h);

dc.DrawText(startAt,r,DT_WORDBREAK|DT_NOCLIP|DT_EXPANDTABS);

//goto next page
lengthToGo += nBlockOfPage;

// end page
bPrintingOK = (dc.EndPage() > 0);
}

tempDC.EndPage();

if(bPrintingOK)
{
// end job
dc.EndDoc();
tempDC.EndDoc();
}
else
{
// abort job
dc.AbortDoc();
tempDC.AbortDoc();
}
}

// restore font
dc.SelectObject(oldFont);
tempDC.SelectObject(oldFont);

// free font memory
aFont.DeleteObject();

// detach the printer DC
dc.Detach();
tempDC.DeleteDC();


return true;

}
菜牛 2006-01-16
  • 打赏
  • 举报
回复

Windows GDI
Printing a Document

After an application initializes the necessary variables, registers its AbortProc function, and displays its modeless Cancel dialog box, it can start the print job by calling the StartDoc function.

After the application begins a print job, it can define individual pages in the document by calling the StartPage and EndPage functions and embedding the appropriate calls to GDI drawing functions within this bracket. After the application has defined the last page, it can close the document and end the print job by calling the EndDoc function.

The following example shows the code required to print a string of text and a bitmapped image. The string of text, centered at the top of the page, identifies the path and file name for the file that contains the bitmapped image. The bitmapped image, centered vertically and horizontally on the page, is drawn so that the same proportions used to draw the image in the application's window are maintained. Note that when targeting a printer DC, use a DIB instead of a DDB -- that is why StretchDIBits is used in this code.

// Zero and then initialize the members of a DOCINFO structure.

memset( &di, 0, sizeof(DOCINFO) );
di.cbSize = sizeof(DOCINFO);
di.lpszDocName = "Bitmap Printing Test";
di.lpszOutput = (LPTSTR) NULL;
di.lpszDataType = (LPTSTR) NULL;
di.fwType = 0;

// Begin a print job by calling the StartDoc function.

nError = StartDoc(pd.hDC, &di);
if (nError == SP_ERROR)
{
errhandler("StartDoc", hwnd);
goto Error;
}

// Inform the driver that the application is about to begin
// sending data.

nError = StartPage(pd.hDC);
if (nError <= 0)
{
errhandler("StartPage", hwnd);
goto Error;
}

// Retrieve the number of pixels-per-logical-inch in the
// horizontal and vertical directions for the display upon which
// the bitmap was created. These are likely the same as for
// the present display, so we use those values here.

hWinDC = GetDC(hWnd);
fLogPelsX1 = (float) GetDeviceCaps(hWinDC, LOGPIXELSX);
fLogPelsY1 = (float) GetDeviceCaps(hWindDC, LOGPIXELSY);

// Retrieve the number of pixels-per-logical-inch in the
// horizontal and vertical directions for the printer upon which
// the bitmap will be printed.

fLogPelsX2 = (float) GetDeviceCaps(pd.hDC, LOGPIXELSX);
fLogPelsY2 = (float) GetDeviceCaps(pd.hDC, LOGPIXELSY);

// Determine the scaling factors required to print the bitmap and
// retain its original proportions.

if (fLogPelsX1 > fLogPelsX2)
fScaleX = (fLogPelsX1 / fLogPelsX2);
else fScaleX = (fLogPelsX2 / fLogPelsX1);

if (fLogPelsY1 > fLogPelsY2)
fScaleY = (fLogPelsY1 / fLogPelsY2);
else fScaleY = (fLogPelsY2 / fLogPelsY1);

// Compute the coordinates of the upper left corner of the
// centered bitmap.

cWidthPels = GetDeviceCaps(pd.hDC, HORZRES);
xLeft = ((cWidthPels / 2) - ((int) (((float) bmih.biWidth)
* fScaleX)) / 2);
cHeightPels = GetDeviceCaps(pd.hDC, VERTRES);
yTop = ((cHeightPels / 2) - ((int) (((float) bmih.biHeight)
* fScaleY)) / 2);

// Use StretchDIBits to scale the bitmap and maintain
// its original proportions (that is, if the bitmap was square
// when it appeared in the application's client area, it should
// also appear square on the page).

if (StretchDIBits(pd.hDC, xLeft, yTop, (int) ((float) bmih.biWidth
* fScaleX), (int) ((float) bmih.biHeight * fScaleY), 0, 0,
bmih.biWidth, bmih.biHeight, lpBits, lpBitsInfo, iUsage,
SRCCOPY) == GDI_ERROR)
{
errhandler("StretchDIBits Failed", hwnd);
}


// Retrieve the width of the string that specifies the full path
// and filename for the file that contains the bitmap.

GetTextExtentPoint32(pd.hDC, ofn.lpstrFile,
ofn.nFileExtension + 3, &szMetric);

// Compute the starting point for the text-output operation. The
// string will be centered horizontally and positioned three lines
// down from the top of the page.

xLeft = ((cWidthPels / 2) - (szMetric.cx / 2));
yTop = (szMetric.cy * 3);

// Print the path and filename for the bitmap, centered at the top
// of the page.

TextOut(pd.hDC, xLeft, yTop, ofn.lpstrFile,
ofn.nFileExtension + 3);

// Determine whether the user has pressed the Cancel button in the
// AbortPrintJob dialog box; if the button has been pressed, call
// the AbortDoc function. Otherwise, inform the spooler that the
// page is complete.

nError = EndPage(pd.hDC);

if (nError <= 0)
{
errhandler("EndPage", hwnd);
goto Error;
}

// Inform the driver that document has ended.

nError = EndDoc(pd.hDC);
if (nError <= 0)
errhandler("EndDoc", hwnd);

Error:
// Enable the application's window.

EnableWindow(hwnd, TRUE);

// Remove the AbortPrintJob dialog box.

DestroyWindow(hdlgCancel);

// Delete the printer DC.

DeleteDC(pd.hDC);
Because the pixels on a screen typically have different dimensions than the dots on a printer, it is necessary to scale bitmapped images to obtain a WYSIWYG effect. This is done by obtaining horizontal and vertical scaling factors and then applying those factors to the width and height values passed to the StretchDIBits function. In the sample application, the scaling factors were obtained by retrieving the horizontal and vertical logical-pixel count for the two devices. Once the scaling factors were obtained, they were used to adjust the bitmap width and height.

To center the bitmap on the page, the application first computed the width and height of the scaled bitmap. (The bitmap was scaled to maintain the original proportions of the image.) These values were divided by two and then subtracted from half of the width and height of the page. The result defines the coordinates of the upper-left corner of the bitmap.

To center the text at the top of the page, the application called the GetTextExtentPoint32 function to retrieve the width and height of the string specifying the path names and file names. Once these values were obtained, the application used the height to position the string three lines down the page and the width to position the string horizontally centered on the page.

The following illustration shows a representation of the page that appeared when the application printed the bitmapped image in the Winlogo.bmp file. This illustration also depicts the variables used to position the text and to position and scale the bitmap.





--------------------------------------------------------------------------------

© 2002 Microsoft Corporation. All rights reserved.
princesshan 2006-01-16
  • 打赏
  • 举报
回复
char szP[256] = "12345678901234567890123456789012345678901234567890";
DWORD pSize = strlen(szP);
PrintString(szP, pSize);
好像不行,上面是我的几行代码,没有出我想象的结果
lixiaosan 2006-01-16
  • 打赏
  • 举报
回复
http://www.codeguru.com/cpp/w-p/printing/article.php/c2957/
Seu_why 2006-01-16
  • 打赏
  • 举报
回复
关注~ 也在做

16,551

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Creator Browser
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧