我需要实现一个功能,输入文档(比如:*.dc文件)路径.点击打印。就直接选定某一打印机直接打印,不弹出打印对话框。要求用ms api实现。诚求高人指点,谢谢~

解决方案 »

  1.   

    一个小例子:
    准备过程:char        devstring[256];      // array for WIN.INI data 
        HANDLE      printer;
        LPDEVMODE   devMode = NULL;
        DWORD       structSize, returnCode;   GetProfileString("Devices", printerName, "", devstring, sizeof(devstring));
        char *driver = strtok (devstring, (const char *) ",");
        char *port = strtok((char *) NULL, (const char *) ",");    if (!driver || !port) {
            MessageBox(win->hwndFrame, "Printer with given name doesn't exist", "Printing problem.", MB_ICONEXCLAMATION | MB_OK);
            return;
        }
        
        BOOL fOk = OpenPrinter((LPSTR)printerName, &printer, NULL);
        if (!fOk) {
            MessageBoxW(win->hwndFrame, _TRW("Could not open Printer"), _TRW("Printing problem."), MB_ICONEXCLAMATION | MB_OK);
            return;
        }    HDC  hdcPrint = NULL;
        structSize = DocumentProperties(NULL,
            printer,                /* Handle to our printer. */ 
            (LPSTR) printerName,    /* Name of the printer. */ 
            NULL,                   /* Asking for size, so */ 
            NULL,                   /* these are not used. */ 
            0);                     /* Zero returns buffer size. */ 
        devMode = (LPDEVMODE)malloc(structSize);
        if (!devMode) goto Exit;    // Get the default DevMode for the printer and modify it for your needs.
        returnCode = DocumentProperties(NULL,
            printer,
            (LPSTR) printerName,
            devMode,        /* The address of the buffer to fill. */ 
            NULL,           /* Not using the input buffer. */ 
            DM_OUT_BUFFER); /* Have the output buffer filled. */     if (IDOK != returnCode) {
            // If failure, inform the user, cleanup and return failure.
            MessageBox(win->hwndFrame, "Could not obtain Printer properties", "Printing problem.", MB_ICONEXCLAMATION | MB_OK);
            goto Exit;     DocumentProperties(NULL,
            printer,
            (LPSTR) printerName,
            devMode,        /* Reuse our buffer for output. */ 
            devMode,        /* Pass the driver our changes. */ 
            DM_IN_BUFFER |  /* Commands to Merge our changes and */ 
            DM_OUT_BUFFER); /* write the result. */     ClosePrinter(printer);    hdcPrint = CreateDC(driver, printerName, port, devMode); 
        if (!hdcPrint) {
            MessageBox(win->hwndFrame, "Couldn't initialize printer", "Printing problem.", MB_ICONEXCLAMATION | MB_OK);
            goto Exit;
        }
        PRINTPAGERANGE pr;
        pr.nFromPage =1;
        pr.nToPage =pagecount();下面是打印过程: StartPage(hdcPrint);//使用hdc打印,,,,应该是个循环,多页
    .....................            if (EndPage(hdcPrint) <= 0) {
                    AbortDoc(hdcPrint);
                    return;
                }
    EndDoc(hdcPrint);