打印文档
1
using System;
2
using System.Drawing;
3
using System.Collections;
4
using System.ComponentModel;
5
using System.Windows.Forms;
6
using System.Data;
7
// 新添加的命名空间。
8
using System.Drawing.Printing;
9
using System.IO;
10![]()
11
namespace PrintDoc
12
{
13
/// <summary>
14
/// 打印文档。
15
/// </summary>
16
public class Form1 : System.Windows.Forms.Form
17
{
18
private System.Windows.Forms.MainMenu mainMenu1;
19
private System.Windows.Forms.RichTextBox richTextBox1;
20
private System.Windows.Forms.MenuItem menuItem1;
21
private System.Windows.Forms.MenuItem menuItem2;
22
private System.Windows.Forms.MenuItem menuItem3;
23
private System.Windows.Forms.MenuItem menuItem4;
24
private System.Windows.Forms.MenuItem menuItem5;
25
private System.Windows.Forms.PrintDialog printDialog1;
26
private System.Drawing.Printing.PrintDocument printDocument1;
27
private System.Windows.Forms.PrintPreviewDialog printPreviewDialog1;
28
private System.Windows.Forms.MenuItem menuItem6;
29
/// <summary>
30
/// 必需的设计器变量。
31
/// </summary>
32
private System.ComponentModel.Container components = null;
33![]()
34
public Form1()
35
{
36
//
37
// Windows 窗体设计器支持所必需的
38
//
39
InitializeComponent();
40![]()
41
//
42
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
43
//
44
}
45![]()
46
/// <summary>
47
/// 清理所有正在使用的资源。
48
/// </summary>
49
protected override void Dispose( bool disposing )
50
{
51
if( disposing )
52
{
53
if (components != null)
54
{
55
components.Dispose();
56
}
57
}
58
base.Dispose( disposing );
59
}
60![]()
61
Windows Form Designer generated code
174![]()
175
/// <summary>
176
/// 应用程序的主入口点。
177
/// </summary>
178
[STAThread]
179
static void Main()
180
{
181
Application.Run(new Form1());
182
}
183![]()
184
private void menuItem5_Click(object sender, System.EventArgs e)
185
{
186
Close();
187
}
188![]()
189
private void menuItem3_Click(object sender, System.EventArgs e)
190
{
191
if (printDialog1.ShowDialog() == DialogResult.OK)
192
printDocument1.Print();
193
}
194![]()
195
private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
196
{
197
Font myFont = new Font("", 30, FontStyle.Regular);
198
int x = 0;
199
int y = 30;
200
int i = 0;
201
for (i = 0; i < richTextBox1.Lines.Length; i++)
202
{
203
e.Graphics.DrawString(richTextBox1.Lines[i], myFont, Brushes.Blue, x, y);
204
y += myFont.Height + 10;
205
}
206
}
207![]()
208
private void menuItem6_Click(object sender, System.EventArgs e)
209
{
210
printPreviewDialog1.ShowDialog();
211
}
212
}
213
}
214![]()
using System;2
using System.Drawing;3
using System.Collections;4
using System.ComponentModel;5
using System.Windows.Forms;6
using System.Data;7
// 新添加的命名空间。8
using System.Drawing.Printing;9
using System.IO;10

11
namespace PrintDoc12
{13
/// <summary>14
/// 打印文档。15
/// </summary>16
public class Form1 : System.Windows.Forms.Form17
{18
private System.Windows.Forms.MainMenu mainMenu1;19
private System.Windows.Forms.RichTextBox richTextBox1;20
private System.Windows.Forms.MenuItem menuItem1;21
private System.Windows.Forms.MenuItem menuItem2;22
private System.Windows.Forms.MenuItem menuItem3;23
private System.Windows.Forms.MenuItem menuItem4;24
private System.Windows.Forms.MenuItem menuItem5;25
private System.Windows.Forms.PrintDialog printDialog1;26
private System.Drawing.Printing.PrintDocument printDocument1;27
private System.Windows.Forms.PrintPreviewDialog printPreviewDialog1;28
private System.Windows.Forms.MenuItem menuItem6;29
/// <summary>30
/// 必需的设计器变量。31
/// </summary>32
private System.ComponentModel.Container components = null;33

34
public Form1()35
{36
//37
// Windows 窗体设计器支持所必需的38
//39
InitializeComponent();40

41
//42
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码43
//44
}45

46
/// <summary>47
/// 清理所有正在使用的资源。48
/// </summary>49
protected override void Dispose( bool disposing )50
{51
if( disposing )52
{53
if (components != null) 54
{55
components.Dispose();56
}57
}58
base.Dispose( disposing );59
}60

61
Windows Form Designer generated code174

175
/// <summary>176
/// 应用程序的主入口点。177
/// </summary>178
[STAThread]179
static void Main() 180
{181
Application.Run(new Form1());182
}183

184
private void menuItem5_Click(object sender, System.EventArgs e)185
{186
Close();187
}188

189
private void menuItem3_Click(object sender, System.EventArgs e)190
{191
if (printDialog1.ShowDialog() == DialogResult.OK)192
printDocument1.Print();193
}194

195
private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)196
{197
Font myFont = new Font("", 30, FontStyle.Regular);198
int x = 0;199
int y = 30;200
int i = 0;201
for (i = 0; i < richTextBox1.Lines.Length; i++)202
{203
e.Graphics.DrawString(richTextBox1.Lines[i], myFont, Brushes.Blue, x, y);204
y += myFont.Height + 10;205
}206
}207

208
private void menuItem6_Click(object sender, System.EventArgs e)209
{210
printPreviewDialog1.ShowDialog();211
}212
}213
}214



浙公网安备 33010602011771号