RichTextBox内容写到Graphics
来源:互联网 发布:php乘法表打印txt文档 编辑:程序博客网 时间:2024/06/05 01:13
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(); }
// Render the contents of the RichTextBox for printing // Return the last character printed + 1 (printing start from this point for next page) public int PrintToGraphics(int charFrom, int charTo, Rectangle MarginBounds,Rectangle PageBounds,Graphics g) { //Calculate the area to render and print RECT rectToPrint; rectToPrint.Top = (int)(MarginBounds.Top * anInch); rectToPrint.Bottom = (int)(MarginBounds.Bottom * anInch); rectToPrint.Left = (int)(MarginBounds.Left * anInch); rectToPrint.Right = (int)(MarginBounds.Right * anInch);
//Calculate the size of the page RECT rectPage; rectPage.Top = (int)(PageBounds.Top * anInch); rectPage.Bottom = (int)(PageBounds.Bottom * anInch); rectPage.Left = (int)(PageBounds.Left * anInch); rectPage.Right = (int)(PageBounds.Right * anInch);
IntPtr hdc = g.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 g.ReleaseHdc(hdc);
//Return last + 1 character printer return res.ToInt32(); } }}
- RichTextBox内容写到Graphics
- C# RichTextBox 修改内容
- 写字符串内容到文件
- 13.02.04 C# Linq 添加、修改 带有richTextBox格式的内容到数据库
- C#WPF里面RichTextBox的内容带格式存到数据库中
- Log4net输出信息到RichTextBox
- 打开word文档到richtextbox
- RichTextBox 贴图/使用Graphics在图像上写字
- 将表中得内容写到文件中
- 把内容写到文件里
- 文件缓冲内容写到磁盘
- 将内容写到txt文档里面
- HttpClient获得内容写到文件
- Servlet练习--写内容到文件
- C# RichTextBox高度随内容动态变化
- (转载)C#richTextBox中的内容换行
- vb.net 逐行读取RichTextBox内容
- WPF SL 获取RichTextBox 的内容(string)
- Check Javascript supported version
- 把Doc文档转换成rtf格式 [C#]
- DataGridView中某一行的某一列及当前行的选取方法(C#实现)
- SWT可交互式Browser控件
- word中插入图片,图表,字符替换,表格填写,合并
- RichTextBox内容写到Graphics
- Delphi之指针使用
- 对ibatis分页功能的改进(1)
- 关于VC中控件ID值重复的解决
- ...提升网站程序开发安全的6大诀窍...
- 对ibatis分页功能的改进(2)
- org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named '/register' is defined
- python和py2exe中使用中文
- 用C#实现XML文件的读取