OpenPrinter

This is a discussion on OpenPrinter within the Windows Programming forums, part of the Platform Specific Boards category; I'm assuming you got the code sample, but I'll repost it anyway. Code: #include <windows.h> int main(void) { HDC hdc ...

  1. #16
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    I'm assuming you got the code sample, but I'll repost it anyway.
    Code:
    #include <windows.h>
    
    int main(void)
    {
        HDC        hdc   = NULL;
        DOCINFO    di    = { 0 };
        TEXTMETRIC tm    = { 0 };
        ULONG      y     = 10;
    
        /* Get a device context for the printer. We should use PrintDlg to retrive an HDC
         * or at least GetDefaultPrinter to get the printer name to use. */
        hdc = CreateDC(TEXT("WINSPOOL"), TEXT("HP DeskJet 712C"), NULL, NULL);
    
        /* Start the document. */
        di.cbSize      = sizeof(DOCINFO); 
        di.lpszDocName = TEXT("Print Test"); 
        di.fwType      = 0; 
        StartDoc(hdc, &di);
    
        StartPage(hdc); /* Start a page. */
    
        /* Set the alignment mode so we can output text and it will automatically be horizontally positioned. */
        SetTextAlign(hdc, GetTextAlign(hdc) | TA_UPDATECP);
    
        /* Get the height of text. */
        GetTextMetrics(hdc, &tm);
    
        /* Set start position. */
        MoveToEx(hdc, 10, y, NULL);
    
        /* We can now use the GDI functions to output content. */
        TextOut(hdc, 0, 0, TEXT("Hello World"), lstrlen(TEXT("Hello World")));
    
        /* Move to new line. */
        MoveToEx(hdc, 10, y += tm.tmHeight + tm.tmExternalLeading, NULL);
    
        TextOut(hdc, 0, 0, TEXT("Bye World. "), lstrlen(TEXT("Bye World ")));
        TextOut(hdc, 0, 0, TEXT("Maybe not!"), lstrlen(TEXT("Maybe not!")));
    
        /* Move to new line. */
        MoveToEx(hdc, 10, y += tm.tmHeight + tm.tmExternalLeading, NULL);
    
        TextOut(hdc, 0, 0, TEXT("OK Now!"), lstrlen(TEXT("OK Now!")));
    
        EndPage(hdc);  /* End page. */
        EndDoc(hdc);   /* End document. */
        DeleteDC(hdc); /* Cleanup hdc. */
    
        return 0;
    }

  2. #17
    Master of Puppets rwmarsh's Avatar
    Join Date
    Feb 2006
    Location
    Texas
    Posts
    96
    Yeah, I got it, Thanks.
    Don't know what happened with all the other posts that were here, seems like 2 or 3 disapeared!
    Using DEV-C++ Under Windows XP
    +------------------------------+

    "No! Do, or Do Not. There is no Try..."

  3. #18
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Just out of curiosity... On my Inkjet printer each character printed has its own width. If I wanted to print, say a report with formated output where each word on a line was lined up to create multiple columns, would I use TabbedTextOut or can I just change settings in the TextMetrics structure? Or is there an easier way?
    Yes, TabbedTextOut would probably be a good idea. The other alternative is to do it manually. You can move around using the MoveToEx function. You can get the length of a specific string by using the GetTextExtentPoint32 function.

    The width of a character is not determined by the printer, but by what font you are using. The font can be changed. Search the board for CreateFont for examples.

  4. #19
    Master of Puppets rwmarsh's Avatar
    Join Date
    Feb 2006
    Location
    Texas
    Posts
    96
    Thats what I figured. Thats probably a little out of my ability right now but after I get more knowlegible with win api I might be able to figure it out. At least I know it can be done if I ever have to do it in the future :P
    Using DEV-C++ Under Windows XP
    +------------------------------+

    "No! Do, or Do Not. There is no Try..."

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. OpenPrinter function call troubles
    By stanlvw in forum Windows Programming
    Replies: 0
    Last Post: 06-05-2008, 04:23 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21