Prdemo: C/C++ Version
The C/C++ version of Prdemo sets up and displays a Print dialog box in its SelectPrinter() function. If a printer is selected, SelectPrinter() also passes its device context to fg_printdc().
/****************************************************************************\
* *
* Prdemo.c *
* *
* This program shows how to print the contents of a virtual buffer. It *
* first loads a 640x480 BMP file into a virtual buffer and displays it. *
* When the user clicks the Print selection on the top-level menu, a 6-inch *
* by 4-inch copy of the BMP image is sent to the selected printer. *
* *
\****************************************************************************/
#include
#include "Prdemo.h"
#define vbWidth 640
#define vbHeight 480
LRESULT CALLBACK WindowProc(HWND,UINT,WPARAM,LPARAM);
int SelectPrinter(void);
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdParam, int iCmdShow)
{
static char szAppName[] = "FGprdemo";
HWND hWnd;
MSG msg;
WNDCLASSEX wndclass;
wndclass.cbSize = sizeof(wndclass);
wndclass.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
wndclass.lpfnWndProc = WindowProc;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = hInstance;
wndclass.hIcon = LoadIcon(NULL,IDI_APPLICATION);
wndclass.hCursor = LoadCursor (NULL,IDC_ARROW);
wndclass.hbrBackground = NULL;
wndclass.lpszMenuName = szAppName;
wndclass.lpszClassName = szAppName;
wndclass.hIconSm = LoadIcon(NULL,IDI_APPLICATION);
RegisterClassEx(&wndclass);
hWnd = CreateWindow(szAppName, // window class name
"Printing Demo", // window caption
WS_OVERLAPPEDWINDOW, // window style
CW_USEDEFAULT, // initial x position
CW_USEDEFAULT, // initial y position
CW_USEDEFAULT, // initial x size
CW_USEDEFAULT, // initial y size
NULL, // parent window handle
NULL, // window menu handle
hInstance, // program instance handle
NULL); // creation parameters
ShowWindow(hWnd,iCmdShow);
UpdateWindow(hWnd);
while (GetMessage(&msg,NULL,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
/****************************************************************************\
* *
* WindowProc() *
* *
\****************************************************************************/
HCURSOR hCursor;
HDC hDC;
HPALETTE hPal;
int hVB;
UINT cxClient, cyClient;
LRESULT CALLBACK WindowProc(HWND hWnd, UINT iMsg, WPARAM wParam, LPARAM lParam)
{
PAINTSTRUCT ps;
switch (iMsg)
{
case WM_CREATE:
hDC = GetDC(hWnd);
fg_setdc(hDC);
hPal = fg_defpal();
fg_realize(hPal);
fg_vbinit();
hVB = fg_vballoc(vbWidth,vbHeight);
fg_vbopen(hVB);
fg_vbcolors();
fg_showbmp("PORCH.BMP",0);
return 0;
case WM_COMMAND:
switch (wParam)
{
case IDM_Print:
if (SelectPrinter() == 0) return 0;
if (fg_printer(STARTDOC) == 0)
{
hCursor = SetCursor(LoadCursor(NULL,IDC_WAIT));
ShowCursor(TRUE);
fg_vbprint(0,vbWidth-1,0,vbHeight-1,0,600,0,400,2);
fg_printer(NEWFRAME);
fg_printer(ENDDOC);
ShowCursor(FALSE);
SetCursor(hCursor);
return 0;
}
else
MessageBox(hWnd,"Printer setup failed","Error",MB_ICONSTOP|MB_OK);
break;
case IDM_Exit:
DestroyWindow(hWnd);
return 0;
}
break;
case WM_PAINT:
BeginPaint(hWnd,&ps);
fg_vbscale(0,vbWidth-1,0,vbHeight-1,0,cxClient-1,0,cyClient-1);
EndPaint(hWnd,&ps);
return 0;
case WM_SETFOCUS:
fg_realize(hPal);
InvalidateRect(hWnd,NULL,TRUE);
return 0;
case WM_SIZE:
cxClient = LOWORD(lParam);
cyClient = HIWORD(lParam);
return 0;
case WM_DESTROY:
fg_vbclose();
fg_vbfree(hVB);
fg_vbfin();
DeleteObject(hPal);
ReleaseDC(hWnd,hDC);
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hWnd,iMsg,wParam,lParam);
}
/****************************************************************************\
* *
* SelectPrinter() *
* *
* Display a dialog box for printer selection. *
* *
\****************************************************************************/
int SelectPrinter()
{
PRINTDLG pd;
// fill in structure fields for Print dialog box
memset(&pd,0,sizeof(PRINTDLG));
pd.lStructSize = sizeof(PRINTDLG);
pd.hwndOwner = GetActiveWindow();
pd.Flags = PD_RETURNDC | PD_NOPAGENUMS | PD_NOSELECTION;
pd.nCopies = 1;
// if successful, make the selected printer the active printer
if (PrintDlg(&pd))
{
fg_printdc(pd.hDC);
return 1;
}
return 0;
}
|