2010-10-01 195 views
3

如何使用C#打印.rtf文件? (的WinForms/WPF應用程序)如何打印.rtf文件?

+5

請您詳細說明一下嗎? – 2010-10-01 11:13:50

回答

4

https://support.microsoft.com/en-us/kb/812425

在Visual C#.NET或Visual C#2005,創建一個名爲RichTextBoxPrintCtrl的類庫項目。默認情況下,創建Class1.cs。 將Class1.cs的名稱更改爲RichTextBoxPrintCtrl.cs。 在解決方案資源管理器中,右鍵單擊引用,然後單擊添加引用。 在添加引用對話框中,雙擊System.Drawing.dll和System.Windows.Forms.dll,然後單擊確定。

用下面的代碼替換現有的代碼在RichTextBoxPrintCtrl.cs:

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 
      RECT rectToPrint; 
      rectToPrint.Top = (int)(e.MarginBounds.Top * anInch); 
      rectToPrint.Bottom = (int)(e.MarginBounds.Bottom * anInch); 
      rectToPrint.Left = (int)(e.MarginBounds.Left * anInch); 
      rectToPrint.Right = (int)(e.MarginBounds.Right * anInch); 

      //Calculate the size of the page 
      RECT rectPage; 
      rectPage.Top = (int)(e.PageBounds.Top * anInch); 
      rectPage.Bottom = (int)(e.PageBounds.Bottom * anInch); 
      rectPage.Left = (int)(e.PageBounds.Left * anInch); 
      rectPage.Right = (int)(e.PageBounds.Right * anInch); 

      IntPtr hdc = e.Graphics.GetHdc(); 

      FORMATRANGE fmtRange; 
      fmtRange.chrg.cpMax = charTo;    //Indicate character from to character to 
      fmtRange.chrg.cpMin = charFrom; 
      fmtRange.hdc = hdc;     //Use the same DC for measuring and rendering 
      fmtRange.hdcTarget = hdc;    //Point at printer hDC 
      fmtRange.rc = rectToPrint;    //Indicate the area on page to print 
      fmtRange.rcPage = rectPage;   //Indicate size of page 

      IntPtr res = IntPtr.Zero; 

      IntPtr wparam = IntPtr.Zero; 
      wparam = new IntPtr(1); 

      //Get the pointer to the FORMATRANGE structure in memory 
      IntPtr lparam= IntPtr.Zero; 
      lparam = Marshal.AllocCoTaskMem(Marshal.SizeOf(fmtRange)); 
      Marshal.StructureToPtr(fmtRange, lparam, false); 

      //Send the rendered data for printing 
      res = SendMessage(Handle, EM_FORMATRANGE, wparam, lparam); 

      //Free the block of memory allocated 
      Marshal.FreeCoTaskMem(lparam); 

      //Release the device context handle obtained by a previous call 
      e.Graphics.ReleaseHdc(hdc); 

      //Return last + 1 character printer 
      return res.ToInt32(); 
     } 

    } 
} 

在調試菜單上,單擊開始運行該應用程序。顯示Form1。 在RichTextBoxPrintCtrl中鍵入一些文本。 單擊頁面設置以設置頁面設​​置。 單擊打印預覽可查看頁面的打印預覽。 單擊「打印」以打印RichTextBoxPrintCtrl的內容。

+0

直接鏈接:http://support.microsoft.com/default.aspx?scid=kb;en-us;812425 – amalgamate 2014-02-07 18:14:35