C# richTextBox封装的一个打印的类

附件
http://files.cnblogs.com/xe2011/CSharpWinForm_richTextBoxPrintClass.rar
在窗体上一个Richtextbox 控件 和3个按钮
调用
页面设置
private void btnPageSetup_Click(object sender, EventArgs e)
{
richTextBoxPrintClass r = new richTextBoxPrintClass();
r.richTextBox = richTextBox1;
r.ShowPageSetupDlg();
}
打印预览
private void btnPrintPreview_Click(object sender, EventArgs e)
{
richTextBoxPrintClass r = new richTextBoxPrintClass();
r.richTextBox = richTextBox1;
r.ShowShowPagePriviewDlg();
}
打印
private void btnPrint_Click(object sender, EventArgs e)
{
richTextBoxPrintClass r = new richTextBoxPrintClass();
r.richTextBox = richTextBox1;
r.ShowPrintDlg();
}
完整的类
using System;
using System.Windows.Forms;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Drawing.Printing; #region 调用示例
/*使用
//页面设置
private void btnPageSetup_Click(object sender, EventArgs e)
{
richTextBoxPrintClass r = new richTextBoxPrintClass();
r.richTextBox = richTextBox1;
r.ShowPageSetupDlg();
}
//打印预览功能
private void btnPrintPreview_Click(object sender, EventArgs e)
{
richTextBoxPrintClass r = new richTextBoxPrintClass();
r.richTextBox = richTextBox1;
r.ShowShowPagePriviewDlg();
}
//打印
private void btnPrint_Click(object sender, EventArgs e)
{
richTextBoxPrintClass r = new richTextBoxPrintClass();
r.richTextBox = richTextBox1;
r.ShowPrintDlg();
}
*/
#endregion
//http://support.microsoft.com/kb/812425/zh-cn
namespace WindowsFormsApplication1
{
class richTextBoxPrintClass
{
public richTextBoxPrintClass()
{
InitializeComponent();
}
#region 打印功用 //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 + ; [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 static int Print(RichTextBox richTextBox, 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(); //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(richTextBox.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();
} #endregion #region 初始化
public RichTextBox richTextBox;
private PageSetupDialog pageSetupDialog1;
private PrintDialog printDialog1;
private PrintDocument printDocument1;
private PrintPreviewDialog printPreviewDialog1; private void InitializeComponent()
{
richTextBox = new RichTextBox();
pageSetupDialog1 = new PageSetupDialog();
printDialog1 = new PrintDialog();
printDocument1 = new PrintDocument();
printPreviewDialog1 = new PrintPreviewDialog(); //
// pageSetupDialog1
//
pageSetupDialog1.Document = printDocument1;
//
// printDialog1
//
printDialog1.Document = printDocument1;
printDialog1.UseEXDialog = true;
//
// printDocument1
//
printDocument1.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(printDocument1_PrintPage);
printDocument1.BeginPrint += new System.Drawing.Printing.PrintEventHandler(printDocument1_BeginPrint);
//
// printPreviewDialog1
// printPreviewDialog1.AutoScrollMargin = new System.Drawing.Size(, );
printPreviewDialog1.AutoScrollMinSize = new System.Drawing.Size(, );
printPreviewDialog1.ClientSize = new System.Drawing.Size(, );
printPreviewDialog1.Document = printDocument1;
printPreviewDialog1.Enabled = true;
// printPreviewDialog1.Icon = ((System.Drawing.Icon)(resources.GetObject("printPreviewDialog1.Icon")));
printPreviewDialog1.Name = "printPreviewDialog1";
printPreviewDialog1.Visible = false;
} private int checkPrint;
private void printDocument1_BeginPrint(object sender, System.Drawing.Printing.PrintEventArgs e)
{
checkPrint = ;
} private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
// Print the content of RichTextBox. Store the last character printed.
//checkPrint = richTextBoxPrintCtrl1.Print(checkPrint, richTextBoxPrintCtrl1.TextLength, e);
checkPrint = richTextBoxPrintClass.Print(richTextBox, checkPrint, richTextBox.TextLength, e); // Check for more pages
if (checkPrint < richTextBox.TextLength)
e.HasMorePages = true;
else
e.HasMorePages = false;
} #endregion //页面设置功能
public void ShowPageSetupDlg()
{
pageSetupDialog1.ShowDialog();
} //打印预览功能
public void ShowShowPagePriviewDlg()
{
printPreviewDialog1.ShowDialog();
} //打印
public void ShowPrintDlg()
{
if (printDialog1.ShowDialog() == DialogResult.OK)
printDocument1.Print();
}
}
}
richTextBoxPrintClass
C# richTextBox封装的一个打印的类的更多相关文章
- C#封装的一个JSON操作类
		
