Code
·
397 lines
·
10528 bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
using System.Drawing.Printing;
namespace AnnuiteitenFormApp
{
/// <summary>
/// Summary description for PrintableListView.
/// </summary>
public class PrintableListView : ListView
{
// Print fields
private PrintDocument m_printDoc = new PrintDocument();
private PageSetupDialog m_setupDlg = new PageSetupDialog();
private PrintPreviewDialog m_previewDlg = new PrintPreviewDialog();
private PrintDialog m_printDlg = new PrintDialog();
private int m_nPageNumber=1;
private int m_nStartRow=0;
private int m_nStartCol=0;
private bool m_bPrintSel=false;
private bool m_bFitToPage=false;
private float m_fListWidth=0.0f;
private float[] m_arColsWidth;
private float m_fDpi=96.0f;
private string m_strTitle="";
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
#region Properties
/// <summary>
/// Gets or sets whether to fit the list width on a single page
/// </summary>
/// <value>
/// <c>True</c> if you want to scale the list width so it will fit on a single page.
/// </value>
/// <remarks>
/// If you choose false (the default value), and the list width exceeds the page width, the list
/// will be broken in multiple page.
/// </remarks>
public bool FitToPage
{
get {return m_bFitToPage;}
set {m_bFitToPage=value;}
}
/// <summary>
/// Gets or sets the title to dispaly as page header in the printed list
/// </summary>
/// <value>
/// A <see cref="string"/> the represents the title printed as page header.
/// </value>
public string Title
{
get {return m_strTitle;}
set {m_strTitle=value;}
}
#endregion
public PrintableListView()
{
// This call is required by the Windows.Forms Form Designer.
InitializeComponent();
m_printDoc.BeginPrint += new PrintEventHandler(OnBeginPrint);
m_printDoc.PrintPage += new PrintPageEventHandler(OnPrintPage);
m_setupDlg.Document = m_printDoc;
m_previewDlg.Document = m_printDoc;
m_printDlg.Document = m_printDoc;
m_printDlg.AllowSomePages = false;
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if( components != null )
components.Dispose();
}
base.Dispose( disposing );
}
#region Component Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
components = new System.ComponentModel.Container();
}
#endregion
/// <summary>
/// Show the standard page setup dialog box that lets the user specify
/// margins, page orientation, page sources, and paper sizes.
/// </summary>
public void PageSetup()
{
m_setupDlg.ShowDialog();
}
/// <summary>
/// Show the standard print preview dialog box.
/// </summary>
public void PrintPreview()
{
m_printDoc.DocumentName = "List View";
m_nPageNumber = 1;
m_bPrintSel = false;
m_previewDlg.ShowDialog(this);
}
/// <summary>
/// Start the print process.
/// </summary>
public void Print()
{
m_printDlg.AllowSelection = this.SelectedItems.Count>0;
// Show the standard print dialog box, that lets the user select a printer
// and change the settings for that printer.
if (m_printDlg.ShowDialog(this) == DialogResult.OK)
{
m_printDoc.DocumentName = m_strTitle;
m_bPrintSel = m_printDlg.PrinterSettings.PrintRange==PrintRange.Selection;
m_nPageNumber = 1;
// Start print
m_printDoc.Print();
}
}
#region Events Handlers
private void OnBeginPrint(object sender, PrintEventArgs e)
{
PreparePrint();
}
private void OnPrintPage(object sender, PrintPageEventArgs e)
{
int nNumItems = GetItemsCount(); // Number of items to print
if (nNumItems==0 || m_nStartRow>=nNumItems)
{
e.HasMorePages = false;
return;
}
int nNextStartCol = 0; // First column exeeding the page width
float x = 0.0f; // Current horizontal coordinate
float y = 0.0f; // Current vertical coordinate
float cx = 4.0f; // The horizontal space, in hundredths of an inch,
// of the padding between items text and
// their cell boundaries.
float fScale = 1.0f; // Scale factor when fit to page is enabled
float fRowHeight = 0.0f; // The height of the current row
float fColWidth = 0.0f; // The width of the current column
RectangleF rectFull; // The full available space
RectangleF rectBody; // Area for the list items
bool bUnprintable = false;
Graphics g = e.Graphics;
if (g.VisibleClipBounds.X<0) // Print preview
{
rectFull = e.MarginBounds;
// Convert to hundredths of an inch
rectFull = new RectangleF(rectFull.X/m_fDpi*100.0f,
rectFull.Y/m_fDpi*100.0f,
rectFull.Width/m_fDpi*100.0f,
rectFull.Height/m_fDpi*100.0f);
}
else // Print
{
// Printable area (approximately) of the page, taking into account the user margins
rectFull = new RectangleF(
e.MarginBounds.Left - (e.PageBounds.Width - g.VisibleClipBounds.Width)/2,
e.MarginBounds.Top - (e.PageBounds.Height - g.VisibleClipBounds.Height)/2,
e.MarginBounds.Width,
e.MarginBounds.Height);
}
rectBody = RectangleF.Inflate(rectFull, 0, -2*Font.GetHeight(g));
// Display title at top
StringFormat sfmt = new StringFormat();
sfmt.Alignment = StringAlignment.Center;
g.DrawString(m_strTitle, Font, Brushes.Black, rectFull, sfmt);
// Display page number at bottom
sfmt.LineAlignment = StringAlignment.Far;
g.DrawString("Page " + m_nPageNumber, Font, Brushes.Black, rectFull, sfmt);
if (m_nStartCol==0 && m_bFitToPage && m_fListWidth>rectBody.Width)
{
// Calculate scale factor
fScale = rectBody.Width / m_fListWidth;
}
// Scale the printable area
rectFull = new RectangleF(rectFull.X/fScale,
rectFull.Y/fScale,
rectFull.Width/fScale,
rectFull.Height/fScale);
rectBody = new RectangleF(rectBody.X/fScale,
rectBody.Y/fScale,
rectBody.Width/fScale,
rectBody.Height/fScale);
// Setting scale factor and unit of measure
g.ScaleTransform(fScale, fScale);
g.PageUnit = GraphicsUnit.Inch;
g.PageScale = 0.01f;
// Start print
nNextStartCol=0;
y = rectBody.Top;
// Columns headers ----------------------------------------
Brush brushHeader = new SolidBrush(Color.LightGray);
Font fontHeader = new Font(this.Font, FontStyle.Bold);
fRowHeight = fontHeader.GetHeight(g)*3.0f;
x = rectBody.Left;
for (int i=m_nStartCol; i<Columns.Count; i++)
{
ColumnHeader ch = Columns[i];
fColWidth = m_arColsWidth[i];
if ( (x + fColWidth) <= rectBody.Right)
{
// Rectangle
g.FillRectangle(brushHeader, x, y, fColWidth, fRowHeight);
g.DrawRectangle(Pens.Black, x, y, fColWidth, fRowHeight);
// Text
StringFormat sf = new StringFormat();
if (ch.TextAlign == HorizontalAlignment.Left)
sf.Alignment = StringAlignment.Near;
else if (ch.TextAlign == HorizontalAlignment.Center)
sf.Alignment = StringAlignment.Center;
else
sf.Alignment = StringAlignment.Far;
sf.LineAlignment = StringAlignment.Center;
sf.FormatFlags = StringFormatFlags.NoWrap;
sf.Trimming = StringTrimming.EllipsisCharacter;
RectangleF rectText = new RectangleF(x+cx, y, fColWidth-1-2*cx, fRowHeight);
g.DrawString(ch.Text, fontHeader, Brushes.Black, rectText, sf);
x += fColWidth;
}
else
{
if (i==m_nStartCol)
bUnprintable=true;
nNextStartCol=i;
break;
}
}
y += fRowHeight;
// Rows ---------------------------------------------------
int nRow = m_nStartRow;
bool bEndOfPage = false;
while (!bEndOfPage && nRow<nNumItems)
{
ListViewItem item = GetItem(nRow);
fRowHeight = item.Bounds.Height/m_fDpi*100.0f + 5.0f;
if (y+fRowHeight>rectBody.Bottom)
{
bEndOfPage=true;
}
else
{
x = rectBody.Left;
for (int i=m_nStartCol; i<Columns.Count; i++)
{
ColumnHeader ch = Columns[i];
fColWidth = m_arColsWidth[i];
if ( (x + fColWidth) <= rectBody.Right)
{
// Rectangle
g.DrawRectangle(Pens.Black, x, y, fColWidth, fRowHeight);
// Text
StringFormat sf = new StringFormat();
if (ch.TextAlign == HorizontalAlignment.Left)
sf.Alignment = StringAlignment.Near;
else if (ch.TextAlign == HorizontalAlignment.Center)
sf.Alignment = StringAlignment.Center;
else
sf.Alignment = StringAlignment.Far;
sf.LineAlignment = StringAlignment.Center;
sf.FormatFlags = StringFormatFlags.NoWrap;
sf.Trimming = StringTrimming.EllipsisCharacter;
// Text
string strText = i==0 ? item.Text : item.SubItems[i].Text;
Font font = i==0 ? item.Font : item.SubItems[i].Font;
RectangleF rectText = new RectangleF(x+cx, y, fColWidth-1-2*cx, fRowHeight);
g.DrawString(strText, font, Brushes.Black, rectText, sf);
x += fColWidth;
}
else
{
nNextStartCol=i;
break;
}
}
y += fRowHeight;
nRow++;
}
}
if (nNextStartCol==0)
m_nStartRow = nRow;
m_nStartCol = nNextStartCol;
m_nPageNumber++;
e.HasMorePages = (!bUnprintable && (m_nStartRow>0 && m_nStartRow<nNumItems) || m_nStartCol>0);
if (!e.HasMorePages)
{
m_nPageNumber=1;
m_nStartRow=0;
m_nStartCol=0;
}
brushHeader.Dispose();
}
#endregion
private int GetItemsCount()
{
return m_bPrintSel ? SelectedItems.Count : Items.Count;
}
private ListViewItem GetItem(int index)
{
return m_bPrintSel ? SelectedItems[index] : Items[index];
}
private void PreparePrint()
{
// Gets the list width and the columns width in units of hundredths of an inch.
this.m_fListWidth = 0.0f;
this.m_arColsWidth = new float[this.Columns.Count];
Graphics g = CreateGraphics();
m_fDpi = g.DpiX;
g.Dispose();
for (int i=0; i<Columns.Count; i++)
{
ColumnHeader ch = Columns[i];
float fWidth = ch.Width/m_fDpi*100 + 1; // Column width + separator
m_fListWidth += fWidth;
m_arColsWidth[i] = fWidth;
}
m_fListWidth += 1; // separator
}
}
}