知道某个打印机名称,怎么获得它的dc因为不能用打印对话框。因为的程序是在后台运行的。

解决方案 »

  1.   


    PrintDialog BS = new PrintDialog();
    int x = BS.PrinterSettings.DefaultPageSettings.PaperSize.Width;//打印机默认纸张大小
    int y = BS.PrinterSettings.DefaultPageSettings.PaperSize.Height;
    Image memoryImage = Image.FromFile(Application.StartupPath + @"\Image\back.jpg");//图片路径
    Rectangle destRect = new Rectangle(0, 0, x, y);//设置要图片铺满的矩形区域
    e.Graphics.DrawImage(memoryImage, destRect, 0, 0, memoryImage.Width, memoryImage.Height, System.Drawing.GraphicsUnit.Pixel);
      

  2.   

    CPrintDialog::GetPrinterDCRetrieves a handle to the printer device context.
      

  3.   

    http://support.microsoft.com/kb/112641/en-us我找到相关的文档了
      

  4.   


    extern "C"  __declspec(dllexport) BOOL GetPrinterDevice(LPTSTR pszPrinterName,enum XlPaperSize xlPage, HGLOBAL* phDevNames, HGLOBAL* phDevMode)
    {
    // if NULL is passed, then assume we are setting app object's
    // devmode and devnames
    if (phDevMode == NULL || phDevNames == NULL)
    return FALSE; // Open printer
    HANDLE hPrinter;
    if (OpenPrinter(pszPrinterName, &hPrinter, NULL) == FALSE)
    return FALSE; // obtain PRINTER_INFO_2 structure and close printer
    DWORD dwBytesReturned, dwBytesNeeded;
    GetPrinter(hPrinter, 2, NULL, 0, &dwBytesNeeded);
    PRINTER_INFO_2* p2 = (PRINTER_INFO_2*)GlobalAlloc(GPTR,
    dwBytesNeeded);
    if (GetPrinter(hPrinter, 2, (LPBYTE)p2, dwBytesNeeded,
    &dwBytesReturned) == 0) {
    GlobalFree(p2);
    ClosePrinter(hPrinter);
    return FALSE;
    }
    ClosePrinter(hPrinter); // Allocate a global handle for DEVMODE
    HGLOBAL  hDevMode = GlobalAlloc(GHND, sizeof(*p2->pDevMode) + p2->pDevMode->dmDriverExtra);
    // ASSERT(hDevMode);
    DEVMODE* pDevMode = (DEVMODE*)GlobalLock(hDevMode);
    // ASSERT(pDevMode); float fwidth ; 
    float fheight ;
    (*((*(p2)).pDevMode)).dmPaperSize=xlPage;
     GetPaperSize ( xlPage, &fwidth, &fheight );
     (*((*(p2)).pDevMode)).dmPaperWidth = fwidth*100;
     (*((*(p2)).pDevMode)).dmPaperLength =fheight*100; // copy DEVMODE data from PRINTER_INFO_2::pDevMode
    memcpy(pDevMode, p2->pDevMode, sizeof(*p2->pDevMode) + p2->pDevMode->dmDriverExtra);
    GlobalUnlock(hDevMode); // Compute size of DEVNAMES structure from PRINTER_INFO_2's data
    DWORD drvNameLen = lstrlen(p2->pDriverName)+1;  // driver name
    DWORD ptrNameLen = lstrlen(p2->pPrinterName)+1; // printer name
    DWORD porNameLen = lstrlen(p2->pPortName)+1;    // port name if (pDevMode->dmFields & DM_ORIENTATION)
    {
    // Pass lpDevMode as both the input and output
    // DEVMODE buffers. It is important to pass
    // in the full DEVMODE from the previous call
    // to ExtDeviceMode() as the input buffer because
    // it has been completely initialized by the
    // driver. If you do not do this, the results
    // are sporadic--sometimes it works and sometimes
    // it doesn't depending on the printer driver and
    // the setting you are trying to change. // Zero out all the fields and then set the bit(s)
    // for the field(s) we want to change.
    pDevMode->dmFields = 0;
    pDevMode->dmFields = DM_ORIENTATION; // Change to landscape.
    pDevMode->dmOrientation = DMORIENT_LANDSCAPE; // Call ExtDeviceMode() once more to allow the driver
    // to change the device-dependent portion of the
    // DEVMODE buffer if it needs to. // aka DM_COPY | DM_MODIFY   hdc = CreateDC(p2->pDriverName,p2->pPrinterName,p2->pPortName,pDevMode);
      int a=33; }
    else
    {
    // The printer doesn't support the field you're
    // trying to change. Just use the current printer
    // settings.
    HDC hdc = CreateDC(p2->pDriverName,p2->pPrinterName,p2->pPortName,pDevMode);
    }
    // Allocate a global handle big enough to hold DEVNAMES.
    HGLOBAL hDevNames = GlobalAlloc(GHND,sizeof(DEVNAMES) + (drvNameLen + ptrNameLen + porNameLen)*sizeof(TCHAR));
    // ASSERT(hDevNames);
    DEVNAMES* pDevNames = (DEVNAMES*)GlobalLock(hDevNames);
    // ASSERT(pDevNames); // Copy the DEVNAMES information from PRINTER_INFO_2
    // tcOffset = TCHAR Offset into structure
    int tcOffset = sizeof(DEVNAMES)/sizeof(TCHAR);
    //ASSERT(sizeof(DEVNAMES) == tcOffset*sizeof(TCHAR)); pDevNames->wDriverOffset = tcOffset;
    memcpy((LPTSTR)pDevNames + tcOffset, p2->pDriverName,drvNameLen*sizeof(TCHAR));
    tcOffset += drvNameLen; pDevNames->wDeviceOffset = tcOffset;memcpy((LPTSTR)pDevNames + tcOffset, p2->pPrinterName, ptrNameLen*sizeof(TCHAR));
    tcOffset += ptrNameLen; pDevNames->wOutputOffset = tcOffset;memcpy((LPTSTR)pDevNames + tcOffset, p2->pPortName, porNameLen*sizeof(TCHAR));
    pDevNames->wDefault = 0; GlobalUnlock(hDevNames);
    GlobalFree(p2);   // free PRINTER_INFO_2 // set the new hDevMode and hDevNames
    *phDevMode = hDevMode;
    *phDevNames = hDevNames;
    return TRUE;
    }