|
|
1.1 root 1:
2: /******************************************************************************\
3: * This is a part of the Microsoft Source Code Samples.
4: * Copyright (C) 1993 Microsoft Corporation.
5: * All rights reserved.
6: * This source code is only intended as a supplement to
7: * Microsoft Development Tools and/or WinHelp documentation.
8: * See these sources for detailed information regarding the
9: * Microsoft samples programs.
10: \******************************************************************************/
11:
12: #include "PortTool.h"
13:
14: HWND hCancelDlg = 0;
15: PRINTDLG pdPrint;
16:
17: /* get default printer configuration and save in hWnd extra bytes for use later */
18: BOOL WINAPI GetPrinterConfig (
19: HWND hWnd)
20: {
21: // PRINTDLG pdPrint;
22:
23: pdPrint.lStructSize = sizeof (PRINTDLG);
24: pdPrint.Flags = PD_RETURNDEFAULT;
25: pdPrint.hwndOwner = hWnd;
26: pdPrint.hDevMode = NULL;
27: pdPrint.hDevNames = NULL;
28: pdPrint.hDC = NULL;
29:
30: PrintDlg (&pdPrint);
31:
32: SetWindowLong (hWnd, WL_HPTRDEVNAMES, (LONG) pdPrint.hDevNames);
33:
34: return TRUE;
35: }
36:
37:
38:
39: /* abort proc called by gdi during print download process */
40: int WINAPI AbortProc (
41: HDC hdc,
42: int nErr)
43: {
44: BOOL fContinue = TRUE;
45: MSG msg;
46:
47: /* process messages for cancel dialog and other apps */
48: while (PeekMessage (&msg, NULL, 0, 0, PM_REMOVE))
49: {
50: if (msg.message == UM_CANCELPRINT)
51: {
52: fContinue = FALSE;
53: break;
54: }
55:
56: else if (!hCancelDlg || !IsDialogMessage (hCancelDlg, &msg))
57: {
58: TranslateMessage (&msg);
59: DispatchMessage (&msg);
60: }
61: }
62:
63: return fContinue;
64: }
65:
66:
67:
68:
69: BOOL WINAPI CancelDlgProc (
70: HWND hWnd,
71: UINT uMsg,
72: UINT uParam,
73: LONG lParam)
74: {
75:
76: switch (uMsg)
77: {
78: case WM_INITDIALOG:
79: {
80: char *lpdn;
81: char lpszTitle[MAX_PATH];
82:
83: /* initialize dialog control information */
84: lpdn = LocalLock ((HANDLE)GetWindowLong (hWnd, WL_HPTRDEVNAMES));
85: SetDlgItemText (hWnd,
86: IDC_PRINTDEVICE,
87: lpdn + sizeof (DEVNAMES) +
88: ((DEVNAMES *)lpdn)->wDeviceOffset);
89: SetDlgItemText (hWnd,
90: IDC_PRINTPORT,
91: lpdn + sizeof (DEVNAMES) +
92: ((DEVNAMES *)lpdn)->wOutputOffset);
93: LocalUnlock ((HANDLE)GetWindowLong (hWnd, WL_HPTRDEVNAMES));
94: GetWindowText (hWnd, lpszTitle, sizeof (lpszTitle));
95: SetDlgItemText (hWnd, IDC_PRINTTITLE, lpszTitle);
96: }
97: break;
98:
99: case WM_COMMAND:
100: /* if cancel button selected, post message to cancel print job */
101: if (LOWORD (uParam) == IDCANCEL)
102: {
103: PostMessage (GetParent (hWnd), UM_CANCELPRINT, 0, 0);
104: DestroyWindow (hWnd);
105: }
106: break;
107:
108: default:
109: return FALSE;
110: }
111: return TRUE;
112: }
113:
114:
115:
116: /* put up the print common dialog, and print */
117: int WINAPI PrintFile (
118: HWND hWnd)
119: {
120: char *lpEditData;
121: HANDLE hEditData;
122: SIZE sLine;
123: int yLineExt;
124: int yExt;
125: int yPageExt;
126: UINT uLine;
127: UINT uNumLines;
128: UINT uOffset;
129: UINT uLineLen;
130: HWND hWndEdit;
131: // PRINTDLG pdPrint;
132: DOCINFO diPrint;
133: char lpszJobName[MAX_PATH];
134:
135:
136: /* call common print dialog to get initialized printer DC */
137: pdPrint.hwndOwner = hWnd;
138: pdPrint.hDC = NULL;
139: pdPrint.Flags = PD_RETURNDC;
140:
141: /* call common print dialog */
142: if (!PrintDlg (&pdPrint))
143: return IDS_PTRCOMMDLGFAILED;
144:
145: /* start cancel dialog box */
146: hCancelDlg = CreateDialog ((HANDLE)GetModuleHandle (NULL),
147: IDD_CANCELDLG,
148: hWnd,
149: CancelDlgProc);
150:
151:
152: if (!hCancelDlg)
153: return IDS_CANCELDLGFAILED;
154:
155: ShowWindow (hCancelDlg, SW_SHOW);
156: UpdateWindow (hCancelDlg);
157:
158: /* set AbortProc callback */
159: if (SetAbortProc (pdPrint.hDC, (PROC)AbortProc) < 0)
160: {
161: /* on error, clean up and go away */
162: DestroyWindow (hCancelDlg);
163: DeleteDC (pdPrint.hDC);
164: return IDS_SETABORTPROCFAILED;
165: }
166:
167: /* initialize printer for job */
168: GetWindowText (hWnd, lpszJobName, sizeof (lpszJobName));
169: diPrint.cbSize = sizeof (DOCINFO);
170: diPrint.lpszDocName = lpszJobName;
171: diPrint.lpszOutput = NULL;
172: if (StartDoc (pdPrint.hDC, &diPrint) == SP_ERROR)
173: {
174: /* on error, clean up and go away */
175: DestroyWindow (hCancelDlg);
176: DeleteDC (pdPrint.hDC);
177: return IDS_STARTDOCFAILED;
178: }
179:
180: /* job started, so display cancel dialog */
181: ShowWindow (hCancelDlg, SW_SHOW);
182: UpdateWindow (hCancelDlg);
183:
184: /* retrieve dimensions for printing and init loop variables */
185: hWndEdit = (HWND)GetWindowLong(hWnd, WL_HWNDEDIT);
186: hEditData = (HANDLE)SendMessage (hWndEdit, EM_GETHANDLE, 0, 0L);
187: uNumLines = (WORD)SendMessage (hWndEdit, EM_GETLINECOUNT, 0, 0L);
188: GetTextExtentPoint (pdPrint.hDC, "CC", 2, &sLine);
189: yLineExt = sLine.cy;
190: yPageExt = GetDeviceCaps (pdPrint.hDC, VERTRES);
191: yExt = 0;
192: uLine = 0;
193:
194: /* print text line by line from top to bottom */
195: while (uLine < uNumLines)
196: {
197: /* if at end of page, start a new page */
198: if ((yExt + yLineExt) > yPageExt)
199: {
200: if (!EndPage (pdPrint.hDC))
201: {
202: DestroyWindow (hCancelDlg);
203: DeleteDC (pdPrint.hDC);
204: return IDS_PRINTABORTED;
205: }
206: yExt = 0;
207: }
208:
209: /* determine buffer offset for current line and line length */
210: uOffset = SendMessage (hWndEdit, EM_LINEINDEX, uLine, 0);
211: uLineLen = SendMessage (hWndEdit, EM_LINELENGTH, uOffset, 0);
212: lpEditData = (char *)LocalLock (hEditData) + uOffset;
213:
214: /* print current the line and unlock the text handle */
215: TextOut (pdPrint.hDC, 0, yExt, lpEditData, uLineLen);
216: LocalUnlock (hEditData);
217:
218: /* increment page position */
219: yExt += yLineExt;
220: uLine++;
221: }
222:
223: /* end the last page and document */
224: EndPage (pdPrint.hDC);
225: EndDoc (pdPrint.hDC);
226:
227: /* end cancel dialog box, clean up and exit */
228: DestroyWindow (hCancelDlg);
229: DeleteDC(pdPrint.hDC);
230: return TRUE;
231: }
232:
233:
234:
235: /* printer setup common dialog */
236: int WINAPI PrinterSetup (
237: HWND hWnd)
238: {
239: // PRINTDLG pdPrint;
240:
241: pdPrint.Flags = PD_PRINTSETUP;
242: pdPrint.hwndOwner = hWnd;
243:
244: /* call common print dialog */
245: if (!PrintDlg (&pdPrint) && CommDlgExtendedError ())
246: return IDS_PTRCOMMDLGFAILED;
247: else
248: return 0;
249: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.