source: Scheduling/trunk/cs/EnhancedPrintPreview/PrintPreview/EnhancedPrintPreviewDialog.cs@ 1367

Last change on this file since 1367 was 1247, checked in by Sam Habiel, 13 years ago

Added Page Orientation Button. Changes to Portrait/Landscape.

File size: 16.0 KB
Line 
1// Code is under the CPOL license. Original by Vincenzo Rossi.
2// Taken from http://www.codeproject.com/KB/printing/EnhancedPrintPreviewDlg.aspx
3
4// Mods by Sam Habiel. Mods to remain under CPOL.
5
6using System;
7using System.Collections.Generic;
8using System.ComponentModel;
9using System.Data;
10using System.Drawing;
11using System.Text;
12using System.Windows.Forms;
13using System.Drawing.Printing;
14using System.Drawing.Drawing2D;
15
16namespace VR.PrintPreview {
17
18 public partial class EnhancedPrintPreviewDialog : Form {
19
20 #region PRIVATE FIELDS
21
22 PrintDocument mDocument;
23 PageSetupDialog mPageSetupDialog;
24 PrintDialog mPrintDialog;
25 int mVisibilePages = 1;
26 int mTotalPages = 0;
27 bool mShowPageSettingsButton = true;
28 bool mShowPrinterSettingsButton = false;
29 bool mShowPrinterSettingsBeforePrint = true;
30 List<AdditionalText> mAdditionalTextList;
31
32 #endregion
33
34 public EnhancedPrintPreviewDialog() {
35 InitializeComponent();
36 printPreviewControl1.StartPageChanged += new EventHandler(printPreviewControl1_StartPageChanged);
37 ShowPrinterSettingsButton = false;
38 }
39
40 #region PROPERTIES
41
42 public List<AdditionalText> AdditionalTextList {
43 get {
44 if (mAdditionalTextList == null) mAdditionalTextList = new List<AdditionalText>();
45 return mAdditionalTextList;
46 }
47 set { mAdditionalTextList = value; }
48 }
49
50 public PageSetupDialog PageSetupDialog {
51 get {
52 if (mPageSetupDialog == null) mPageSetupDialog = new PageSetupDialog();
53 return mPageSetupDialog;
54 }
55 set { mPageSetupDialog = value; }
56 }
57
58 public PrintDialog PrintDialog {
59 get {
60 if (mPrintDialog == null) mPrintDialog = new PrintDialog();
61 return mPrintDialog;
62 }
63 set { mPrintDialog = value; }
64 }
65
66 public bool ShowPrinterSettingsButton {
67 get { return mShowPrinterSettingsButton; }
68 set {
69 mShowPrinterSettingsButton = value;
70 tsBtnPrinterSettings.Visible = value;
71 }
72 }
73
74 public bool ShowPageSettingsButton {
75 get { return mShowPageSettingsButton; }
76 set {
77 mShowPageSettingsButton = value;
78 tsBtnPageSettings.Visible = value;
79 }
80 }
81
82 public bool ShowPrinterSettingsBeforePrint {
83 get { return mShowPrinterSettingsBeforePrint; }
84 set { mShowPrinterSettingsBeforePrint = value; }
85 }
86
87 public PrintPreviewControl PrintPreviewControl { get { return printPreviewControl1; } }
88
89 public PrintDocument Document {
90 get { return mDocument; }
91 set {
92 SwitchPrintDocumentHandlers(mDocument, false);
93 mDocument = value;
94 SwitchPrintDocumentHandlers(mDocument, true);
95 printPreviewControl1.Document = mDocument;
96 }
97 }
98
99 public bool UseAntiAlias {
100 get { return printPreviewControl1.UseAntiAlias; }
101 set { printPreviewControl1.UseAntiAlias = value; }
102 }
103
104 #endregion
105
106
107 #region DOCUMENT EVENT HANDLERS
108
109
110 void mDocument_BeginPrint(object sender, PrintEventArgs e) {
111 mTotalPages = 0;
112 }
113
114 void mDocument_EndPrint(object sender, PrintEventArgs e) {
115 tsLblTotalPages.Text = " / " + mTotalPages.ToString();
116 }
117
118 void mDocument_PrintPage(object sender, PrintPageEventArgs e) {
119 mTotalPages++;
120 if (mAdditionalTextList == null) return;
121
122 SizeF measure;
123 PointF point = new PointF();
124 Brush brush;
125 StringFormat format = new StringFormat();
126 string text;
127
128 foreach (AdditionalText at in mAdditionalTextList) {
129 text = at.Text.Replace("$pagenumber", mTotalPages.ToString());
130 measure = e.Graphics.MeasureString(text, at.Font);
131 brush = at.Brush;
132 format = new StringFormat();
133
134 switch (at.Position) {
135 case TextPosition.HTopLeft:
136 point.Y = e.MarginBounds.Top - measure.Height;
137 point.X = e.MarginBounds.Left;
138 break;
139 case TextPosition.HTopCenter:
140 point.Y = e.MarginBounds.Top - measure.Height;
141 point.X = e.MarginBounds.Left + (e.MarginBounds.Width - measure.Width) / 2;
142 break;
143 case TextPosition.HTopRight:
144 point.Y = e.MarginBounds.Top - measure.Height;
145 point.X = e.MarginBounds.Right - measure.Width;
146 break;
147 case TextPosition.HBottomLeft:
148 point.Y = e.MarginBounds.Bottom;
149 point.X = e.MarginBounds.Left;
150 break;
151 case TextPosition.HBottomCenter:
152 point.Y = e.MarginBounds.Bottom;
153 point.X = e.MarginBounds.Left + (e.MarginBounds.Width - measure.Width) / 2;
154 break;
155 case TextPosition.HBottomRight:
156 point.Y = e.MarginBounds.Bottom;
157 point.X = e.MarginBounds.Right - measure.Width;
158 break;
159 case TextPosition.VTopLeft:
160 format.FormatFlags = StringFormatFlags.DirectionVertical;
161 point.Y = e.MarginBounds.Top;
162 point.X = e.MarginBounds.Left - measure.Height;
163 break;
164 case TextPosition.VMiddleLeft:
165 format.FormatFlags = StringFormatFlags.DirectionVertical;
166 point.X = e.MarginBounds.Left - measure.Height;
167 point.Y = e.MarginBounds.Top + (e.MarginBounds.Height - measure.Width) / 2;
168
169 break;
170 case TextPosition.VBottomLeft:
171 format.FormatFlags = StringFormatFlags.DirectionVertical;
172 point.X = e.MarginBounds.Left - measure.Height;
173 point.Y = e.MarginBounds.Bottom - measure.Width;
174 break;
175 case TextPosition.VTopRight:
176 format.FormatFlags = StringFormatFlags.DirectionVertical;
177 point.Y = e.MarginBounds.Top;
178 point.X = e.MarginBounds.Right;
179 break;
180 case TextPosition.VMiddleRight:
181 format.FormatFlags = StringFormatFlags.DirectionVertical;
182 point.Y = e.MarginBounds.Top + (e.MarginBounds.Height - measure.Width) / 2;
183 point.X = e.MarginBounds.Right;
184 break;
185 case TextPosition.VBottomRight:
186 format.FormatFlags = StringFormatFlags.DirectionVertical;
187 point.Y = e.MarginBounds.Bottom - measure.Width;
188 point.X = e.MarginBounds.Right;
189 break;
190 case TextPosition.WaterMark:
191 point.Y = e.MarginBounds.Top + (e.MarginBounds.Height - measure.Height) / 2;
192 point.X = e.MarginBounds.Left + (e.MarginBounds.Width - measure.Width) / 2;
193 point.X += at.OffsetX;
194 point.Y += at.OffsetY;
195 int TranslationX = (int)(point.X + measure.Width / 2);
196 int TranslationY = (int)(point.Y + measure.Height / 2);
197 e.Graphics.TranslateTransform(TranslationX, TranslationY);
198 e.Graphics.RotateTransform(-55f);
199 point.X = -measure.Width / 2;
200 point.Y = -measure.Height / 2;
201 if (at.Brush is SolidBrush && ((SolidBrush)at.Brush).Color.A == 255)
202 brush = new SolidBrush(Color.FromArgb(100, ((SolidBrush)at.Brush).Color));
203
204 e.Graphics.DrawString(text, at.Font, brush, point, format);
205 e.Graphics.RotateTransform(55f);
206 e.Graphics.TranslateTransform(-TranslationX, -TranslationY);
207
208 break;
209 default:
210 break;
211 }
212
213
214 if (at.Position != TextPosition.WaterMark) {
215 point.X += at.OffsetX;
216 point.Y += at.OffsetY;
217 e.Graphics.DrawString(text, at.Font, brush, point, format);
218 }
219 }
220 }
221
222 #endregion
223
224
225 #region TOOLBAR EVENT HANDLERS
226
227
228 private void tsTxtCurrentPage_Leave(object sender, EventArgs e) {
229 int startPage;
230 if (Int32.TryParse(tsTxtCurrentPage.Text, out startPage)) {
231 try {
232 startPage--;
233 if (startPage < 0) startPage = 0;
234 if (startPage > mTotalPages - 1) startPage = mTotalPages - mVisibilePages;
235 printPreviewControl1.StartPage = startPage;
236 } catch { }
237 }
238 }
239
240 private void NumOfPages_Click(object sender, EventArgs e) {
241 ToolStripMenuItem mnuitem = (ToolStripMenuItem)sender;
242 tsDDownPages.Image = mnuitem.Image;
243 mVisibilePages = Int32.Parse((string)mnuitem.Tag);
244 switch (mVisibilePages) {
245 case 1:
246 printPreviewControl1.Rows = 1;
247 printPreviewControl1.Columns = 1;
248 break;
249 case 2:
250 printPreviewControl1.Rows = 1;
251 printPreviewControl1.Columns = 2;
252 break;
253 case 4:
254 printPreviewControl1.Rows = 2;
255 printPreviewControl1.Columns = 2;
256 break;
257 case 6:
258 printPreviewControl1.Rows = 2;
259 printPreviewControl1.Columns = 3;
260 break;
261 case 8:
262 printPreviewControl1.Rows = 2;
263 printPreviewControl1.Columns = 4;
264 break;
265 }
266 tsBtnZoom_Click(null, null);
267 }
268
269 // Manages the next and previous button
270 private void Navigate_Click(object sender, EventArgs e) {
271 ToolStripButton btn = (ToolStripButton)sender;
272 int startPage = printPreviewControl1.StartPage;
273 try {
274 if (btn.Name == "tsBtnNext") {
275 startPage += mVisibilePages;
276 }
277 else {
278 startPage -= mVisibilePages;
279 }
280 if (startPage < 0) startPage = 0;
281 if (startPage > mTotalPages - 1) startPage = mTotalPages - mVisibilePages;
282 printPreviewControl1.StartPage = startPage;
283
284 } catch { }
285 }
286
287 void printPreviewControl1_StartPageChanged(object sender, EventArgs e) {
288 int tmp = printPreviewControl1.StartPage + 1;
289 tsTxtCurrentPage.Text = tmp.ToString();
290 }
291
292 private void tsComboZoom_Leave(object sender, EventArgs e) {
293 if (tsComboZoom.SelectedIndex == 0) {
294 printPreviewControl1.AutoZoom = true;
295 return;
296 }
297 string sZoomVal = tsComboZoom.Text.Replace("%", "");
298 double zoomval;
299 if (double.TryParse(sZoomVal, out zoomval)) {
300 try {
301 printPreviewControl1.Zoom = zoomval / 100;
302 } catch { }
303 zoomval = (printPreviewControl1.Zoom * 100);
304 tsComboZoom.Text = zoomval.ToString() + "%";
305 }
306 }
307
308 private void tsBtnZoom_Click(object sender, EventArgs e) {
309 tsComboZoom.SelectedIndex = 0;
310 tsComboZoom_Leave(null, null);
311 }
312
313 private void tsComboZoom_KeyPress(object sender, KeyPressEventArgs e) {
314 if (e.KeyChar == 13) {
315 tsComboZoom_Leave(null, null);
316 }
317 }
318
319 private void tsBtnPrint_Click(object sender, EventArgs e) {
320 PrintDialog.Document = this.mDocument;
321 if (mShowPrinterSettingsBeforePrint) {
322 if (PrintDialog.ShowDialog() == DialogResult.OK) {
323 try {
324 mDocument.Print();
325 } catch { }
326 }
327 }
328 else {
329 try {
330 mDocument.Print();
331 } catch { }
332 }
333 }
334
335 private void tsBtnPageSettings_Click(object sender, EventArgs e) {
336 PageSetupDialog.Document = this.mDocument;
337 if (PageSetupDialog.ShowDialog() == DialogResult.OK) {
338 printPreviewControl1.InvalidatePreview();
339 }
340 }
341
342 private void tsBtnPrinterSettings_Click(object sender, EventArgs e) {
343 PrintDialog.Document = this.mDocument;
344 PrintDialog.ShowDialog();
345 }
346
347 #endregion
348
349
350 private void RadPrintPreview_FormClosing(object sender, FormClosingEventArgs e) {
351 SwitchPrintDocumentHandlers(mDocument, false);
352 }
353
354 private void SwitchPrintDocumentHandlers(PrintDocument Document, bool Attach) {
355 if (Document == null) return;
356 if (Attach) {
357 mDocument.BeginPrint += new PrintEventHandler(mDocument_BeginPrint);
358 mDocument.PrintPage += new PrintPageEventHandler(mDocument_PrintPage);
359 mDocument.EndPrint += new PrintEventHandler(mDocument_EndPrint);
360 }
361 else {
362 mDocument.BeginPrint -= new PrintEventHandler(mDocument_BeginPrint);
363 mDocument.PrintPage -= new PrintPageEventHandler(mDocument_PrintPage);
364 mDocument.EndPrint -= new PrintEventHandler(mDocument_EndPrint);
365 }
366 }
367
368 private void landcapeToolStripMenuItem_Click(object sender, EventArgs e)
369 {
370 if (this.Document.DefaultPageSettings.Landscape == true) return;
371
372 this.Document.DefaultPageSettings.Landscape = true;
373 tsBtnLandScapePortrait.Text = tsBtnLandScapePortrait.DropDownItems[0].Text;
374 tsBtnLandScapePortrait.DropDownItems[0].Enabled = false; //disable LS
375 tsBtnLandScapePortrait.DropDownItems[1].Enabled = true; //enable POR
376 this.printPreviewControl1.InvalidatePreview();
377 }
378
379 private void portraitToolStripMenuItem_Click(object sender, EventArgs e)
380 {
381 if (this.Document.DefaultPageSettings.Landscape == false) return;
382
383 this.Document.DefaultPageSettings.Landscape = false;
384 tsBtnLandScapePortrait.Text = tsBtnLandScapePortrait.DropDownItems[1].Text;
385 tsBtnLandScapePortrait.DropDownItems[1].Enabled = false; //disable POR
386 tsBtnLandScapePortrait.DropDownItems[0].Enabled = true; //Enable LS
387 this.printPreviewControl1.InvalidatePreview();
388 }
389
390 private void tsBtnLandScapePortrait_ButtonClick(object sender, EventArgs e)
391 {
392 if (this.Document.DefaultPageSettings.Landscape == false) // portrait - d to Landscape
393 {
394 landcapeToolStripMenuItem_Click(sender, e);
395 }
396 else // landscape - d to portrait
397 {
398 portraitToolStripMenuItem_Click(sender, e);
399 }
400 }
401 }
402}
Note: See TracBrowser for help on using the repository browser.