已經可以了, 列出完整程式分享!!
// prn03.cpp
#define STRUCT
#include <windows.h>
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
BOOL InitApp(HINSTANCE);
BOOL InitInstance(HINSTANCE, int);
char szClassName[] = "prn03";
HDC GetPrintInfo(void);
int PrinterSet();
HWND hParent;
HINSTANCE hInst;
PRINTER_INFO_5* prninfo=NULL;
int WINAPI WinMain(HINSTANCE hCurInst, HINSTANCE hPrevInst, LPSTR lpszCmdLine, int nCmdShow)
{
MSG msg;
if (!hPrevInst) {
if (!InitApp(hCurInst))
return FALSE;
}
if (!InitInstance(hCurInst, nCmdShow)) {
return FALSE;
}
while (GetMessage(&msg, NULL, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
// 視窗註冊, 傳回 True or False
BOOL InitApp(HINSTANCE hInst)
{
WNDCLASS wc;
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = WndProc; // 程序名稱
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInst; // 實例
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wc.lpszMenuName = NULL; // 功能表名,還必須在編譯時加入rc所產生的.o檔案
wc.lpszClassName = szClassName;
return RegisterClass(&wc);
}
// 產生視窗
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
HWND hWnd;
hInst = hInstance;
hWnd = CreateWindow(szClassName,
"連貓也會的列印處理", // 顯示在標題列的名稱
WS_OVERLAPPEDWINDOW, // 視窗的種類
CW_USEDEFAULT, // X座標
CW_USEDEFAULT, // Y座標
CW_USEDEFAULT, // 視窗長度
CW_USEDEFAULT, // 視窗寬度
NULL,
NULL, // 選單名稱
hInstance,
NULL);
if (!hWnd) return FALSE;
ShowWindow(hWnd, nCmdShow); // nCmdShow
UpdateWindow(hWnd);
hParent = hWnd;
return TRUE;
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wp, LPARAM lp)
{
switch (msg) {
case WM_CREATE:
PrinterSet();
break;
case WM_CLOSE:
DestroyWindow(hWnd);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return(DefWindowProc(hWnd, msg, wp, lp));
}
return (0L);
}
HDC GetPrintInfo(void)
{
DWORD dwNeeded, dwReturned;
EnumPrinters(PRINTER_ENUM_LOCAL, NULL, 5, NULL, 0, &dwNeeded, &dwReturned);
prninfo = (PRINTER_INFO_5*)GlobalAlloc(GPTR, dwNeeded);
if (EnumPrinters(PRINTER_ENUM_LOCAL, NULL, 5, (LPBYTE)prninfo, dwNeeded, &dwNeeded, &dwReturned)) {
for (DWORD i = 0; i < dwReturned; i++, prninfo++) {
TCHAR szBuf[256];
wsprintf(szBuf, TEXT("x = %s"), prninfo->pPrinterName);
MessageBox(NULL, szBuf, TEXT("Debug"), MB_OK);
}
}
return 0;
}
int PrinterSet()
{
HANDLE hPrint;
GetPrintInfo();
OpenPrinter(prninfo[0].pPrinterName, &hPrint, NULL);
PrinterProperties(hParent, hPrint);
ClosePrinter(hPrint);
return 0;
}