作者 主題: 請問如何使用 EnumPrinters 抓取 印表機名稱?  (閱讀 4676 次)

0 會員 與 1 訪客 正在閱讀本文。

yehchge

  • 可愛的小學生
  • *
  • 文章數: 9
    • 檢視個人資料
請問如何使用 EnumPrinters 抓取 印表機名稱?? (PS: 照理說應該會出現印表機的設定畫面才對啊?!) 都沒有任何錯誤 !

以下是範例, 一直不知道要如何才可以抓的到 ??

代碼: [選擇]

// 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[3];

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;
    if (EnumPrinters(PRINTER_ENUM_DEFAULT, NULL, 5, (LPBYTE)&prninfo, sizeof(prninfo), &dwNeeded, &dwReturned)) {
        MessageBox(NULL,TEXT(prninfo[0].pPrinterName),"印表機名稱",MB_OK);
        return CreateDC(NULL, prninfo[0].pPrinterName, NULL, NULL);
    } else {
        TCHAR szBuf[256];
        wsprintf(szBuf, TEXT("x = %s"), GetLastError());
        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;
}


yehchge

  • 可愛的小學生
  • *
  • 文章數: 9
    • 檢視個人資料
已經可以了, 列出完整程式分享!!
代碼: [選擇]
// 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;
}