日期:2014-05-20  浏览次数:21024 次

再提打印问题
本人想通过创建   RichTextBoxPrintCtrl   控件中所输入的值来打印,现在只能打印一个RichTextBoxPrintCtrl   控件中所输入的值,如果我想打印5个chTextBoxPrintCtrl   中值该如何实现


创建   RichTextBoxPrintCtrl   控件
-----------------------------------
新建一个名为   RichTextBoxPrintCtrl   的类库项目,代码如下:
using   System;
using   System.Windows.Forms;
using   System.Drawing;
using   System.Runtime.InteropServices;
using   System.Drawing.Printing;

namespace   RichTextBoxPrintCtrl
{
public   class   RichTextBoxPrintCtrl:RichTextBox
{
//Convert   the   unit   used   by   the   .NET   framework   (1/100   inch)  
//and   the   unit   used   by   Win32   API   calls   (twips   1/1440   inch)
private   const   double   anInch   =   14.4;

[StructLayout(LayoutKind.Sequential)]  
private   struct   RECT
{
public   int   Left;
public   int   Top;
public   int   Right;
public   int   Bottom;
}

[StructLayout(LayoutKind.Sequential)]
private   struct   CHARRANGE
{
public   int   cpMin;                   //First   character   of   range   (0   for   start   of   doc)
public   int   cpMax;                       //Last   character   of   range   (-1   for   end   of   doc)
}

[StructLayout(LayoutKind.Sequential)]
private   struct   FORMATRANGE
{
public   IntPtr   hdc;                           //Actual   DC   to   draw   on
public   IntPtr   hdcTarget;               //Target   DC   for   determining   text   formatting
public   RECT   rc;                                 //Region   of   the   DC   to   draw   to   (in   twips)
public   RECT   rcPage;                         //Region   of   the   whole   DC   (page   size)   (in   twips)
public   CHARRANGE   chrg;                   //Range   of   text   to   draw   (see   earlier   declaration)
}

private   const   int   WM_USER     =   0x0400;
private   const   int   EM_FORMATRANGE     =   WM_USER   +   57;

[DllImport( "USER32.dll ")]
private   static   extern   IntPtr   SendMessage   (IntPtr   hWnd   ,   int   msg   ,   IntPtr   wp,   IntPtr   lp);  

//   Render   the   contents   of   the   RichTextBox   for   printing
// Return   the   last   character   printed   +   1   (printing   start   from   this   point   for   next   page)
public   int   Print(   int   charFrom,   int   charTo,PrintPageEventArgs   e)
{
//Calculate   the   area   to   render   and   print