Pamela Patterson
October 19th, 1999, 05:03 PM
Hello. I'm back. Thanks again for a the great help I get. I have a small program that I have created. I want to print the text from the windows program. Everything compiles and links fine. I even get the system print dialog box with my default printer indicated.- Just nothing prints out. I know it has to been in the &msg the way I have defined it or something. I am not allowed to use MFC. I can only use PetZold's book published - 1996 - Programming Windows 95. Here is the code I have written.
HERE IS THE HEADER FILE FOR THE MENU AND MY OWN LITTLE DIALOG BOX(NOT NEEDED REALLY)
//testresource.h
#define IDM_FILE 0
#define IDM_PRINT 1
#define IDD_FNAME 10
HERE IS THE "MAIN" .CPP
//test.cpp
/*------------------------------------------------------------
HELLOWIN.C -- Displays "Hello, Windows 95!" in client area
(c) Charles Petzold, 1996
------------------------------------------------------------*/
#define print zprint
#include <windows.h>
#include <commdlg.h>
#include <stdlib.h>
//#include "print.h"
#include "stdio.h"
#include "ios.h"
#include "string.h"
#include "math.h"
#include "testresource.h"
void _cdecl print(char *str_);
HWND hwnd;
HINSTANCE hInst;
HDC GlobalHDC;
HFONT hFont;
TEXTMETRIC tm;
int i1;
int i2;
char szAppName[] = "HelloWin" ;
LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
//BOOL CALLBACK AboutDlgProc(HWND, UINT, WPARAM, LPARAM);
// Function for printing to the printer printer.c
BOOL SndToPrnt(HINSTANCE, HWND,PSTR);
//global variables
static HWND hDlgModeless;
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)
{
MSG msg ;
HACCEL hAccel;
WNDCLASSEX wndclass ;
wndclass.cbSize = sizeof (wndclass) ;
wndclass.style = CS_HREDRAW | CS_VREDRAW ;
wndclass.lpfnWndProc = WndProc ;
wndclass.cbClsExtra = 0 ;
wndclass.cbWndExtra = 0 ;
wndclass.hInstance = hInstance ;
wndclass.hIcon = LoadIcon (NULL, IDI_APPLICATION) ;
wndclass.hCursor = LoadCursor (NULL, IDC_ARROW) ;
wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
wndclass.lpszMenuName = szAppName;
wndclass.lpszClassName = szAppName ;
wndclass.hIconSm = LoadIcon (NULL, IDI_APPLICATION) ;
RegisterClassEx (&wndclass) ;
hwnd = CreateWindow (szAppName, // window class name
"The Hello Program", // window caption
WS_OVERLAPPEDWINDOW, // window style
0,//CW_USEDEFAULT, // initial x position
0,//CW_USEDEFAULT, // initial y position
640,//CW_USEDEFAULT, // initial x size
480,//CW_USEDEFAULT, // initial y size
NULL, // parent window handle
NULL, // window menu handle
hInstance, // program instance handle
szCmdLine); // creation parameters
ShowWindow (hwnd, iCmdShow) ;
UpdateWindow (hwnd) ;
hAccel = LoadAccelerators(hInstance, szAppName);
while (GetMessage (&msg, NULL, 0, 0))
{
if(hDlgModeless == NULL || !IsDialogMessage (hDlgModeless, &msg))
{
if(!TranslateAccelerator(hwnd, hAccel, &msg))
{
TranslateMessage (&msg) ;
DispatchMessage (&msg) ;
}
}
}
return msg.wParam ;
}
LRESULT CALLBACK WndProc (HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam)
{
HMENU hMenu, hMenuPopup;
int iSelection = IDM_PRINT;
PAINTSTRUCT ps ;
int i;
switch (iMsg)
{
case WM_CREATE:
hMenu = CreateMenu ();
hMenuPopup = CreateMenu();
AppendMenu(hMenuPopup,MF_STRING, IDM_PRINT, "&Print");
AppendMenu(hMenu,MF_POPUP, (UINT)hMenuPopup, "&File");
SetMenu(hwnd, hMenu);
GlobalHDC = GetDC(hwnd);
ReleaseDC(hwnd,GlobalHDC);
return 0;
case WM_COMMAND:
hMenu = GetMenu(hwnd);
switch (LOWORD(wParam))
{
case IDM_PRINT :
if(!SndToPrnt(hInst, hwnd,szAppName))
MessageBox(hwnd,"Unable to print.",NULL,MB_OK);
return 0;
}
break;
case WM_PAINT :
GlobalHDC = BeginPaint (hwnd, &ps) ;
//MessageBox(hwnd,"Starting Hello","Hello",MB_OK);
i1 = 0, i2 = 0;
print("12345678901234567890123456789012345678901234567890123456789012345678901234567890");
for(i = 0; i < 25 ; i++){
print("Hello");
i1++;
//if (i == 25) i2 = 40;
}
EndPaint (hwnd, &ps) ;
return 0 ;
case WM_DESTROY :
PostQuitMessage (0) ;
return 0 ;
}
return DefWindowProc (hwnd, iMsg, wParam, lParam) ;
}
void _cdecl print(char *str_)
{
int xx = strlen(str_);
int xpos;
int ypos;
int xchar;
int ychar;
hFont = CreateFont(17,7,0,0,FW_NORMAL,FALSE,FALSE,FALSE,ANSI_CHARSET,OUT_DEFAULT_PRECIS,
0,PROOF_QUALITY,FIXED_PITCH|FF_DONTCARE, "courier new");
SelectObject(GlobalHDC,hFont);
GetTextMetrics(GlobalHDC, &tm);
xchar = tm.tmMaxCharWidth;
ychar = tm.tmHeight + tm.tmExternalLeading;
ypos = (ychar * i1);
xpos = /*(*/(xchar * i2); // 1.40);
TextOut(GlobalHDC, xpos, ypos,str_,xx);
DeleteObject(SelectObject(GlobalHDC, hFont));
}
HERE IS THE ONE TO GET THE TEXT FROM THE WINDOW AND PRINT IT.
//testprint.cpp
#include <windows.h>
#include <commdlg.h>
#include <string.h>
#include "testresource.h"
BOOL bUserAbort;
HWND hDlgPrint;
BOOL CALLBACK PrintDlgProc (HWND hDlg, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch (msg)
{
case WM_INITDIALOG :
EnableMenuItem(GetSystemMenu(hDlg,FALSE), SC_CLOSE, MF_GRAYED);
return TRUE;
case WM_COMMAND:
bUserAbort = TRUE;
EnableWindow(GetParent(hDlg), TRUE); //check here should maybe be EnableWindow(GetParent(hdlg), TRUE;
DestroyWindow(hDlg);
hDlgPrint = 0;
return TRUE;
}
return FALSE;
}
BOOL CALLBACK AbortProc(HDC hPrinterDC, int iCode)
{
MSG msg;
while(!bUserAbort && PeekMessage(&msg, NULL,0,0, PM_REMOVE))
{
if(!hDlgPrint || !IsDialogMessage (hDlgPrint, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return !bUserAbort;
}
BOOL SndToPrnt(HINSTANCE hInst, HWND hwnd,LPSTR szAppName)
{
static DOCINFO di = {sizeof (DOCINFO), "", NULL };
static PRINTDLG pd;
BOOL bSuccess;
LPCTSTR pstrBuffer;
int yChar, iCharsPerLine, iLinesPerPage, iTotalLines,
iTotalPages, iPage, iLine, iLineNum;
TEXTMETRIC tm;
WORD iColCopy, iNoiColCopy;
pd.lStructSize = sizeof (PRINTDLG);
pd.hwndOwner = hwnd;
pd.hDevMode = NULL;
pd.hDevNames = NULL;
pd.hDC = NULL;
pd.Flags = PD_ALLPAGES|PD_COLLATE|PD_RETURNDC;
pd.nFromPage = 0;
pd.nToPage = 0;
pd.nMinPage = 0;
pd.nMaxPage = 0;
pd.nCopies = 1;
pd.hInstance = NULL;
pd.lCustData = 0L;
pd.lpfnPrintHook = NULL;
pd.lpfnSetupHook = NULL;
pd.lpPrintTemplateName = NULL;
pd.lpSetupTemplateName = NULL;
pd.hPrintTemplate = NULL;
pd.hSetupTemplate = NULL;
if(!PrintDlg (&pd))
return TRUE;
iTotalLines = (short) SendMessage (hwnd, EM_GETLINECOUNT,0,0L);
if(iTotalLines == 0)
return TRUE;
GetTextMetrics(pd.hDC, &tm);
yChar = tm.tmHeight + tm.tmExternalLeading;
iCharsPerLine = GetDeviceCaps(pd.hDC, HORZRES)/tm.tmAveCharWidth;
iLinesPerPage = GetDeviceCaps(pd.hDC, VERTRES)/yChar;
iTotalPages = (iTotalLines + iLinesPerPage - 1)/iLinesPerPage;
pstrBuffer = (LPCTSTR) HeapAlloc (GetProcessHeap(),
HEAP_NO_SERIALIZE, iCharsPerLine + 1);
EnableWindow(hwnd,FALSE);
bSuccess = TRUE;
bUserAbort = FALSE;
hDlgPrint = CreateDialog(hInst, (LPCTSTR) "PrintDlgBox", hwnd, PrintDlgProc);
SetDlgItemText (hDlgPrint, IDD_FNAME, szAppName);
SetAbortProc(pd.hDC, AbortProc);
GetWindowText(hwnd,(PTSTR) di.lpszDocName, sizeof(PTSTR));
if(StartDoc(pd.hDC, &di)>0)
{
for(iColCopy = 0;
iColCopy < ((WORD) pd.Flags & PD_COLLATE ? pd.nCopies :1);
iColCopy++)
{
for(iPage = 0;iPage<iTotalPages; iPage++)
{
for(iNoiColCopy = 0;
iNoiColCopy < (pd.Flags & PD_COLLATE ? 1: pd.nCopies);
iNoiColCopy++)
{
if(StartPage (pd.hDC) <0)
{
bSuccess = FALSE;
break;
}
for(iLine = 0; iLine<iLinesPerPage; iLine++)
{
iLineNum = iLinesPerPage * iPage + iLine;
if(iLineNum > iTotalLines)
break;
*(int *) pstrBuffer = iCharsPerLine;
TextOut(pd.hDC, 0, yChar * iLine, pstrBuffer,
(int) SendMessage (hwnd,EM_GETLINE,
(WPARAM) iLineNum, (LPARAM) pstrBuffer));
}
if(EndPage(pd.hDC)<0)
{
bSuccess = FALSE;
break;
}
if(bUserAbort)
break;
}
if(!bSuccess || bUserAbort)
break;
}
if(!bSuccess || bUserAbort)
break;
}
}
else
bSuccess = FALSE;
if(bSuccess)
EndDoc(pd.hDC);
if(!bUserAbort)
{
EnableWindow(hwnd,TRUE);
DestroyWindow(hDlgPrint);
}
HeapFree(GetProcessHeap(),0,(LPVOID) pstrBuffer);
DeleteDC(pd.hDC);
return bSuccess && !bUserAbort;
}
TTFN
:-)
I do greatly appreciate any help or suggestions.
Pamela
HERE IS THE HEADER FILE FOR THE MENU AND MY OWN LITTLE DIALOG BOX(NOT NEEDED REALLY)
//testresource.h
#define IDM_FILE 0
#define IDM_PRINT 1
#define IDD_FNAME 10
HERE IS THE "MAIN" .CPP
//test.cpp
/*------------------------------------------------------------
HELLOWIN.C -- Displays "Hello, Windows 95!" in client area
(c) Charles Petzold, 1996
------------------------------------------------------------*/
#define print zprint
#include <windows.h>
#include <commdlg.h>
#include <stdlib.h>
//#include "print.h"
#include "stdio.h"
#include "ios.h"
#include "string.h"
#include "math.h"
#include "testresource.h"
void _cdecl print(char *str_);
HWND hwnd;
HINSTANCE hInst;
HDC GlobalHDC;
HFONT hFont;
TEXTMETRIC tm;
int i1;
int i2;
char szAppName[] = "HelloWin" ;
LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
//BOOL CALLBACK AboutDlgProc(HWND, UINT, WPARAM, LPARAM);
// Function for printing to the printer printer.c
BOOL SndToPrnt(HINSTANCE, HWND,PSTR);
//global variables
static HWND hDlgModeless;
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)
{
MSG msg ;
HACCEL hAccel;
WNDCLASSEX wndclass ;
wndclass.cbSize = sizeof (wndclass) ;
wndclass.style = CS_HREDRAW | CS_VREDRAW ;
wndclass.lpfnWndProc = WndProc ;
wndclass.cbClsExtra = 0 ;
wndclass.cbWndExtra = 0 ;
wndclass.hInstance = hInstance ;
wndclass.hIcon = LoadIcon (NULL, IDI_APPLICATION) ;
wndclass.hCursor = LoadCursor (NULL, IDC_ARROW) ;
wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
wndclass.lpszMenuName = szAppName;
wndclass.lpszClassName = szAppName ;
wndclass.hIconSm = LoadIcon (NULL, IDI_APPLICATION) ;
RegisterClassEx (&wndclass) ;
hwnd = CreateWindow (szAppName, // window class name
"The Hello Program", // window caption
WS_OVERLAPPEDWINDOW, // window style
0,//CW_USEDEFAULT, // initial x position
0,//CW_USEDEFAULT, // initial y position
640,//CW_USEDEFAULT, // initial x size
480,//CW_USEDEFAULT, // initial y size
NULL, // parent window handle
NULL, // window menu handle
hInstance, // program instance handle
szCmdLine); // creation parameters
ShowWindow (hwnd, iCmdShow) ;
UpdateWindow (hwnd) ;
hAccel = LoadAccelerators(hInstance, szAppName);
while (GetMessage (&msg, NULL, 0, 0))
{
if(hDlgModeless == NULL || !IsDialogMessage (hDlgModeless, &msg))
{
if(!TranslateAccelerator(hwnd, hAccel, &msg))
{
TranslateMessage (&msg) ;
DispatchMessage (&msg) ;
}
}
}
return msg.wParam ;
}
LRESULT CALLBACK WndProc (HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam)
{
HMENU hMenu, hMenuPopup;
int iSelection = IDM_PRINT;
PAINTSTRUCT ps ;
int i;
switch (iMsg)
{
case WM_CREATE:
hMenu = CreateMenu ();
hMenuPopup = CreateMenu();
AppendMenu(hMenuPopup,MF_STRING, IDM_PRINT, "&Print");
AppendMenu(hMenu,MF_POPUP, (UINT)hMenuPopup, "&File");
SetMenu(hwnd, hMenu);
GlobalHDC = GetDC(hwnd);
ReleaseDC(hwnd,GlobalHDC);
return 0;
case WM_COMMAND:
hMenu = GetMenu(hwnd);
switch (LOWORD(wParam))
{
case IDM_PRINT :
if(!SndToPrnt(hInst, hwnd,szAppName))
MessageBox(hwnd,"Unable to print.",NULL,MB_OK);
return 0;
}
break;
case WM_PAINT :
GlobalHDC = BeginPaint (hwnd, &ps) ;
//MessageBox(hwnd,"Starting Hello","Hello",MB_OK);
i1 = 0, i2 = 0;
print("12345678901234567890123456789012345678901234567890123456789012345678901234567890");
for(i = 0; i < 25 ; i++){
print("Hello");
i1++;
//if (i == 25) i2 = 40;
}
EndPaint (hwnd, &ps) ;
return 0 ;
case WM_DESTROY :
PostQuitMessage (0) ;
return 0 ;
}
return DefWindowProc (hwnd, iMsg, wParam, lParam) ;
}
void _cdecl print(char *str_)
{
int xx = strlen(str_);
int xpos;
int ypos;
int xchar;
int ychar;
hFont = CreateFont(17,7,0,0,FW_NORMAL,FALSE,FALSE,FALSE,ANSI_CHARSET,OUT_DEFAULT_PRECIS,
0,PROOF_QUALITY,FIXED_PITCH|FF_DONTCARE, "courier new");
SelectObject(GlobalHDC,hFont);
GetTextMetrics(GlobalHDC, &tm);
xchar = tm.tmMaxCharWidth;
ychar = tm.tmHeight + tm.tmExternalLeading;
ypos = (ychar * i1);
xpos = /*(*/(xchar * i2); // 1.40);
TextOut(GlobalHDC, xpos, ypos,str_,xx);
DeleteObject(SelectObject(GlobalHDC, hFont));
}
HERE IS THE ONE TO GET THE TEXT FROM THE WINDOW AND PRINT IT.
//testprint.cpp
#include <windows.h>
#include <commdlg.h>
#include <string.h>
#include "testresource.h"
BOOL bUserAbort;
HWND hDlgPrint;
BOOL CALLBACK PrintDlgProc (HWND hDlg, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch (msg)
{
case WM_INITDIALOG :
EnableMenuItem(GetSystemMenu(hDlg,FALSE), SC_CLOSE, MF_GRAYED);
return TRUE;
case WM_COMMAND:
bUserAbort = TRUE;
EnableWindow(GetParent(hDlg), TRUE); //check here should maybe be EnableWindow(GetParent(hdlg), TRUE;
DestroyWindow(hDlg);
hDlgPrint = 0;
return TRUE;
}
return FALSE;
}
BOOL CALLBACK AbortProc(HDC hPrinterDC, int iCode)
{
MSG msg;
while(!bUserAbort && PeekMessage(&msg, NULL,0,0, PM_REMOVE))
{
if(!hDlgPrint || !IsDialogMessage (hDlgPrint, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return !bUserAbort;
}
BOOL SndToPrnt(HINSTANCE hInst, HWND hwnd,LPSTR szAppName)
{
static DOCINFO di = {sizeof (DOCINFO), "", NULL };
static PRINTDLG pd;
BOOL bSuccess;
LPCTSTR pstrBuffer;
int yChar, iCharsPerLine, iLinesPerPage, iTotalLines,
iTotalPages, iPage, iLine, iLineNum;
TEXTMETRIC tm;
WORD iColCopy, iNoiColCopy;
pd.lStructSize = sizeof (PRINTDLG);
pd.hwndOwner = hwnd;
pd.hDevMode = NULL;
pd.hDevNames = NULL;
pd.hDC = NULL;
pd.Flags = PD_ALLPAGES|PD_COLLATE|PD_RETURNDC;
pd.nFromPage = 0;
pd.nToPage = 0;
pd.nMinPage = 0;
pd.nMaxPage = 0;
pd.nCopies = 1;
pd.hInstance = NULL;
pd.lCustData = 0L;
pd.lpfnPrintHook = NULL;
pd.lpfnSetupHook = NULL;
pd.lpPrintTemplateName = NULL;
pd.lpSetupTemplateName = NULL;
pd.hPrintTemplate = NULL;
pd.hSetupTemplate = NULL;
if(!PrintDlg (&pd))
return TRUE;
iTotalLines = (short) SendMessage (hwnd, EM_GETLINECOUNT,0,0L);
if(iTotalLines == 0)
return TRUE;
GetTextMetrics(pd.hDC, &tm);
yChar = tm.tmHeight + tm.tmExternalLeading;
iCharsPerLine = GetDeviceCaps(pd.hDC, HORZRES)/tm.tmAveCharWidth;
iLinesPerPage = GetDeviceCaps(pd.hDC, VERTRES)/yChar;
iTotalPages = (iTotalLines + iLinesPerPage - 1)/iLinesPerPage;
pstrBuffer = (LPCTSTR) HeapAlloc (GetProcessHeap(),
HEAP_NO_SERIALIZE, iCharsPerLine + 1);
EnableWindow(hwnd,FALSE);
bSuccess = TRUE;
bUserAbort = FALSE;
hDlgPrint = CreateDialog(hInst, (LPCTSTR) "PrintDlgBox", hwnd, PrintDlgProc);
SetDlgItemText (hDlgPrint, IDD_FNAME, szAppName);
SetAbortProc(pd.hDC, AbortProc);
GetWindowText(hwnd,(PTSTR) di.lpszDocName, sizeof(PTSTR));
if(StartDoc(pd.hDC, &di)>0)
{
for(iColCopy = 0;
iColCopy < ((WORD) pd.Flags & PD_COLLATE ? pd.nCopies :1);
iColCopy++)
{
for(iPage = 0;iPage<iTotalPages; iPage++)
{
for(iNoiColCopy = 0;
iNoiColCopy < (pd.Flags & PD_COLLATE ? 1: pd.nCopies);
iNoiColCopy++)
{
if(StartPage (pd.hDC) <0)
{
bSuccess = FALSE;
break;
}
for(iLine = 0; iLine<iLinesPerPage; iLine++)
{
iLineNum = iLinesPerPage * iPage + iLine;
if(iLineNum > iTotalLines)
break;
*(int *) pstrBuffer = iCharsPerLine;
TextOut(pd.hDC, 0, yChar * iLine, pstrBuffer,
(int) SendMessage (hwnd,EM_GETLINE,
(WPARAM) iLineNum, (LPARAM) pstrBuffer));
}
if(EndPage(pd.hDC)<0)
{
bSuccess = FALSE;
break;
}
if(bUserAbort)
break;
}
if(!bSuccess || bUserAbort)
break;
}
if(!bSuccess || bUserAbort)
break;
}
}
else
bSuccess = FALSE;
if(bSuccess)
EndDoc(pd.hDC);
if(!bUserAbort)
{
EnableWindow(hwnd,TRUE);
DestroyWindow(hDlgPrint);
}
HeapFree(GetProcessHeap(),0,(LPVOID) pstrBuffer);
DeleteDC(pd.hDC);
return bSuccess && !bUserAbort;
}
TTFN
:-)
I do greatly appreciate any help or suggestions.
Pamela