CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Apr 1999
    Posts
    51

    Attaching Printer to CDC

    How can I attach a printer to a CDC object without calling CPrintDialog and the CDC.Attach(GetPrinterDC())? Normally, you would have something like this:

    CDC dc;
    CPrintDialog printDlg(FALSE);

    // ask the user to select a printer
    if (printDlg.DoModal() == IDCANCEL)
    return;

    // Attach a printer DC
    dc.Attach(printDlg.GetPrinterDC());

    I already know what the printer device name is and I just want to assign that and bypass the dialog asking the user to select the printer. I think I need to eliminate the DoModal() and call printDlg.CreatePrinterDC() but I am not sure how to initialize the DEVMODE and/or DEVNAMES structures and how they are passed or utilized.

    Also, how do you change the print margins after you have the printer DC?

    Any info would be greatly appreciated.


    Thanks,

    Steve


    Steven M. McNeese
    http://[email protected]

  2. #2
    Join Date
    Apr 1999
    Location
    Miami, FL
    Posts
    67

    Re: Attaching Printer to CDC

    Here's a function that should do what you want:

    HDC CreatePrinterDC(LPCTSTR szPrinterName)
    {
    PDEVMODE pDevMode = NULL;

    LONG lDevModeSize = :ocumentProperties(NULL, NULL, (LPTSTR)szPrinterName, NULL, NULL, 0);
    if (lDevModeSize > 0)
    {
    HANDLE hDevMode = ::GlobalAlloc(GHND, lDevModeSize);
    pDevMode = (PDEVMODE) ::GlobalLock(hDevMode);
    :ocumentProperties(GetDesktopWindow(), NULL, (LPTSTR)szPrinterName, pDevMode, NULL, DM_OUT_BUFFER);
    }
    else
    return NULL;

    // Here you can change the margins, orientation, etc. by manipulating pDevMode.

    // Now get a DC for the printer
    HDC hDC = ::CreateDC(NULL, szPrinterName, NULL, pDevMode);

    ::GlobalFree((HANDLE)pDevMode);
    return hDC;
    }



    I don't know how to change the margins after the DC is created but you can certainly set them beforehand (via the DEVMODE pointer).

    Good luck!

    Alvaro

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured