1
using
System;
2
using
System.Collections.Generic;
3
using
System.Linq;
4
using
System.Text;
5
using
System.Windows.Forms;
6
using
DevExpress.XtraPrinting;
7
8
namespace
Teleware.GeologicDisaster.ForecastApp
9
{
10
public
class
PrintClass
11
{
12
#region
私有字段
13
14
private
PrintingSystem ps
=
null
;
15
private
string
formName
=
null
;
16
private
PrintableComponentLink link
=
null
;
17
18
#endregion
19
20
#region
属性
21
string
_PrintHeader
=
null
;
//
设置打印时的标题
22
private
string
_PrintFooter
=
null
;
//
设置打印时页脚显示内容
23
private
bool
_landScape;
//
设置页面横纵向
24
private
System.Drawing.Printing.PaperKind _paperKind;
//
设置纸张类型(A1、A2、A3、A4)
25
26
///
<summary>
27
///
设置打印时的标题
28
///
</summary>
29
public
string
PrintHeader
30
{
31
set
32
{
33
_PrintHeader
=
value;
34
}
35
}
36
37
///
<summary>
38
///
设置打印时页脚显示内容
39
///
</summary>
40
public
string
PrintFooter
41
{
42
set
43
{
44
_PrintFooter
=
value;
45
}
46
}
47
48
///
<summary>
49
///
设置页面横纵向
50
///
</summary>
51
public
bool
LandScape
52
{
53
set
{ _landScape
=
value; }
54
}
55
56
///
<summary>
57
///
设置纸张类型(A1、A2、A3、A4)
58
///
</summary>
59
public
System.Drawing.Printing.PaperKind PaperKind
60
{
61
set
{ _paperKind
=
value; }
62
}
63
#endregion
64
65
#region
构造函数
66
///
<summary>
67
///
打印控制器
68
///
</summary>
69
///
<param name="control">
要打印的部件
</param>
70
public
PrintClass(IPrintable control)
71
{
72
if
(control
==
null
)
73
{
74
return
;
75
}
76
Control c
=
(Control)control;
77
formName
=
c.FindForm().GetType().FullName
+
"
.
"
+
c.Name;
78
ps
=
new
DevExpress.XtraPrinting.PrintingSystem();
79
link
=
new
DevExpress.XtraPrinting.PrintableComponentLink(ps);
80
ps.Links.Add(link);
81
link.Component
=
control;
82
}
83
#endregion
84
85
#region
打印预览
86
///
<summary>
87
///
打印预览
88
///
</summary>
89
public
void
Preview()
90
{
91
try
92
{
93
if
(DevExpress.XtraPrinting.PrintHelper.IsPrintingAvailable)
94
{
95
Cursor.Current
=
Cursors.AppStarting;
96
if
(_PrintHeader
!=
null
)
97
{
98
PageHeaderFooter phf
=
link.PageHeaderFooter
as
PageHeaderFooter;
99
100
//
设置页眉
101
phf.Header.Content.Clear();
102
phf.Header.Content.AddRange(
new
string
[] {
""
, _PrintHeader,
""
});
103
phf.Header.Font
=
new
System.Drawing.Font(
"
宋体
"
,
14
, System.Drawing.FontStyle.Bold);
104
phf.Header.LineAlignment
=
BrickAlignment.Center;
105
106
//
设置页脚
107
phf.Footer.Content.Clear();
108
phf.Footer.Content.AddRange(
new
string
[] {
""
,
""
, _PrintFooter });
109
phf.Footer.Font
=
new
System.Drawing.Font(
"
宋体
"
,
9
, System.Drawing.FontStyle.Regular);
110
phf.Footer.LineAlignment
=
BrickAlignment.Center;
111
112
}
113
link.PaperKind
=
ps.PageSettings.PaperKind;
114
link.Margins
=
ps.PageSettings.Margins;
115
link.Landscape
=
ps.PageSettings.Landscape;
116
link.CreateDocument();
117
118
//
汉化
119
//
DevExpress.XtraPrinting.Localization.PreviewLocalizer.Active = new Dxperience.LocalizationCHS.DxperienceXtraPrintingLocalizationCHS();
120
ps.PreviewFormEx.Show();
121
122
}
123
else
124
{
125
Cursor.Current
=
Cursors.Default;
126
MessageBox.Show(
"
打印机不可用
"
,
"
提示
"
, MessageBoxButtons.OK, MessageBoxIcon.Information);
127
}
128
}
129
finally
130
{
131
Cursor.Current
=
Cursors.Default;
132
}
133
}
134
#endregion
135
136
#region
打印
137
138
///
<summary>
139
///
打印
140
///
</summary>
141
public
void
Print()
142
{
143
try
144
{
145
if
(DevExpress.XtraPrinting.PrintHelper.IsPrintingAvailable)
146
{
147
if
(_PrintHeader
!=
null
)
148
{
149
PageHeaderFooter phf
=
link.PageHeaderFooter
as
PageHeaderFooter;
150
151
//
设置页眉
152
phf.Header.Content.Clear();
153
phf.Header.Content.AddRange(
new
string
[] {
""
, _PrintHeader,
""
});
154
phf.Header.Font
=
new
System.Drawing.Font(
"
宋体
"
,
14
, System.Drawing.FontStyle.Bold);
155
phf.Header.LineAlignment
=
BrickAlignment.Center;
156
157
//
设置页脚
158
phf.Footer.Content.Clear();
159
phf.Footer.Content.AddRange(
new
string
[] {
""
,
""
, _PrintFooter });
160
phf.Footer.Font
=
new
System.Drawing.Font(
"
宋体
"
,
9
, System.Drawing.FontStyle.Regular);
161
phf.Footer.LineAlignment
=
BrickAlignment.Center;
162
163
}
164
link.PaperKind
=
ps.PageSettings.PaperKind;
165
link.Margins
=
ps.PageSettings.Margins;
166
link.Landscape
=
ps.PageSettings.Landscape;
167
link.CreateDocument();
168
link.CreateDocument();
169
//
汉化
170
//
DevExpress.XtraPrinting.Localization.PreviewLocalizer.Active = new Dxperience.LocalizationCHS.DxperienceXtraPrintingLocalizationCHS();
171
ps.Print();
172
}
173
else
174
{
175
Cursor.Current
=
Cursors.Default;
176
MessageBox.Show(
"
打印机不可用
"
,
"
提示
"
, MessageBoxButtons.OK, MessageBoxIcon.Information);
177
}
178
}
179
finally
180
{
181
}
182
}
183
184
#endregion
185
186
#region
获取页面设置信息
187
188
///
<summary>
189
///
获取页面设置信息
190
///
</summary>
191
public
void
LoadPageSetting()
192
{
193
System.Drawing.Printing.Margins margins
=
new
System.Drawing.Printing.Margins(
60
,
60
,
60
,
60
);
194
ps.PageSettings.Assign(margins, _paperKind, _landScape);
195
}
196
#endregion
197
}
198
}
199