using System; using System.Collections.Generic; using System.Collections; using System.Text; using S ...
 - 封装了一个HOOKAPI的类。。。
		
我惊奇地发现,我手竟然这么生了..... 很久很久没有写这方面的东西,现在写着竟然这么手生....哎.... 这玩艺,还得天天练....
 - PHP实现的一个时间帮助类
		
背景 工作的过程中经常会遇到各种时间类的操作,因此封装了一个帮助工具类,提高代码的复用率 主要功能 根据相差的天数获取连续的时间段 /** * 根据相差的天数获取所有连续的时间段 * @param $ ...
 - Timber(对Log类封装的一个工具)
		
Timber(对Log类封装的一个工具) https://blog.csdn.net/hzl9966/article/details/51314137 https://www.jianshu.com/ ...
 - SpringMVC 中,当前台传入多个参数时,可将参数封装成一个bean类
		
在实际业务场景中,当前台通过 url 向后台传送多个参数时,可以将参数封装成一个bean类,在bean类中对各个参数进行非空,默认值等的设置. 前台 url ,想后台传送两个参数,userName 和 ...
 - 基于Dapper二次封装了一个易用的ORM工具类:SqlDapperUtil
		
基于Dapper二次封装了一个易用的ORM工具类:SqlDapperUtil,把日常能用到的各种CRUD都进行了简化封装,让普通程序员只需关注业务即可,因为非常简单,故直接贴源代码,大家若需使用可以直 ...
 - 封装一个redis操作类来操作hash格式
		
最近项目要用redis,依然是基于tp3.2. 发现thinkphp3.2自带的缓存类并不好使用,就自己封装了一个 目前只支持hash格式,其他数据类型的操作后面用到的时候再补充 <?php / ...
 - Python_selenium封装一个浏览器引擎类
		
Python_selenium封装一个浏览器引擎类 现在我们在编写一个类,叫浏览器引擎类(此例为:启动浏览器),将文件名命名为browser.py,代码下面通过更改一个字符串的值,运用if语句判断和控 ...
 - block传值以及利用block封装一个网络请求类
		
1.block在俩个UIViewController间传值 近期刚学了几招block 的高级使用方法,事实上就是利用block语法在俩个UIViewController之间传值,在这里分享给刚開始学习 ...
 
随机推荐
- Javascript 备忘
			
1遍历所有属性 var person={fname:"John",lname:"Doe",age:25}; for (x in person) { txt=tx ...
 - Linq语句与aspnetpager结合分页
			
public void DataBindList() { List<EnDeContent> listCon = null; in ...
 - 数据库 - FMDB
			
FMDB 是基于 SQLite 封装的 面向对对象(OC) 的API. FMDB是iOS平台的SQLite数据库框架 FMDB以OC的方式封装了SQLite的C语言API FMDB 需要libsqli ...
 - C++返回引用的函数
			
要以引用返回函数值,则函数定义时的格式如下: 类型标识符&类型名 (形参列表及类型说明) { 函数体 } 用const限定引用的声明方式为: const 类型标识符&引用名=目标变量名 ...
 - hdu 4731
			
一道找规律的题,但今天的智商捉急,一直都想不到点子上: 比赛之后和别人讨论的时候,在n=2的情况下,前面两个是aa,后面就接着很多个aababb,然后最后再判断下就行了~~~ 以后对于这种题还是不要太 ...
 - iOS,object-c传参c语言的二维数组
			
有那么一瞬间,懒得用NSArray,NSNumber,NSValue等一大堆蛋疼的转换,所以就定义了一个C的二维数组,反正OC支持C混编,可是蛋疼往往是传递的,这里不疼了,哪里就要疼,想把一个c的二维 ...
 - VS2010 C# 调用Web Service .
			
转自:http://blog.csdn.net/fenglailea/article/details/7262566 打开VS2010,打开"文件-新建-网站",选择"A ...
 - 使用VisualStudio2010创建C#应用程序
			
打开VisualStudio2010,选择“文件”——“新建”——“项目”菜单命令.调出“新建项目”对话框.
 - Android 如何把一个 RelativeLayout或ImageView背景设为透明
			
在项目中,需要把RelativeLayout 和 ImageView背景设置为透明,怎么实现呢?这里主要通过代码,请参阅以下关键代码: public ImageView imgDetail; pri ...
 - 【HDOJ】2612 Find a way
			
BFS. #include <iostream> #include <cstdio> #include <cstring> #include <queue&g ...