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
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
|
// Windows Template Library - WTL version 8.1
// Copyright (C) Microsoft Corporation. All rights reserved.
//
// This file is a part of the Windows Template Library.
// The use and distribution terms for this software are covered by the
// Common Public License 1.0 (http://opensource.org/licenses/cpl1.0.php)
// which can be found in the file CPL.TXT at the root of this distribution.
// By using this software in any fashion, you are agreeing to be bound by
// the terms of this license. You must not remove this notice, or
// any other, from this software.
#ifndef __ATLPRINT_H__
#define __ATLPRINT_H__
#pragma once
#ifdef _WIN32_WCE
#error atlprint.h is not supported on Windows CE
#endif
#ifndef __ATLAPP_H__
#error atlprint.h requires atlapp.h to be included first
#endif
#ifndef __ATLWIN_H__
#error atlprint.h requires atlwin.h to be included first
#endif
///////////////////////////////////////////////////////////////////////////////
// Classes in this file:
//
// CPrinterInfo<t_nInfo>
// CPrinterT<t_bManaged>
// CDevModeT<t_bManaged>
// CPrinterDC
// CPrintJobInfo
// CPrintJob
// CPrintPreview
// CPrintPreviewWindowImpl<T, TBase, TWinTraits>
// CPrintPreviewWindow
// CZoomPrintPreviewWindowImpl<T, TBase, TWinTraits>
// CZoomPrintPreviewWindow
namespace WTL
{
///////////////////////////////////////////////////////////////////////////////
// CPrinterInfo - This class wraps all of the PRINTER_INFO_* structures
// and provided by ::GetPrinter.
template <unsigned int t_nInfo>
class _printer_info
{
public:
typedef void infotype;
};
template <> class _printer_info<1> { public: typedef PRINTER_INFO_1 infotype; };
template <> class _printer_info<2> { public: typedef PRINTER_INFO_2 infotype; };
template <> class _printer_info<3> { public: typedef PRINTER_INFO_3 infotype; };
template <> class _printer_info<4> { public: typedef PRINTER_INFO_4 infotype; };
template <> class _printer_info<5> { public: typedef PRINTER_INFO_5 infotype; };
template <> class _printer_info<6> { public: typedef PRINTER_INFO_6 infotype; };
template <> class _printer_info<7> { public: typedef PRINTER_INFO_7 infotype; };
// these are not in the old (vc6.0) headers
#ifdef _ATL_USE_NEW_PRINTER_INFO
template <> class _printer_info<8> { public: typedef PRINTER_INFO_8 infotype; };
template <> class _printer_info<9> { public: typedef PRINTER_INFO_9 infotype; };
#endif // _ATL_USE_NEW_PRINTER_INFO
template <unsigned int t_nInfo>
class CPrinterInfo
{
public:
// Data members
typename _printer_info<t_nInfo>::infotype* m_pi;
// Constructor/destructor
CPrinterInfo() : m_pi(NULL)
{ }
CPrinterInfo(HANDLE hPrinter) : m_pi(NULL)
{
GetPrinterInfo(hPrinter);
}
~CPrinterInfo()
{
Cleanup();
}
// Operations
bool GetPrinterInfo(HANDLE hPrinter)
{
Cleanup();
return GetPrinterInfoHelper(hPrinter, (BYTE**)&m_pi, t_nInfo);
}
// Implementation
void Cleanup()
{
delete [] (BYTE*)m_pi;
m_pi = NULL;
}
static bool GetPrinterInfoHelper(HANDLE hPrinter, BYTE** pi, int nIndex)
{
ATLASSERT(pi != NULL);
DWORD dw = 0;
BYTE* pb = NULL;
::GetPrinter(hPrinter, nIndex, NULL, 0, &dw);
if (dw > 0)
{
ATLTRY(pb = new BYTE[dw]);
if (pb != NULL)
{
memset(pb, 0, dw);
DWORD dwNew;
if (!::GetPrinter(hPrinter, nIndex, pb, dw, &dwNew))
{
delete [] pb;
pb = NULL;
}
}
}
*pi = pb;
return (pb != NULL);
}
};
///////////////////////////////////////////////////////////////////////////////
// CPrinter - Wrapper class for a HANDLE to a printer
template <bool t_bManaged>
class CPrinterT
{
public:
// Data members
HANDLE m_hPrinter;
// Constructor/destructor
CPrinterT(HANDLE hPrinter = NULL) : m_hPrinter(hPrinter)
{ }
~CPrinterT()
{
ClosePrinter();
}
// Operations
CPrinterT& operator =(HANDLE hPrinter)
{
if (hPrinter != m_hPrinter)
{
ClosePrinter();
m_hPrinter = hPrinter;
}
return *this;
}
bool IsNull() const { return (m_hPrinter == NULL); }
bool OpenPrinter(HANDLE hDevNames, const DEVMODE* pDevMode = NULL)
{
bool b = false;
DEVNAMES* pdn = (DEVNAMES*)::GlobalLock(hDevNames);
if (pdn != NULL)
{
LPTSTR lpszPrinterName = (LPTSTR)pdn + pdn->wDeviceOffset;
b = OpenPrinter(lpszPrinterName, pDevMode);
::GlobalUnlock(hDevNames);
}
return b;
}
bool OpenPrinter(LPCTSTR lpszPrinterName, const DEVMODE* pDevMode = NULL)
{
ClosePrinter();
PRINTER_DEFAULTS pdefs = { NULL, (DEVMODE*)pDevMode, PRINTER_ACCESS_USE };
::OpenPrinter((LPTSTR) lpszPrinterName, &m_hPrinter, (pDevMode == NULL) ? NULL : &pdefs);
return (m_hPrinter != NULL);
}
bool OpenPrinter(LPCTSTR lpszPrinterName, PRINTER_DEFAULTS* pprintdefs)
{
ClosePrinter();
::OpenPrinter((LPTSTR) lpszPrinterName, &m_hPrinter, pprintdefs);
return (m_hPrinter != NULL);
}
bool OpenDefaultPrinter(const DEVMODE* pDevMode = NULL)
{
ClosePrinter();
const int cchBuff = 512;
TCHAR buffer[cchBuff];
buffer[0] = 0;
::GetProfileString(_T("windows"), _T("device"), _T(",,,"), buffer, cchBuff);
int nLen = lstrlen(buffer);
if (nLen != 0)
{
LPTSTR lpsz = buffer;
while (*lpsz)
{
if (*lpsz == _T(','))
{
*lpsz = 0;
break;
}
lpsz = CharNext(lpsz);
}
PRINTER_DEFAULTS pdefs = { NULL, (DEVMODE*)pDevMode, PRINTER_ACCESS_USE };
::OpenPrinter(buffer, &m_hPrinter, (pDevMode == NULL) ? NULL : &pdefs);
}
return m_hPrinter != NULL;
}
void ClosePrinter()
{
if (m_hPrinter != NULL)
{
if (t_bManaged)
::ClosePrinter(m_hPrinter);
m_hPrinter = NULL;
}
}
bool PrinterProperties(HWND hWnd = NULL)
{
if (hWnd == NULL)
hWnd = ::GetActiveWindow();
return !!::PrinterProperties(hWnd, m_hPrinter);
}
HANDLE CopyToHDEVNAMES() const
{
HANDLE h = NULL;
CPrinterInfo<5> pinfon5;
CPrinterInfo<2> pinfon2;
LPTSTR lpszPrinterName = NULL;
// Some printers fail for PRINTER_INFO_5 in some situations
if (pinfon5.GetPrinterInfo(m_hPrinter))
lpszPrinterName = pinfon5.m_pi->pPrinterName;
else if (pinfon2.GetPrinterInfo(m_hPrinter))
lpszPrinterName = pinfon2.m_pi->pPrinterName;
if (lpszPrinterName != NULL)
{
int nLen = sizeof(DEVNAMES) + (lstrlen(lpszPrinterName) + 1) * sizeof(TCHAR);
h = ::GlobalAlloc(GMEM_MOVEABLE, nLen);
BYTE* pv = (BYTE*)::GlobalLock(h);
DEVNAMES* pdev = (DEVNAMES*)pv;
if (pv != NULL)
{
memset(pv, 0, nLen);
pdev->wDeviceOffset = sizeof(DEVNAMES) / sizeof(TCHAR);
pv = pv + sizeof(DEVNAMES); // now points to end
SecureHelper::strcpy_x((LPTSTR)pv, lstrlen(lpszPrinterName) + 1, lpszPrinterName);
::GlobalUnlock(h);
}
}
return h;
}
HDC CreatePrinterDC(const DEVMODE* pdm = NULL) const
{
CPrinterInfo<5> pinfo5;
CPrinterInfo<2> pinfo2;
HDC hDC = NULL;
LPTSTR lpszPrinterName = NULL;
// Some printers fail for PRINTER_INFO_5 in some situations
if (pinfo5.GetPrinterInfo(m_hPrinter))
lpszPrinterName = pinfo5.m_pi->pPrinterName;
else if (pinfo2.GetPrinterInfo(m_hPrinter))
lpszPrinterName = pinfo2.m_pi->pPrinterName;
if (lpszPrinterName != NULL)
hDC = ::CreateDC(NULL, lpszPrinterName, NULL, pdm);
return hDC;
}
HDC CreatePrinterIC(const DEVMODE* pdm = NULL) const
{
CPrinterInfo<5> pinfo5;
CPrinterInfo<2> pinfo2;
HDC hDC = NULL;
LPTSTR lpszPrinterName = NULL;
// Some printers fail for PRINTER_INFO_5 in some situations
if (pinfo5.GetPrinterInfo(m_hPrinter))
lpszPrinterName = pinfo5.m_pi->pPrinterName;
else if (pinfo2.GetPrinterInfo(m_hPrinter))
lpszPrinterName = pinfo2.m_pi->pPrinterName;
if (lpszPrinterName != NULL)
hDC = ::CreateIC(NULL, lpszPrinterName, NULL, pdm);
return hDC;
}
void Attach(HANDLE hPrinter)
{
ClosePrinter();
m_hPrinter = hPrinter;
}
HANDLE Detach()
{
HANDLE hPrinter = m_hPrinter;
m_hPrinter = NULL;
return hPrinter;
}
operator HANDLE() const { return m_hPrinter; }
};
typedef CPrinterT<false> CPrinterHandle;
typedef CPrinterT<true> CPrinter;
///////////////////////////////////////////////////////////////////////////////
// CDevMode - Wrapper class for DEVMODE
template <bool t_bManaged>
class CDevModeT
{
public:
// Data members
HANDLE m_hDevMode;
DEVMODE* m_pDevMode;
// Constructor/destructor
CDevModeT(HANDLE hDevMode = NULL) : m_hDevMode(hDevMode)
{
m_pDevMode = (m_hDevMode != NULL) ? (DEVMODE*)::GlobalLock(m_hDevMode) : NULL;
}
~CDevModeT()
{
Cleanup();
}
// Operations
CDevModeT<t_bManaged>& operator =(HANDLE hDevMode)
{
Attach(hDevMode);
return *this;
}
void Attach(HANDLE hDevModeNew)
{
Cleanup();
m_hDevMode = hDevModeNew;
m_pDevMode = (m_hDevMode != NULL) ? (DEVMODE*)::GlobalLock(m_hDevMode) : NULL;
}
HANDLE Detach()
{
if (m_hDevMode != NULL)
::GlobalUnlock(m_hDevMode);
HANDLE hDevMode = m_hDevMode;
m_hDevMode = NULL;
return hDevMode;
}
bool IsNull() const { return (m_hDevMode == NULL); }
bool CopyFromPrinter(HANDLE hPrinter)
{
CPrinterInfo<2> pinfo;
bool b = pinfo.GetPrinterInfo(hPrinter);
if (b)
b = CopyFromDEVMODE(pinfo.m_pi->pDevMode);
return b;
}
bool CopyFromDEVMODE(const DEVMODE* pdm)
{
if (pdm == NULL)
return false;
int nSize = pdm->dmSize + pdm->dmDriverExtra;
HANDLE h = ::GlobalAlloc(GMEM_MOVEABLE, nSize);
if (h != NULL)
{
void* p = ::GlobalLock(h);
SecureHelper::memcpy_x(p, nSize, pdm, nSize);
::GlobalUnlock(h);
}
Attach(h);
return (h != NULL);
}
bool CopyFromHDEVMODE(HANDLE hdm)
{
bool b = false;
if (hdm != NULL)
{
DEVMODE* pdm = (DEVMODE*)::GlobalLock(hdm);
b = CopyFromDEVMODE(pdm);
::GlobalUnlock(hdm);
}
return b;
}
HANDLE CopyToHDEVMODE()
{
if ((m_hDevMode == NULL) || (m_pDevMode == NULL))
return NULL;
int nSize = m_pDevMode->dmSize + m_pDevMode->dmDriverExtra;
HANDLE h = ::GlobalAlloc(GMEM_MOVEABLE, nSize);
if (h != NULL)
{
void* p = ::GlobalLock(h);
SecureHelper::memcpy_x(p, nSize, m_pDevMode, nSize);
::GlobalUnlock(h);
}
return h;
}
// If this devmode was for another printer, this will create a new devmode
// based on the existing devmode, but retargeted at the new printer
bool UpdateForNewPrinter(HANDLE hPrinter)
{
bool bRet = false;
LONG nLen = ::DocumentProperties(NULL, hPrinter, NULL, NULL, NULL, 0);
CTempBuffer<DEVMODE, _WTL_STACK_ALLOC_THRESHOLD> buff;
DEVMODE* pdm = buff.AllocateBytes(nLen);
if(pdm != NULL)
{
memset(pdm, 0, nLen);
LONG l = ::DocumentProperties(NULL, hPrinter, NULL, pdm, m_pDevMode, DM_IN_BUFFER | DM_OUT_BUFFER);
if (l == IDOK)
bRet = CopyFromDEVMODE(pdm);
}
return bRet;
}
bool DocumentProperties(HANDLE hPrinter, HWND hWnd = NULL)
{
CPrinterInfo<1> pi;
pi.GetPrinterInfo(hPrinter);
if (hWnd == NULL)
hWnd = ::GetActiveWindow();
bool bRet = false;
LONG nLen = ::DocumentProperties(hWnd, hPrinter, pi.m_pi->pName, NULL, NULL, 0);
CTempBuffer<DEVMODE, _WTL_STACK_ALLOC_THRESHOLD> buff;
DEVMODE* pdm = buff.AllocateBytes(nLen);
if(pdm != NULL)
{
memset(pdm, 0, nLen);
LONG l = ::DocumentProperties(hWnd, hPrinter, pi.m_pi->pName, pdm, m_pDevMode, DM_IN_BUFFER | DM_OUT_BUFFER | DM_PROMPT);
if (l == IDOK)
bRet = CopyFromDEVMODE(pdm);
}
return bRet;
}
operator HANDLE() const { return m_hDevMode; }
operator DEVMODE*() const { return m_pDevMode; }
// Implementation
void Cleanup()
{
if (m_hDevMode != NULL)
{
::GlobalUnlock(m_hDevMode);
if(t_bManaged)
::GlobalFree(m_hDevMode);
m_hDevMode = NULL;
}
}
};
typedef CDevModeT<false> CDevModeHandle;
typedef CDevModeT<true> CDevMode;
///////////////////////////////////////////////////////////////////////////////
// CPrinterDC
class CPrinterDC : public CDC
{
public:
// Constructors/destructor
CPrinterDC()
{
CPrinter printer;
printer.OpenDefaultPrinter();
Attach(printer.CreatePrinterDC());
ATLASSERT(m_hDC != NULL);
}
CPrinterDC(HANDLE hPrinter, const DEVMODE* pdm = NULL)
{
CPrinterHandle p;
p.Attach(hPrinter);
Attach(p.CreatePrinterDC(pdm));
ATLASSERT(m_hDC != NULL);
}
~CPrinterDC()
{
DeleteDC();
}
};
///////////////////////////////////////////////////////////////////////////////
// CPrintJob - Wraps a set of tasks for a specific printer (StartDoc/EndDoc)
// Handles aborting, background printing
// Defines callbacks used by CPrintJob (not a COM interface)
class ATL_NO_VTABLE IPrintJobInfo
{
public:
virtual void BeginPrintJob(HDC hDC) = 0; // allocate handles needed, etc.
virtual void EndPrintJob(HDC hDC, bool bAborted) = 0; // free handles, etc.
virtual void PrePrintPage(UINT nPage, HDC hDC) = 0;
virtual bool PrintPage(UINT nPage, HDC hDC) = 0;
virtual void PostPrintPage(UINT nPage, HDC hDC) = 0;
// If you want per page devmodes, return the DEVMODE* to use for nPage.
// You can optimize by only returning a new DEVMODE* when it is different
// from the one for nLastPage, otherwise return NULL.
// When nLastPage==0, the current DEVMODE* will be the default passed to
// StartPrintJob.
// Note: During print preview, nLastPage will always be "0".
virtual DEVMODE* GetNewDevModeForPage(UINT nLastPage, UINT nPage) = 0;
virtual bool IsValidPage(UINT nPage) = 0;
};
// Provides a default implementatin for IPrintJobInfo
// Typically, MI'd into a document or view class
class ATL_NO_VTABLE CPrintJobInfo : public IPrintJobInfo
{
public:
virtual void BeginPrintJob(HDC /*hDC*/) // allocate handles needed, etc
{
}
virtual void EndPrintJob(HDC /*hDC*/, bool /*bAborted*/) // free handles, etc
{
}
virtual void PrePrintPage(UINT /*nPage*/, HDC hDC)
{
m_nPJState = ::SaveDC(hDC);
}
virtual bool PrintPage(UINT /*nPage*/, HDC /*hDC*/) = 0;
virtual void PostPrintPage(UINT /*nPage*/, HDC hDC)
{
RestoreDC(hDC, m_nPJState);
}
virtual DEVMODE* GetNewDevModeForPage(UINT /*nLastPage*/, UINT /*nPage*/)
{
return NULL;
}
virtual bool IsValidPage(UINT /*nPage*/)
{
return true;
}
// Implementation - data
int m_nPJState;
};
class CPrintJob
{
public:
// Data members
CPrinterHandle m_printer;
IPrintJobInfo* m_pInfo;
DEVMODE* m_pDefDevMode;
DOCINFO m_docinfo;
int m_nJobID;
bool m_bCancel;
bool m_bComplete;
unsigned long m_nStartPage;
unsigned long m_nEndPage;
// Constructor/destructor
CPrintJob() : m_nJobID(0), m_bCancel(false), m_bComplete(true)
{ }
~CPrintJob()
{
ATLASSERT(IsJobComplete()); // premature destruction?
}
// Operations
bool IsJobComplete() const
{
return m_bComplete;
}
bool StartPrintJob(bool bBackground, HANDLE hPrinter, DEVMODE* pDefaultDevMode,
IPrintJobInfo* pInfo, LPCTSTR lpszDocName,
unsigned long nStartPage, unsigned long nEndPage,
bool bPrintToFile = false, LPCTSTR lpstrOutputFile = NULL)
{
ATLASSERT(m_bComplete); // previous job not done yet?
if (pInfo == NULL)
return false;
memset(&m_docinfo, 0, sizeof(m_docinfo));
m_docinfo.cbSize = sizeof(m_docinfo);
m_docinfo.lpszDocName = lpszDocName;
m_pInfo = pInfo;
m_nStartPage = nStartPage;
m_nEndPage = nEndPage;
m_printer.Attach(hPrinter);
m_pDefDevMode = pDefaultDevMode;
m_bComplete = false;
if(bPrintToFile)
m_docinfo.lpszOutput = (lpstrOutputFile != NULL) ? lpstrOutputFile : _T("FILE:");
if (!bBackground)
{
m_bComplete = true;
return StartHelper();
}
// Create a thread and return
DWORD dwThreadID = 0;
#if !defined(_ATL_MIN_CRT) && defined(_MT)
HANDLE hThread = (HANDLE)_beginthreadex(NULL, 0, (UINT (WINAPI*)(void*))StartProc, this, 0, (UINT*)&dwThreadID);
#else
HANDLE hThread = ::CreateThread(NULL, 0, StartProc, (void*)this, 0, &dwThreadID);
#endif
if (hThread == NULL)
return false;
::CloseHandle(hThread);
return true;
}
// Implementation
static DWORD WINAPI StartProc(void* p)
{
CPrintJob* pThis = (CPrintJob*)p;
pThis->StartHelper();
pThis->m_bComplete = true;
return 0;
}
bool StartHelper()
{
CDC dcPrinter;
dcPrinter.Attach(m_printer.CreatePrinterDC(m_pDefDevMode));
if (dcPrinter.IsNull())
return false;
m_nJobID = ::StartDoc(dcPrinter, &m_docinfo);
if (m_nJobID <= 0)
return false;
m_pInfo->BeginPrintJob(dcPrinter);
// print all the pages now
unsigned long nLastPage = 0;
for (unsigned long nPage = m_nStartPage; nPage <= m_nEndPage; nPage++)
{
if (!m_pInfo->IsValidPage(nPage))
break;
DEVMODE* pdm = m_pInfo->GetNewDevModeForPage(nLastPage, nPage);
if (pdm != NULL)
dcPrinter.ResetDC(pdm);
dcPrinter.StartPage();
m_pInfo->PrePrintPage(nPage, dcPrinter);
if (!m_pInfo->PrintPage(nPage, dcPrinter))
m_bCancel = true;
m_pInfo->PostPrintPage(nPage, dcPrinter);
dcPrinter.EndPage();
if (m_bCancel)
break;
nLastPage = nPage;
}
m_pInfo->EndPrintJob(dcPrinter, m_bCancel);
if (m_bCancel)
::AbortDoc(dcPrinter);
else
::EndDoc(dcPrinter);
m_nJobID = 0;
return true;
}
// Cancels a print job. Can be called asynchronously.
void CancelPrintJob()
{
m_bCancel = true;
}
};
///////////////////////////////////////////////////////////////////////////////
// CPrintPreview - Adds print preview support to an existing window
class CPrintPreview
{
public:
// Data members
IPrintJobInfo* m_pInfo;
CPrinterHandle m_printer;
CEnhMetaFile m_meta;
DEVMODE* m_pDefDevMode;
DEVMODE* m_pCurDevMode;
SIZE m_sizeCurPhysOffset;
// Constructor
CPrintPreview() : m_pInfo(NULL), m_pDefDevMode(NULL), m_pCurDevMode(NULL)
{
m_sizeCurPhysOffset.cx = 0;
m_sizeCurPhysOffset.cy = 0;
}
// Operations
void SetPrintPreviewInfo(HANDLE hPrinter, DEVMODE* pDefaultDevMode, IPrintJobInfo* pji)
{
m_printer.Attach(hPrinter);
m_pDefDevMode = pDefaultDevMode;
m_pInfo = pji;
m_nCurPage = 0;
m_pCurDevMode = NULL;
}
void SetEnhMetaFile(HENHMETAFILE hEMF)
{
m_meta = hEMF;
}
void SetPage(int nPage)
{
if (!m_pInfo->IsValidPage(nPage))
return;
m_nCurPage = nPage;
m_pCurDevMode = m_pInfo->GetNewDevModeForPage(0, nPage);
if (m_pCurDevMode == NULL)
m_pCurDevMode = m_pDefDevMode;
CDC dcPrinter = m_printer.CreatePrinterDC(m_pCurDevMode);
int iWidth = dcPrinter.GetDeviceCaps(PHYSICALWIDTH);
int iHeight = dcPrinter.GetDeviceCaps(PHYSICALHEIGHT);
int nLogx = dcPrinter.GetDeviceCaps(LOGPIXELSX);
int nLogy = dcPrinter.GetDeviceCaps(LOGPIXELSY);
RECT rcMM = { 0, 0, ::MulDiv(iWidth, 2540, nLogx), ::MulDiv(iHeight, 2540, nLogy) };
m_sizeCurPhysOffset.cx = dcPrinter.GetDeviceCaps(PHYSICALOFFSETX);
m_sizeCurPhysOffset.cy = dcPrinter.GetDeviceCaps(PHYSICALOFFSETY);
CEnhMetaFileDC dcMeta(dcPrinter, &rcMM);
m_pInfo->PrePrintPage(nPage, dcMeta);
m_pInfo->PrintPage(nPage, dcMeta);
m_pInfo->PostPrintPage(nPage, dcMeta);
m_meta.Attach(dcMeta.Close());
}
void GetPageRect(RECT& rc, LPRECT prc)
{
int x1 = rc.right-rc.left;
int y1 = rc.bottom - rc.top;
if ((x1 < 0) || (y1 < 0))
return;
CEnhMetaFileInfo emfinfo(m_meta);
ENHMETAHEADER* pmh = emfinfo.GetEnhMetaFileHeader();
// Compute whether we are OK vertically or horizontally
int x2 = pmh->szlDevice.cx;
int y2 = pmh->szlDevice.cy;
int y1p = MulDiv(x1, y2, x2);
int x1p = MulDiv(y1, x2, y2);
ATLASSERT((x1p <= x1) || (y1p <= y1));
if (x1p <= x1)
{
prc->left = rc.left + (x1 - x1p) / 2;
prc->right = prc->left + x1p;
prc->top = rc.top;
prc->bottom = rc.bottom;
}
else
{
prc->left = rc.left;
prc->right = rc.right;
prc->top = rc.top + (y1 - y1p) / 2;
prc->bottom = prc->top + y1p;
}
}
// Painting helpers
void DoPaint(CDCHandle dc)
{
// this one is not used
}
void DoPaint(CDCHandle dc, RECT& rc)
{
CEnhMetaFileInfo emfinfo(m_meta);
ENHMETAHEADER* pmh = emfinfo.GetEnhMetaFileHeader();
int nOffsetX = MulDiv(m_sizeCurPhysOffset.cx, rc.right-rc.left, pmh->szlDevice.cx);
int nOffsetY = MulDiv(m_sizeCurPhysOffset.cy, rc.bottom-rc.top, pmh->szlDevice.cy);
dc.OffsetWindowOrg(-nOffsetX, -nOffsetY);
dc.PlayMetaFile(m_meta, &rc);
}
// Implementation - data
int m_nCurPage;
};
///////////////////////////////////////////////////////////////////////////////
// CPrintPreviewWindow - Implements a print preview window
template <class T, class TBase = ATL::CWindow, class TWinTraits = ATL::CControlWinTraits>
class ATL_NO_VTABLE CPrintPreviewWindowImpl : public ATL::CWindowImpl<T, TBase, TWinTraits>, public CPrintPreview
{
public:
DECLARE_WND_CLASS_EX(NULL, CS_VREDRAW | CS_HREDRAW, -1)
enum { m_cxOffset = 10, m_cyOffset = 10 };
// Constructor
CPrintPreviewWindowImpl() : m_nMaxPage(0), m_nMinPage(0)
{ }
// Operations
void SetPrintPreviewInfo(HANDLE hPrinter, DEVMODE* pDefaultDevMode,
IPrintJobInfo* pji, int nMinPage, int nMaxPage)
{
CPrintPreview::SetPrintPreviewInfo(hPrinter, pDefaultDevMode, pji);
m_nMinPage = nMinPage;
m_nMaxPage = nMaxPage;
}
bool NextPage()
{
if (m_nCurPage == m_nMaxPage)
return false;
SetPage(m_nCurPage + 1);
Invalidate();
return true;
}
bool PrevPage()
{
if (m_nCurPage == m_nMinPage)
return false;
if (m_nCurPage == 0)
return false;
SetPage(m_nCurPage - 1);
Invalidate();
return true;
}
// Message map and handlers
BEGIN_MSG_MAP(CPrintPreviewWindowImpl)
MESSAGE_HANDLER(WM_ERASEBKGND, OnEraseBkgnd)
MESSAGE_HANDLER(WM_PAINT, OnPaint)
MESSAGE_HANDLER(WM_PRINTCLIENT, OnPaint)
END_MSG_MAP()
LRESULT OnEraseBkgnd(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
{
return 1; // no need for the background
}
LRESULT OnPaint(UINT /*uMsg*/, WPARAM wParam, LPARAM /*lParam*/, BOOL& /*bHandled*/)
{
T* pT = static_cast<T*>(this);
RECT rc = { 0 };
if(wParam != NULL)
{
pT->DoPrePaint((HDC)wParam, rc);
pT->DoPaint((HDC)wParam, rc);
}
else
{
CPaintDC dc(m_hWnd);
pT->DoPrePaint(dc.m_hDC, rc);
pT->DoPaint(dc.m_hDC, rc);
}
return 0;
}
// Painting helper
void DoPrePaint(CDCHandle dc, RECT& rc)
{
RECT rcClient = { 0 };
GetClientRect(&rcClient);
RECT rcArea = rcClient;
T* pT = static_cast<T*>(this);
pT; // avoid level 4 warning
::InflateRect(&rcArea, -pT->m_cxOffset, -pT->m_cyOffset);
if (rcArea.left > rcArea.right)
rcArea.right = rcArea.left;
if (rcArea.top > rcArea.bottom)
rcArea.bottom = rcArea.top;
GetPageRect(rcArea, &rc);
CRgn rgn1, rgn2;
rgn1.CreateRectRgnIndirect(&rc);
rgn2.CreateRectRgnIndirect(&rcClient);
rgn2.CombineRgn(rgn1, RGN_DIFF);
dc.SelectClipRgn(rgn2);
dc.FillRect(&rcClient, COLOR_BTNSHADOW);
dc.SelectClipRgn(NULL);
dc.FillRect(&rc, (HBRUSH)::GetStockObject(WHITE_BRUSH));
}
// Implementation - data
int m_nMinPage;
int m_nMaxPage;
};
class CPrintPreviewWindow : public CPrintPreviewWindowImpl<CPrintPreviewWindow>
{
public:
DECLARE_WND_CLASS_EX(_T("WTL_PrintPreview"), CS_VREDRAW | CS_HREDRAW, -1)
};
///////////////////////////////////////////////////////////////////////////////
// CZoomPrintPreviewWindowImpl - Implements print preview window with zooming
#ifdef __ATLSCRL_H__
template <class T, class TBase = ATL::CWindow, class TWinTraits = ATL::CControlWinTraits>
class ATL_NO_VTABLE CZoomPrintPreviewWindowImpl : public CPrintPreviewWindowImpl< T, TBase, TWinTraits >, public CZoomScrollImpl< T >
{
public:
bool m_bSized;
CZoomPrintPreviewWindowImpl()
{
SetScrollExtendedStyle(SCRL_DISABLENOSCROLL);
InitZoom();
}
// should be called to reset data members before recreating window
void InitZoom()
{
m_bSized = false;
m_nZoomMode = ZOOMMODE_OFF;
m_fZoomScaleMin = 1.0;
m_fZoomScale = 1.0;
}
BEGIN_MSG_MAP(CZoomPrintPreviewWindowImpl)
MESSAGE_HANDLER(WM_SETCURSOR, CZoomScrollImpl< T >::OnSetCursor)
MESSAGE_HANDLER(WM_VSCROLL, CScrollImpl< T >::OnVScroll)
MESSAGE_HANDLER(WM_HSCROLL, CScrollImpl< T >::OnHScroll)
MESSAGE_HANDLER(WM_MOUSEWHEEL, CScrollImpl< T >::OnMouseWheel)
#if !((_WIN32_WINNT >= 0x0400) || (_WIN32_WINDOWS > 0x0400))
MESSAGE_HANDLER(m_uMsgMouseWheel, CScrollImpl< T >::OnMouseWheel)
#endif // !((_WIN32_WINNT >= 0x0400) || (_WIN32_WINDOWS > 0x0400))
MESSAGE_HANDLER(WM_SETTINGCHANGE, CScrollImpl< T >::OnSettingChange)
MESSAGE_HANDLER(WM_LBUTTONDOWN, CZoomScrollImpl< T >::OnLButtonDown)
MESSAGE_HANDLER(WM_MOUSEMOVE, CZoomScrollImpl< T >::OnMouseMove)
MESSAGE_HANDLER(WM_LBUTTONUP, CZoomScrollImpl< T >::OnLButtonUp)
MESSAGE_HANDLER(WM_CAPTURECHANGED, CZoomScrollImpl< T >::OnCaptureChanged)
MESSAGE_HANDLER(WM_SIZE, OnSize)
MESSAGE_HANDLER(WM_ERASEBKGND, OnEraseBkgnd)
MESSAGE_HANDLER(WM_PAINT, OnPaint)
MESSAGE_HANDLER(WM_PRINTCLIENT, OnPaint)
ALT_MSG_MAP(1)
COMMAND_ID_HANDLER(ID_SCROLL_UP, CScrollImpl< T >::OnScrollUp)
COMMAND_ID_HANDLER(ID_SCROLL_DOWN, CScrollImpl< T >::OnScrollDown)
COMMAND_ID_HANDLER(ID_SCROLL_PAGE_UP, CScrollImpl< T >::OnScrollPageUp)
COMMAND_ID_HANDLER(ID_SCROLL_PAGE_DOWN, CScrollImpl< T >::OnScrollPageDown)
COMMAND_ID_HANDLER(ID_SCROLL_TOP, CScrollImpl< T >::OnScrollTop)
COMMAND_ID_HANDLER(ID_SCROLL_BOTTOM, CScrollImpl< T >::OnScrollBottom)
COMMAND_ID_HANDLER(ID_SCROLL_LEFT, CScrollImpl< T >::OnScrollLeft)
COMMAND_ID_HANDLER(ID_SCROLL_RIGHT, CScrollImpl< T >::OnScrollRight)
COMMAND_ID_HANDLER(ID_SCROLL_PAGE_LEFT, CScrollImpl< T >::OnScrollPageLeft)
COMMAND_ID_HANDLER(ID_SCROLL_PAGE_RIGHT, CScrollImpl< T >::OnScrollPageRight)
COMMAND_ID_HANDLER(ID_SCROLL_ALL_LEFT, CScrollImpl< T >::OnScrollAllLeft)
COMMAND_ID_HANDLER(ID_SCROLL_ALL_RIGHT, CScrollImpl< T >::OnScrollAllRight)
END_MSG_MAP()
LRESULT OnSize(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
SIZE sizeClient = {GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam)};
POINT ptOffset = m_ptOffset;
SIZE sizeAll = m_sizeAll;
SetScrollSize(sizeClient);
if(sizeAll.cx > 0)
ptOffset.x = ::MulDiv(ptOffset.x, m_sizeAll.cx, sizeAll.cx);
if(sizeAll.cy > 0)
ptOffset.y = ::MulDiv(ptOffset.y, m_sizeAll.cy, sizeAll.cy);
SetScrollOffset(ptOffset);
CScrollImpl< T >::OnSize(uMsg, wParam, lParam, bHandled);
if(!m_bSized)
{
m_bSized = true;
T* pT = static_cast<T*>(this);
pT->ShowScrollBar(SB_HORZ, TRUE);
pT->ShowScrollBar(SB_VERT, TRUE);
}
return 0;
}
LRESULT OnEraseBkgnd(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
{
return 1;
}
LRESULT OnPaint(UINT /*uMsg*/, WPARAM wParam, LPARAM /*lParam*/, BOOL& /*bHandled*/)
{
T* pT = static_cast<T*>(this);
RECT rc = { 0 };
if(wParam != NULL)
{
CDCHandle dc = (HDC)wParam;
int nMapModeSav = dc.GetMapMode();
dc.SetMapMode(MM_ANISOTROPIC);
SIZE szWindowExt = { 0, 0 };
dc.SetWindowExt(m_sizeLogAll, &szWindowExt);
SIZE szViewportExt = { 0, 0 };
dc.SetViewportExt(m_sizeAll, &szViewportExt);
POINT ptViewportOrg = { 0, 0 };
dc.SetViewportOrg(-m_ptOffset.x, -m_ptOffset.y, &ptViewportOrg);
pT->DoPrePaint(dc, rc);
pT->DoPaint(dc, rc);
dc.SetMapMode(nMapModeSav);
dc.SetWindowExt(szWindowExt);
dc.SetViewportExt(szViewportExt);
dc.SetViewportOrg(ptViewportOrg);
}
else
{
CPaintDC dc(pT->m_hWnd);
pT->PrepareDC(dc.m_hDC);
pT->DoPrePaint(dc.m_hDC, rc);
pT->DoPaint(dc.m_hDC, rc);
}
return 0;
}
// Painting helpers
void DoPaint(CDCHandle dc)
{
// this one is not used
}
void DoPrePaint(CDCHandle dc, RECT& rc)
{
RECT rcClient;
GetClientRect(&rcClient);
RECT rcArea = rcClient;
T* pT = static_cast<T*>(this);
pT; // avoid level 4 warning
::InflateRect(&rcArea, -pT->m_cxOffset, -pT->m_cyOffset);
if (rcArea.left > rcArea.right)
rcArea.right = rcArea.left;
if (rcArea.top > rcArea.bottom)
rcArea.bottom = rcArea.top;
GetPageRect(rcArea, &rc);
HBRUSH hbrOld = dc.SelectBrush(::GetSysColorBrush(COLOR_BTNSHADOW));
dc.PatBlt(rcClient.left, rcClient.top, rc.left - rcClient.left, rcClient.bottom - rcClient.top, PATCOPY);
dc.PatBlt(rc.left, rcClient.top, rc.right - rc.left, rc.top - rcClient.top, PATCOPY);
dc.PatBlt(rc.right, rcClient.top, rcClient.right - rc.right, rcClient.bottom - rcClient.top, PATCOPY);
dc.PatBlt(rc.left, rc.bottom, rc.right - rc.left, rcClient.bottom - rc.bottom, PATCOPY);
dc.SelectBrush((HBRUSH)::GetStockObject(WHITE_BRUSH));
dc.PatBlt(rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top, PATCOPY);
dc.SelectBrush(::GetSysColorBrush(COLOR_3DDKSHADOW));
dc.PatBlt(rc.right, rc.top + 4, 4, rc.bottom - rc.top, PATCOPY);
dc.PatBlt(rc.left + 4, rc.bottom, rc.right - rc.left, 4, PATCOPY);
dc.SelectBrush(hbrOld);
}
void DoPaint(CDCHandle dc, RECT& rc)
{
CEnhMetaFileInfo emfinfo(m_meta);
ENHMETAHEADER* pmh = emfinfo.GetEnhMetaFileHeader();
int nOffsetX = MulDiv(m_sizeCurPhysOffset.cx, rc.right-rc.left, pmh->szlDevice.cx);
int nOffsetY = MulDiv(m_sizeCurPhysOffset.cy, rc.bottom-rc.top, pmh->szlDevice.cy);
dc.OffsetWindowOrg(-nOffsetX, -nOffsetY);
dc.PlayMetaFile(m_meta, &rc);
}
};
class CZoomPrintPreviewWindow : public CZoomPrintPreviewWindowImpl<CZoomPrintPreviewWindow>
{
public:
DECLARE_WND_CLASS_EX(_T("WTL_ZoomPrintPreview"), CS_VREDRAW | CS_HREDRAW, -1)
};
#endif // __ATLSCRL_H__
}; // namespace WTL
#endif // __ATLPRINT_H__
|