一个Wingdows 打印程序求高手指点

x360995630 2007-05-16 10:23:20
显示的一个简短的Windows程序能够在缺省的打印机上打印矩形和一行文本。
# include <windows.h>
LRESULT CALLBACK WndProc(HWND hWnd,UINT message, WPARAM wParam, LPARAM lParam);
void PrintRectangle();

int WINAPI WinMain(HINSTANCE hCurrentInst,HINSTANCE hPrevInstance,PSTR lpszCmdLine, int nCmdShow)
{
WNDCLASS wndClass;
HWND hWnd;
MSG msg;
UINT width;
UINT height;

wndClass.style=CS_HREDRAW|CS_VREDRAW;
wndClass.lpfnWndProc=WndProc;
wndClass.cbClsExtra=0;
wndClass.cbWndExtra=0;
wndClass.hInstance=hCurrentInst;
wndClass.hCursor=LoadIcon(NULL,IDI_APPLICATION);
wndClass.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);
wndClass.lpszMenuName=NULL;
wndClass.lpszClassName="BasicPrintApp";

RegisterClass(&wndClass);

width=GetSystemMetrics(SM_CXSCREEN)/2;
height=GetSystemMetrics(SM_CYSCREEN)/2;

hWnd=CreateWindow(
"BasicPrintApp", //窗口类的名称
"Basic Print App", //标题栏的文本
WS_OVERLAPPEDWINDOW, //窗口的样式
10, //X位置
10, //Y位置
width, //宽度
height, //高度
NULL, //父窗口的句柄
NULL, //菜单句柄
hCurrentInst, //示例句柄
NULL); //无其他数据
ShowWindow(hWnd,nCmdShow);
UpdateWindow(hWnd);

while(GetMessage(&msg,NULL,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
HDC hDC;
PAINTSTRUCT paintStruct;

switch(message)
{
case WM_PAINT:
hDC=BeginPaint(hWnd,&paintStruct);
TextOut(hDC,10,10,"Click in the windows to print.",29);
EndPaint(hWnd,&paintStruct);
return 0;
case WM_LBUTTONDOWN:
PrintRectangle();
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hWnd, message, wParam, lParam);
}
void PrintRectangle()
{
PRINTER_INFO_5 printerInfo5[3];
DWORD needed,returned;
HDC printDC;
DOCINFO docInfo;
char docName[]="RectangleDoc";
int result;

//第一步:获得打印机的DC
EnumPrinters(PRINTER_ENUM_DEFAULT,NULL,5,(LPBYTE)printerInfo5, sizeof(printerInfo5), &needed, &returned);
printDC=CreateDC(NULL, printerInfo5[0].pPrinterName,NULL,NULL);

//第二步:调用StartDoc()
docInfo.cbSize=sizeof(docInfo);
docInfo.lpszDocName=docName;
docInfo.lpszOutput=NULL;
docInfo.lpszDatatype=NULL;
docInfo.fwType=0;
result=StartDoc(printDC,&docInfo);
if(result<=0)
{
MessageBox(0,"StartDoc() failed","Basic Print App",MB_OK|MB_ICONERROR);
return;
}
//第三步:调用StartPage()
result=StartPage(printDC);
if(result<=0)
{
MessageBox(0,"StartPage() failed","Basic Print App", MB_OK|MB_ICONERROR);
return;
}
//第四步:打印数据
Rectangle(printDC,20,20,1000,200);
TextOut(printDC,100,90,"Windows printing in action!",27);

//第五步:调用EndPage()
result=EndPage(printDC);
if(result<=0)
{
MessageBox(0,"EndPage() failed", "Basic Print App", MB_OK|MB_ICONERROR);
return;
}
//第六步:调用EndDOC()
EndDoc(printDC);
MessageBox(0,"Document printed","Basic Print App", MB_OK|MB_ICONINFORMATION);
}
...全文
53 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

590

社区成员

发帖
与我相关
我的任务
社区描述
提出问题
其他 技术论坛(原bbs)
社区管理员
  • community_281
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