CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Hybrid View

  1. #1
    Join Date
    Sep 2021
    Posts
    11

    Printing in Windows

    Hi
    I am trying to learn how to print the content of a window. By now I've ended up with the following code (it should just draw a line as a test print). When executing it, the printer starts working but gives out an empty page. If I choose deskPDF Creator instead of a printer, a pdf file is created, however when trying to open it the pdf reader reports an "internal error".
    What am I missing here?


    Code:
    PRINTDLG pd;
    DOCINFO di = {0};
    HPEN hpen;
    .......
    di.cbSize = sizeof(DOCINFO);
    di.lpszDocName = "Printout";
    hpen = CreatePen(PS_SOLID, 2, RGB(256,0,0));
    // Initialize PRINTDLG
    ZeroMemory(&pd, sizeof(pd));
    pd.lStructSize = sizeof(pd);
    pd.hwndOwner   = hwnd;
    pd.hDevMode    = NULL;     // Don't forget to free or store hDevMode.
    pd.hDevNames   = NULL;     // Don't forget to free or store hDevNames.
    pd.Flags       = PD_USEDEVMODECOPIESANDCOLLATE | PD_RETURNDC;
    pd.nCopies     = 1;
    pd.nFromPage   = 1;
    pd.nToPage     = 1;
    pd.nMinPage    = 1;
    pd.nMaxPage    = 1;
    
    PrintDlg(&pd);
    SelectObject(pd.hDC, hpen);
    StartDoc(pd.hDC, &di);
    StartPage(pd.hDC);
    MoveToEx(pd.hDC, 10,100,NULL);
    LineTo(pd.hDC,20,200);
    EndPage(pd.hDC);
    EndDoc(pd.hDC);
    DeleteObject(hpen);

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,109

    Re: Printing in Windows

    Are you sure the points (10, 100) and (20, 200) are within the printing area?
    Victor Nijegorodov

  3. #3
    Join Date
    Sep 2021
    Posts
    11

    Re: Printing in Windows

    I guess so, all printers have much higher resolution so I would expect this line to be very short and within the printed area.
    I also tried copying the whole window into a virtual bitmap and then copying that bitmap onto the printer's DC using BitBlt. The result is the same. If it was a problem of resolution mismatch I would expect at least a part of the window to be printed and there would not be "internal error" in the created pdf file.

  4. #4
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,109

    Re: Printing in Windows

    Did you debug your code?
    What do all these functions:
    PrintDlg
    StartDoc
    StartPage
    EndPage
    EndDoc
    return?
    Victor Nijegorodov

  5. #5
    Join Date
    Sep 2021
    Posts
    11

    Re: Printing in Windows

    I've done it now, they all return TRUE.
    It looks like all these function do exactly what is expected - the correct printer gets selected and it does print a page (an empty one though). So it looks like the problem is with drawing to the printer's DC

  6. #6
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,109

    Re: Printing in Windows

    Then try to use OpenPrinter function rather than the PrintDlg
    Victor Nijegorodov

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