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之间传值,在这里分享给刚開始学习 ...
随机推荐
- [置顶] Android Journal
==================================================================================================== ...
- JNI/NDK开发指南(开山篇)
转载请注明出处:http://blog.csdn.net/xyang81/article/details/41759643 相信很多做过Java或Android开发的朋友经常会接触到JNI方面的技术, ...
- Bootstrap 分页插件 ajax获取数据显示
Bootstrap 分页插件 ajax获取数据显示 标签(空格分隔): bootstrap 文章的内容是使用bootstrap-paginator进行分页,使用ajax获取后台数据.渲染. 1. 版本 ...
- bzoj 2244: [SDOI2011]拦截导弹 cdq分治
2244: [SDOI2011]拦截导弹 Time Limit: 30 Sec Memory Limit: 512 MBSec Special JudgeSubmit: 237 Solved: ...
- hdu 1827
强连通分量——tarjin算法: 这题的思路就是找出多少个出度为0的连通分量,结果就是这些连通分量的元素的最小值相加: 一道很简单的题,改了我好久,= =!~ 贴代码: #include<cst ...
- 李洪强iOS开发Swift篇---12_NSThread线程相关简单说明
李洪强iOS开发Swift篇---12_NSThread线程相关简单说明 一 说明 1)关于多线程部分的理论知识和OC实现,在之前的博文中已经写明,所以这里不再说明. 2)该文仅仅简单讲解NSThre ...
- C51 的编程规范
编程首要是要考虑程序的可行性,然后是可读性.可移植性.健壮性以及可测试性.这是总则.但是很多人忽略了可读性.可移植性和健壮性(可调试的方法可能歌不相同),这是不对的. 1.当项目比较大时,最好分模块编 ...
- vs2010创建和使用动态链接库(dll)
本文将创建一个简单的动态链接库,并编写一个应用台控制程序使用该动态链接库,并提出了与实现相关的几个问题,供初学者交流. 本文包含以下内容: 创建动态链接库项目 向动态链接库添加类 创建引用动态链接库的 ...
- Android Intent.FLAG_NEW_TASK详解,包括其他的标记的一些解释
本文大部分参考自 http://blog.csdn.net/mayingcai1987/article/details/6200909 ,对原文中的讲解FLAG_NEW_TASK地方加了一些自己的观点 ...
- WCF 托管在IIS中遇到Http的错误
IIS8中部署WCF服务出错:HTTP 错误 404.3 - Not Found http://www.cnblogs.com/xwgli/archive/2013/03/15/2961022.htm ...