我是用的自定义打印,即用CDC类的StartDoc()、EndDoc()等函数,我的文本是CString,其中含有“\r\n”,想实现换行,我不想将字符串拆分后按行打印,有没有好的方法直接实现打印换行?

解决方案 »

  1.   

    RedealTextData 函 数 根 据 每 行 最 大 宽 度 对 文 件 进 行 重 新 调 整。 主 要 是 计 算 文 件 中 每 行 的 宽 度, 如 果 超 过 最 大 宽 度 则 加 入 换 行 符(0x0d,0x0a)。 函 数 实 现 如 下: 
    //=======================================// RedealTextData//注://pDoc->buffer为文件缓冲区//pDoc->file_length为文件字节长度//pDoc->TextLines为文件原行数//pDoc->MaxLineLength为文件原最大行字节宽度//=======================================void CTextView::RedealTextData(){  CDocViewDoc* pDoc = GetDocument();  ASSERT_VALID(pDoc);  short LineLengthMax = m_MaxLineChar;  unsigned short lines=0;  unsigned long i,j;  //申请新的缓冲区保存调整后的文件  long size = pDoc->file_length + pDoc->TextLines* (pDoc->MaxLineLength/m_MaxLineChar+1);  m_newBuffer = new char [size ];  LPSTR newTempPtr = m_newBuffer;  m_file_length =pDoc->file_length;  //保存文件新的行数  m_lines = 1;   i = 0;  //记录当前行的宽度  short theLineLength=0;   //记录当前行中汉字字节数,  //以防止将一半汉字分为两行  unsigned short halfChinese=0;  while(i file_length)  {   *newTempPtr++ = pDoc->buffer[i];    j=i+1;   if( (pDoc->buffer[i] == 0x0d &&  pDoc->buffer[j] == 0x0a))   {      m_lines++;      theLineLength = 0;   }   else   {      //如果是TAB字符,宽度加8      if(pDoc->buffer[i] == VK_TAB) theLineLength += 8;      else       {      //大于0xa1的字节为汉字字节 if((unsigned char)pDoc->buffer[i] >= 0xa1) halfChinese++; theLineLength++; } //如果行宽大于每行最大宽度,进行特殊处理 if(theLineLength > LineLengthMax) {   char buff[256];   short m=255;   newTempPtr--;          if((unsigned char )*newTempPtr <0xa1) 
    //如果当前字符的前一个字符是数字、 //字母或一些特殊的前置符号时, 
    //指针循环向前取, //以防止将一个单词分为两行。
     while((*newTempPtr>='0' && *newTempPtr<='9')
    || (*newTempPtr>='a' && *newTempPtr <= 'z') 
    || (*newTempPtr>='A' && *newTempPtr <= 'Z') 
    || *newTempPtr="=" '_' || *newTempPtr="=" '*' 
    || *newTempPtr="=" '^' 
    || *newTempPtr="=" '~' ) buff[m--]="*newTempPtr--;" } else
    //汉字 { //防止将一个汉字分为两行。 
    if(halfChinese%2) buff[m--]="*newTempPtr--;" } newTempPtr++; 
    //加入换行符,分为两行 
    *newTempPtr++="0x0d;" ;*newTempPtr++="0x0a;" ; 
    for(short k="m+1;" k<256; k++) *newTempPtr++="buff[k];" m_lines++; 
    theLineLength="0;" m_file_length +="2;" } } i++; } } 
    比较复杂,我给你个地址,你上去看看吧
    http://www.csdn.net/Dev/Visual%20C++/document/Article/vc0008.htm
      

  2.   

    首先谢谢!听说用DrawText和楼上的方法都可以实现换行,但是我的东西其实每行都比较短,而且每行的长度都不一样,而行数有很多,有很多页,用DrawText和上面的方法实现起来还是不太方便吧
      

  3.   

    刚刚我用DrawText试了一下,还是有点问题,● DT_BOTTOM底部对齐 Specifies bottom-justified text. This value must be combined with DT_SINGLELINE. 
     
     
    ● DT_CALCRECT计算指定文字时所需要矩形尺寸 Determines the width and height of the rectangle. If there are multiple lines of text, DrawText will use the width of the rectangle pointed to by lpRect and extend the base of the rectangle to bound the last line of text. If there is only one line of text, DrawText will modify the right side of the rectangle so that it bounds the last character in the line. In either case, DrawText returns the height of the formatted text, but does not draw the text. 
     
     
    ● DT_CENTER中部对齐 Centers text horizontally. 
     
     
    ● DT_END_ELLIPSIS or DT_PATH_ELLIPSIS Replaces part of the given string with ellipses, if necessary, so that the result fits in the specified rectangle. The given string is not modified unless the DT_MODIFYSTRING flag is specified. 
    You can specify DT_END_ELLIPSIS to replace characters at the end of the string, or DT_PATH_ELLIPSIS to replace characters in the middle of the string. If the string contains backslash (\) characters, DT_PATH_ELLIPSIS preserves as much as possible of the text after the last backslash. 
     
    ● DT_EXPANDTABS Expands tab characters. The default number of characters per tab is eight. 
     
     
    ● DT_EXTERNALLEADING Includes the font抯 external leading in the line height. Normally, external leading is not included in the height of a line of text. 
     
     
    ● DT_LEFT左对齐 Aligns text flush-left. 
     
     
    ● DT_MODIFYSTRING Modifies the given string to match the displayed text. This flag has no effect unless the DT_END_ELLIPSIS or DT_PATH_ELLIPSIS flag is specified. 
    Note Some uFormat flag combinations can cause the passed string to be modified. Using DT_MODIFYSTRING with either DT_END_ELLIPSIS or DT_PATH_ELLIPSIS may cause the string to be modified, causing an assertion in the CString override. 
     
     
    ● DT_NOCLIP Draws without clipping. DrawText is somewhat faster when DT_NOCLIP is used. 
     
     
    ● DT_NOPREFIX禁止使用&前缀 Turns off processing of prefix characters. Normally, DrawText interprets the ampersand (&) mnemonic-prefix character as a directive to underscore the character that follows, and the two-ampersand (&&) mnemonic-prefix characters as a directive to print a single ampersand. By specifying DT_NOPREFIX, this processing is turned off. 
     
     
    ● DT_PATH_ELLIPSIS  
     
     
    ● DT_RIGHT右对齐 Aligns text flush-right. 
     
     
    ● DT_SINGLELINE单行输出 Specifies single line only. Carriage returns and linefeeds do not break the line. 
     
     
    ● DT_TABSTOP设置TAB字符所占宽度 Sets tab stops. The high-order byte of nFormat is the number of characters for each tab. The default number of characters per tab is eight. 
     
     
    ● DT_TOP定部对齐 Specifies top-justified text (single line only). 
     
     
    ● DT_VCENTER中部对齐 Specifies vertically centered text (single line only). 
     
     
    ● DT_WORDBREAK每行只在单词间被折行 Specifies word-breaking. Lines are automatically broken between words if a word would extend past the edge of the rectangle specified by lpRect. A carriage return杔inefeed sequence will also break the line.  
     
    在输出文字时如果希望改变文字的颜色,你可以利用CDC::SetTextColor( COLORREF crColor )进行设置,如果你希望改变背景色就利用CDC::SetBkColor( COLORREF crColor ),很多时候你可能需要透明的背景色你可以利用CDC::SetBkMode( int nBkMode )设置,可接受的参数有  
     
    ● OPAQUE Background is filled with the current background color before the text, hatched brush, or pen is drawn. This is the default background mode. 
     
     
    ● TRANSPARENT Background is not changed before drawing. 我用上面的参数进行了组合,没找出合适的来~`
      

  4.   

    对TextOut来讲只能输出单行的文字,而DrawText可以指定在一个矩形中输出单行或多行文字,并且可以规定对齐方式和使用何种风格。
    你再试一下,用DrawText应该能行的~~
      

  5.   

    辛苦辛苦,真的感谢!如果能有像屏幕输出一样能响应“\r\n”的打印设置就好了,呵呵使用DrawText的时候,如果我一次想打印三行文字,那么这三行文字的长度应该都是CRect的宽度吧?如果这样的话,我就只能一行一行打印,可是单行打印时就得认为的确定行高和CRect的位置,这在分页打印时会不会出现问题?
      

  6.   

    这三行文字的长度都是CRect的宽度分页的时候肯定会有问题,可能还要判~~能响应“\r\n”的打印设置,我不知道有没有,如果找到了也告诉我一声,呵呵
      

  7.   

    问题已经解决,用了另外一种思路,就是先写文件,再打印,也就可以响应“\r\n”了,非常方便,呵呵,文件打印命令:ShellExecute(NULL, "print", sFileName, NULL, sFilePath, NULL);非常感谢tyjoe(天乐) !现在结贴