2 * Printing support for MSWindows
4 * $Id: print_mswin.c,v 1.7 2003/11/11 13:59:53 ulfl Exp $
6 * Ethereal - Network traffic analyzer
7 * By Gerald Combs <gerald@ethereal.com>
8 * Copyright 2002, Jeffrey C. Foster <jfoste@woodward.com>
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25 * This original code was from the Technet Article Q139652 :
26 * HOWTO: Print a Document
40 BOOL CALLBACK abort_proc( HDC hDC, int Error );
41 HDC get_printer_dc(void);
42 void init_doc_struct( DOCINFO* di, char* docname);
43 void print_file( char* file_name, HDC hdc);
45 void print_mswin(char *file_name)
51 HWND hWndParent = HWND_DESKTOP; /* would be better to be a real window */
53 /* Need a printer DC to print to. */
54 hDC = get_printer_dc();
56 /* Did you get a good DC?, Cancel will return NULL also, so what to do? */
62 /* You always have to use an AbortProc(). */
63 if( SetAbortProc( hDC, abort_proc ) == SP_ERROR )
65 MessageBox( NULL, "Error setting up AbortProc",
66 "Error", MB_APPLMODAL | MB_OK);
70 /* Init the DOCINFO and start the document. */
71 init_doc_struct( &di, "MyDoc");
76 print_file(file_name, hDC );
79 /* Indicate end of document. */
86 /*===============================*/
87 /* Obtain printer device context */
88 /* ==============================*/
89 HDC get_printer_dc(void)
93 /* Initialize the PRINTDLG structure. */
94 memset( &pdlg, 0, sizeof( PRINTDLG ) );
95 pdlg.lStructSize = sizeof( PRINTDLG );
96 /* Set the flag to return printer DC. */
98 /* return the device context we need */
100 /* disable the "Pages" radio button */
102 /* disable the "Selection" radio button */
104 /* let device print multiple pages (if requested) */
105 PD_USEDEVMODECOPIESANDCOLLATE;
107 /* Invoke the printer dialog box. */
110 /* hDC member of the PRINTDLG structure contains the printer DC. */
114 /*===============================*/
115 /* The Abort Procudure */
116 /* ==============================*/
117 BOOL CALLBACK abort_proc( HDC hDC, int Error )
120 while( PeekMessage( &msg, NULL, 0, 0, PM_REMOVE ) )
122 TranslateMessage( &msg );
123 DispatchMessage( &msg );
128 /*===============================*/
129 /* Initialize DOCINFO structure */
130 /* ==============================*/
131 void init_doc_struct( DOCINFO* di, char* docname)
133 /* Always zero it before using it. */
134 memset( di, 0, sizeof( DOCINFO ) );
135 /* Fill in the required members. */
136 di->cbSize = sizeof( DOCINFO );
137 di->lpszDocName = docname;
140 /*===============================*/
141 /* Drawing on the DC */
142 /* ==============================*/
143 void print_file( char *file_name, HDC hdc) {
145 #define max_buf_size 1024
151 int results, cnt=0, y_pos = y_offset, y_cnt = 0;
152 char buf[ max_buf_size];
156 GetTextMetrics(hdc, &tm);
157 SetMapMode (hdc, MM_TEXT);
160 fh1 = fopen( file_name, "r" );
162 perror( "open failed on input file" );
165 while ((results = fread( &ch, 1, 1, fh1 )) != 0) {
167 /* if end of line send buffer and more y position */
171 TextOut(hdc, x_offset,y_pos, buf, strlen(buf));
172 y_pos += tm.tmHeight;
174 if ( ++y_cnt == max_lines){
175 /* Print one page. */
182 /* if line buffer is full, dump it */
183 }else { if ( cnt == ( max_buf_size - 1)) {
185 TextOut(hdc, x_offset, y_pos, buf, strlen(buf));
186 y_pos += tm.tmHeight;
189 if ( ++y_cnt == max_lines){
190 /* Print one page. */
201 /*XXX need feof test here ? */
203 /* Print the last text if needed */
206 TextOut(hdc, 0,y_pos, buf, strlen(buf));