CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Nov 2009

    Printing in C++ Win32


    I was wondering if anyone can help me with a Printing problem I am having its Win32 program using GNU gcc compiler and the codeblocks IDE.

    I am using TextOut to the printer to try to print mutiple lines of text to the printer
    but it just seems to printout two lines of the same text, which is not what i want it to do.

    my code is
    #include <math.h>
    #include <stdlib.h>
    #include <Windows.h>
    #include <iostream>
    #include <string.h>
    #include <Winspool.h>
    #include <CommDlg.h>
    #include <stdio.h>
    #include <wingdi.h>
    #include <winbase.h>

    // Global Variables.

    double CV = 1040;
    double VALUE1 = 3412;
    double Total;
    double Total2;
    double Total3;
    double Rate;
    double n ;
    double n2;
    char EditText[256];
    char QuoteText[256] = "The Result is ";
    char QuoteText2[256] = "The Result is ";
    char QuoteText3[256] = "Gas Flow Rate ";
    int EditTextLength;
    char EditText2[256];
    int EditTextLength2;
    char szAppName [] = TEXT("GasRates");
    int iWidth;
    int iHeight;
    HBITMAP hbm;
    RECT rc;
    HDC hdc ;

    HDC GetPrinterDC (void)

    // Initialize a PRINTDLG object's size and set the PD_RETURNDC flag.
    // The PD_RETURNDC flag tells the dialog to return a printer device context.
    PRINTDLG pd = {0};
    pd.lStructSize = sizeof( pd );
    pd.Flags = PD_RETURNDC;

    // Retrieves the printer DC
    hdc =pd.hDC;
    return hdc ;


    // **********
    // class CRaster
    // - Generic class for BMP raster images.
    class CRaster {
    int Width,Height; // Dimensions
    int BPP; // Bits Per Pixel.
    char * Raster; // Bits of the Image.
    RGBQUAD * Palette; // RGB Palette for the image.
    int BytesPerRow; // Row Width (in bytes).
    BITMAPINFO * pbmi; // BITMAPINFO structure

    // Member functions (defined later):
    int LoadBMP (char * szFile);
    int GDIPaint (HDC hdc,int x,int y);
    // **********
    // Windows Main Function.
    // - Here starts our demo program
    int WINAPI WinMain ( HINSTANCE hInstance, HINSTANCE hPrevInstance,
    LPSTR lpCmdLine, int nCmdShow )

    HWND hwnd;
    MSG msg;

    WNDCLASS wc;
    wc.style = CS_HREDRAW|CS_VREDRAW;
    wc.lpfnWndProc = WindowProc;
    wc.cbClsExtra = 0;
    wc.cbWndExtra = 0;
    wc.hInstance = hInstance;

    wc.hIcon = LoadIcon (hInstance, IDI_WINLOGO);
    wc.hCursor = LoadCursor (NULL, IDC_ARROW);
    wc.hbrBackground = (HBRUSH) (COLOR_WINDOW+1);
    wc.lpszMenuName = NULL;
    wc.lpszClassName = szAppName;

    RegisterClass (&wc);

    hwnd = CreateWindow (szAppName,"Gas Rate Calculator",WS_OVERLAPPED|WS_CAPTION|WS_SYSMENU|WS_MINIMIZEBOX,

    ShowWindow (hwnd,nCmdShow);
    UpdateWindow (hwnd);

    while (GetMessage(&msg,0,0,0))

    if(!IsDialogMessage(hwnd, &msg))
    TranslateMessage (&msg);

    DispatchMessage (&msg);
    return msg.wParam;

    // **********
    // Main Window Procedure.
    // - Processes Window Messages

    (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)

    static HWND hwndEdit;
    static HWND hwndEdit2;
    static HWND hwndButton;
    static HWND hwndButton2;
    static HWND hwndButton3;
    static HWND hwndLabel;
    static HWND hwndLabel2;
    static HWND hwndLabel3;
    static HWND hwndLabel4;
    static HWND hwndLabel5;
    static HBITMAP hBitmap;
    static int cxSource, cySource;
    HDC Hdc, hdcMem;
    HINSTANCE hInstance;
    BITMAP bitmap;
    HBRUSH NewBrush;
    NewBrush = CreateSolidBrush(RGB(255, 0, 0));
    RECT rect ;
    rect.bottom = 160;
    rect.left = 160;
    rect.right = 160;
    rect.top = 160;
    switch (message)
    case WM_CREATE:
    //load bitmap from resource

    hInstance = ((LPCREATESTRUCT) lParam) ->hInstance;
    hBitmap = LoadBitmap (hInstance,MAKEINTRESOURCE(3));
    GetObject (hBitmap, sizeof (BITMAP), &bitmap);
    cxSource = bitmap.bmWidth;
    cySource = bitmap.bmHeight;

    HICON hICon;
    hICon = LoadIcon(GetModuleHandle(NULL), MAKEINTRESOURCE(2));

    SetClassLong(hwnd, // window handle
    GCL_HICON, // changes icon
    (LONG) hICon); // The new icon

    PostMessage (hwnd, WM_SETICON, ICON_BIG, (LPARAM) hICon);
    hwndLabel5 = CreateWindow ( TEXT ("static"),"",WS_CHILD|ES_LEFT|1,0,400,200,30,hwnd,(HMENU) 9,((LPCREATESTRUCT) lParam) -> hInstance,NULL);
    hwndLabel4 = CreateWindow ( TEXT ("static"),"",WS_CHILD|ES_LEFT|1,0,350,200,30,hwnd,(HMENU) 8,((LPCREATESTRUCT) lParam) -> hInstance,NULL);
    hwndLabel3 = CreateWindow ( TEXT ("static"),"",WS_CHILD|ES_LEFT|1,0,300,200,25,hwnd,(HMENU) 7,((LPCREATESTRUCT) lParam) -> hInstance,NULL);
    hwndLabel2 = CreateWindow ( TEXT ("static"),"Enter Time Taken in Seconds.",WS_CHILD|WS_VISIBLE|ES_LEFT|1,7,350,200,25,hwnd,(HMENU) 5,((LPCREATESTRUCT) lParam) -> hInstance,NULL);
    hwndLabel = CreateWindow ( TEXT ("static"),"Enter Volume in Cubic Feet.",WS_CHILD|WS_VISIBLE|ES_LEFT|1,7,260,180,25,hwnd,(HMENU) 6,((LPCREATESTRUCT) lParam) -> hInstance,NULL);
    hwndButton = CreateWindow ( TEXT ("button"),"Calculate",WS_CHILD|WS_VISIBLE|WS_TABSTOP|1,55,500,100,25,hwnd,(HMENU) 3,((LPCREATESTRUCT) lParam) -> hInstance,NULL);
    hwndButton2 = CreateWindow ( TEXT ("button"),"Back",WS_CHILD|1,55,500,100,25,hwnd,(HMENU) 4,((LPCREATESTRUCT) lParam) -> hInstance,NULL);
    hwndButton3 = CreateWindow ( TEXT ("button"),"Print",WS_CHILD|1,55,465,100,25,hwnd,(HMENU) 10,((LPCREATESTRUCT) lParam) -> hInstance,NULL);
    hwndEdit = CreateWindow (TEXT ("edit"),NULL,WS_CHILD| WS_VISIBLE| ES_LEFT|WS_BORDER| WS_TABSTOP,20, 300, 50, 20, hwnd,
    (HMENU) 1,((LPCREATESTRUCT) lParam) -> hInstance ,NULL);
    hwndEdit2 = CreateWindow (TEXT ("edit"),NULL,WS_CHILD| WS_VISIBLE| ES_LEFT| WS_BORDER| WS_TABSTOP,20, 370, 75, 20, hwnd,
    (HMENU) 2,((LPCREATESTRUCT) lParam) -> hInstance ,NULL);

    SetFocus (hwndEdit);

    return 0;

    return (LRESULT)CreateSolidBrush(0xFFFFFF);

    case WM_SETFOCUS:
    SetFocus (hwndEdit);
    return 0 ;

    case WM_COMMAND:

    switch (wParam)
    case 3 :
    EditTextLength = GetWindowTextLength(hwndEdit) +1;
    GetWindowText(hwndEdit, EditText, EditTextLength);

    EditTextLength2 = GetWindowTextLength(hwndEdit2) +1;
    GetWindowText(hwndEdit2, EditText2, EditTextLength2);

    ShowWindow(hwndEdit, SW_HIDE);
    ShowWindow(hwndEdit2, SW_HIDE);
    ShowWindow(hwndButton, SW_HIDE);
    ShowWindow(hwndLabel, SW_HIDE);
    ShowWindow(hwndLabel2, SW_HIDE);

    ShowWindow(hwndButton2, SW_SHOW);
    ShowWindow(hwndButton3, SW_SHOW);
    ShowWindow(hwndLabel3, SW_SHOW);
    ShowWindow(hwndLabel4, SW_SHOW);
    ShowWindow(hwndLabel5, SW_SHOW);
    n = atof( EditText );

    n2= atof( EditText2 );
    if (n <=0) break;
    if (n2 <=0) break;
    Rate = n2 / n;
    Total = (3600*CV)/(VALUE1*Rate);
    Total2 = (Total*VALUE1);
    Total3 = (Total/10.7);
    sprintf (EditText, "%.2f", Total);
    strcat (QuoteText, EditText);
    strcat (QuoteText, " Kw/h.");
    SetWindowText (hwndLabel3, QuoteText);

    sprintf (EditText, "%.2f", Total2);
    strcat (QuoteText2, EditText);
    strcat (QuoteText2, " BTU/h.");
    SetWindowText (hwndLabel4, QuoteText2);

    sprintf (EditText, "%.2f", Total3);
    strcat (QuoteText3, EditText);
    strcat (QuoteText3, " m/h.");
    SetWindowText (hwndLabel5, QuoteText3);
    break ;

    case 4 :
    ShowWindow(hwndEdit, SW_SHOW);
    ShowWindow(hwndEdit2, SW_SHOW);
    ShowWindow(hwndButton, SW_SHOW);
    ShowWindow(hwndLabel2, SW_SHOW);
    SetWindowText (hwndEdit, "");
    SetWindowText (hwndEdit2, "");
    ShowWindow(hwndButton2, SW_HIDE);
    ShowWindow(hwndButton3, SW_HIDE);
    strcpy (QuoteText, "The Result is ");
    SetWindowText (hwndLabel3, QuoteText);

    strcpy (QuoteText2, "The Result is ");
    SetWindowText (hwndLabel4, QuoteText2);

    strcpy (QuoteText3, "Gas Flow Rate ");
    SetWindowText (hwndLabel5, QuoteText3);
    ShowWindow(hwndLabel3, SW_HIDE);
    ShowWindow(hwndLabel4, SW_HIDE);
    ShowWindow(hwndLabel5, SW_HIDE);
    break ;

    case 10:

    ShowWindow(hwndButton3, SW_HIDE);
    char szTextStr[]= "";
    char szTextStr2[]= "";

    HDC prn ;


    prn = GetPrinterDC();

    // Prepare the DOCINFO.
    static DOCINFO di = { sizeof (DOCINFO), TEXT ("Gas Calculator : Printing...")};
    StartDoc (prn, &di);

    StartPage (prn) ;

    TextOut(prn, 650,350,szTextStr, lstrlen (szTextStr));
    TextOut(prn, 650,550,szTextStr2, lstrlen (szTextStr2));

    EndPage (prn);



    ShowWindow(hwndButton3, SW_SHOW);


    return 0 ;

    case WM_PAINT:

    Hdc=BeginPaint (hwnd,&ps);
    hdcMem = CreateCompatibleDC (Hdc);

    SelectObject(hdcMem, hBitmap);


    EndPaint (hwnd, &ps) ;

    return 0;
    case WM_DESTROY:
    PostQuitMessage (0);
    return 0;
    return DefWindowProc (hwnd,message,wParam,lParam);

  2. #2
    Join Date
    Feb 2005

    Re: Printing in C++ Win32

    Please use code tags--especially with this much code.

    It looks like your problem is here:

    char szTextStr[]= "";
    char szTextStr2[]= "";
    HDC prn ;
    Your essentially declaring each of these strings as an array of one character (the terminating NULL in the empty string. You're then trying to concatenate presumably more text than an empty sting further down. You're overflowing the array resulting in undefined behavior.

    Allocate at least as much as your largest expected string:

    char szTextStr[100]= "";
    char szTextStr2[100]= "";

  3. #3
    Join Date
    Nov 2009

    Talking Re: Printing in C++ Win32

    Thanks for the reply that has sorted my little problem out.

    Sorry for not using code tags but I am a newbie to the site.

    If you dont mind telling me how do i use code tags ?

    Also I cant believe I made such a simple mistake. I am just learning C++
    but even I should know that one.

    Thanks for pointing it out so quickly
    my Program works perfectly now.


  4. #4
    Join Date
    Feb 2005

    Re: Printing in C++ Win32

    For code tags, just use [code] and [/code] around your code.

    typing this in the editor:

    int main(void)
    return ERROR_SUCCESS;

    Show up like this:

    int main(void)
      return ERROR_SUCCESS;

  5. #5
    Join Date
    Oct 2009

    Smile Re: Printing in C++ Win32

    Hi Snoopy07
    With the additions I made to yor code it prints 3 posts of result.
    It gave me a clue as to develop my own calculator. Thanks!


    case 10:

    ShowWindow(hwndButton3, SW_HIDE);
    char szTextStr[100]= "";
    char szTextStr2[100]= "";
    char szTextStr3[100]= ""; /* "Added line" */

    HDC prn ;

    strcat(szTextStr3,QuoteText3); /* "Added line" */

    prn = GetPrinterDC();

    // Prepare the DOCINFO.
    static DOCINFO di = { sizeof (DOCINFO), TEXT ("Gas Calculator : Printing...")};
    StartDoc (prn, &di);

    StartPage (prn) ;

    TextOut(prn, 650,350,szTextStr, lstrlen (szTextStr));
    TextOut(prn, 650,550,szTextStr2, lstrlen (szTextStr2));
    TextOut(prn, 650,750,szTextStr3, lstrlen (szTextStr3)); /* "Added line" */

    EndPage (prn);


Posting Permissions

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

Windows Mobile Development Center

Click Here to Expand Forum to Full Width

On-Demand Webinars (sponsored)

We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.