当前位置: 代码迷 >> VC/MFC >> MFC打印机怎么解决单行文本过长的有关问题
  详细解决方案

MFC打印机怎么解决单行文本过长的有关问题

热度:91   发布时间:2016-05-02 03:43:02.0
MFC打印机如何解决单行文本过长的问题
如题,基于对话框的MFC打印程序,我的里面有个编辑框的控件,当我在编辑框内输入一二三四五六七八九十十一十二十三十四十五的时候 ,打印机纸张过窄只能打印出一二三四五六七八九十,各位大神能不能告诉我如何让它自动换行打印,附上打印按钮代码,
void CprintDCDlg::OnBnClickedButton1()
{
// TODO: 在此添加控件通知处理程序代码
 this->UpdateData();  
  
    CString strMessage;  
    CString strPrintDevice;  
  
    this->GetDlgItem(IDC_EDIT1)->GetWindowTextW(strMessage);  
    strMessage += _T("\r\n");       //添加结尾,方便后面循环读取打印数据  
  
    this->GetDlgItem(IDC_COMBO1)->GetWindowTextW(strPrintDevice);  
  
    DWORD dwFlag = PD_ALLPAGES | PD_NOPAGENUMS | PD_USEDEVMODECOPIES | PD_HIDEPRINTTOFILE;  //打印配置界面的按钮可用性,因为后台打印,其实这个配置没什么意义  
  
    CPrintDialog pPrintdlg(FALSE, dwFlag, this);                                            //CPrintDialog实例化,因为MFC的打印设备无关性,可以理解为这就是一台打印机  
  
    HGLOBAL hDevMode = NULL;  
    HGLOBAL hDevNames = NULL;  
    if (GetPrinterDevice(strPrintDevice.GetBuffer(0), &hDevNames, &hDevMode))               //获得指定打印机的配置、名字  
        AfxGetApp()->SelectPrinter(hDevNames, hDevMode);  
    else  
        AfxMessageBox(_T("Failed to select custom printer"));  
  
    strPrintDevice.ReleaseBuffer();  
  
    pPrintdlg.m_pd.hDevMode = hDevMode;                                                     //让pPrintdlg使用我们指定的打印机  
    pPrintdlg.m_pd.hDevNames = hDevNames;  
  
    CDC dc;  
    dc.Attach(pPrintdlg.CreatePrinterDC());                                                 //后台打印创建法,如果需要弹出打印对话框,请用DoModal  

    DOCINFO di;                                                                             //下面的内容网上很多,就不解释了  
    di.cbSize = sizeof(DOCINFO);  
    di.lpszDocName = _T("有驱打印测试");  
    di.lpszDatatype = NULL;  
    di.lpszOutput = NULL;  
    di.fwType = 0;  
  
    dc.StartDocW(&di);  
    dc.StartPage();  
    dc.SetMapMode(MM_TEXT);  
  
    CRect recPrint(0, 0, dc.GetDeviceCaps(LOGPIXELSX), dc.GetDeviceCaps(LOGPIXELSY));
    dc.DPtoLP(&recPrint);    
    dc.SetWindowOrg(0, 0);  
  
    CFont newFont;  
    VERIFY(newFont.CreatePointFont(120, _T("宋体"), &dc));  
    CFont* oldFont = dc.SelectObject(&newFont);  
  
dc.SetTextAlign(TA_TOP | TA_LEFT);  

    CString strPrint;  
    int nIndex = 0;  
    int x = 50;  
    int y = 50;
    CSize textSize;
    textSize = dc.GetTextExtent(_T("00"), 2);                           //根据当前字体的宽、高,后面以此高度为行高
  
    while ((nIndex = strMessage.Find(_T("\r\n"))) > -1)                  //将IDC_EDIT1编辑框中内容打印,支持换行,一次换行等于'\r\n',所以在开头strMessage += _T("\r\n")
    {
        strPrint = strMessage.Left(nIndex);
        strMessage = strMessage.Mid(nIndex+2);
  
        dc.TextOutW(x, y, strPrint);
  
        y += textSize.cy;                                               //下移一行,行高为字体高度
    }

    dc.SelectObject(oldFont);
    newFont.DeleteObject();
    dc.EndPage();
    dc.EndDoc();
    DeleteDC(dc.Detach());
}

------解决思路----------------------
打印机初始化时读取打印横向点数,再写入文本处理时,先计算一行文本的点数,将超出的部分输出到下一行。这些都是你程序要自行完成的任务。
------解决思路----------------------
引用:
@zhao4zhong2 

GetTextExtentPoint32
The GetTextExtentPoint32 function computes the width and height of the specified string of text. 

BOOL GetTextExtentPoint32(
  HDC hdc,           // handle to device context
  LPCTSTR lpString,  // pointer to text string
  int cbString,      // number of characters in string
  LPSIZE lpSize      // pointer to structure for string size
);
 
Parameters
hdc 
Handle to the device context. 
lpString 
Pointer to the string of text. The string does not need to be zero-terminated, since cbString specifies the length of the string. 
cbString 
Specifies the number of characters in the string. 
lpSize 
Pointer to a SIZE structure in which the dimensions of the string are to be returned. 
Return Values
If the function succeeds, the return value is nonzero.

If the function fails, the return value is zero. 

Windows NT: To get extended error information, callGetLastError. 

Remarks
The GetTextExtentPoint32 function uses the currently selected font to compute the dimensions of the string. The width and height, in logical units, are computed without considering any clipping. 

Because some devices kern characters, the sum of the extents of the characters in a string may not be equal to the extent of the string. 

The calculated string width takes into account the intercharacter spacing set by the SetTextCharacterExtra function. 

Windows CE: The GetTextExtentPoint32 function is identical to the GetTextExtentPoint function.

QuickInfo
  Windows NT: Requires version 3.5 or later.
  Windows: Requires Windows 95 or later.
  Windows CE: Requires version 2.0 or later.
  Header: Declared in wingdi.h.
  Import Library: Use gdi32.lib.
  Unicode: Implemented as Unicode and ANSI versions on Windows and Windows NT.

See Also
Fonts and Text Overview, Font and Text Functions, SetTextCharacterExtra, SIZE 

 
  相关解决方案