Results 1 to 3 of 3

Thread: Help With PrintDlg.. I Already Have Some Code...

Hybrid View

  1. #1

    Thread Starter
    Hyperactive Member AAG's Avatar
    Join Date
    Aug 2000
    Location
    United States
    Posts
    411

    Help With PrintDlg.. I Already Have Some Code...

    How do i print ??? i have the print dialog displaying but i have no idea how to print.. i have some visual basic code but I can't seem to translate it properly to c++...

    Code:
    void Print_Dialog()
    {
    	PRINTDLG pd;
    	ZeroMemory(&pd, sizeof(PRINTDLG));
    	pd.lStructSize = sizeof(PRINTDLG);
    	pd.hwndOwner   = Main_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     = 15; 
    	pd.nMinPage    = 1; 
    	pd.nMaxPage    = 15; 
    
    	if (PrintDlg(&pd) == TRUE) 
    	{
    
    
    		//pd.hDevMode
    		DeleteDC(pd.hDC);
    	}
    }
    This Business Is Binary. Your a 1 or a 0. Alive or Dead. (AntiTrust)

  2. #2
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    From the ever helpful MSDN:
    Platform SDK: Windows GDI
    Printing a Document
    Once the application initializes the necessary variables, registers its AbortProc function, and displays its modeless Cancel dialog box, it can start the print job by calling the StartDoc function.

    After the application begins a print job, it can define individual pages in the document by calling the StartPage and EndPage functions and embedding the appropriate calls to GDI drawing functions within this bracket. After the application has defined the last page, it can close the document and end the print job by calling the EndDoc function.

    The following example shows the code required to print a string of text and a bitmapped image. The string of text, centered at the top of the page, identifies the path and file name for the file that contains the bitmapped image. The bitmapped image, centered vertically and horizontally on the page, is drawn so that the same proportions used to draw the image in the application's window are maintained. Note that when targetting a printer DC, use a DIB instead of a DDB -- that is why StretchDIBits is used in this code.

    // Zero and then initialize the members of a DOCINFO structure.

    memset( &di, 0, sizeof(DOCINFO) );
    di.cbSize = sizeof(DOCINFO);
    di.lpszDocName = "Bitmap Printing Test";
    di.lpszOutput = (LPTSTR) NULL;
    di.lpszDataType = (LPTSTR) NULL;
    di.fwType = 0;

    // Begin a print job by calling the StartDoc function.

    nError = StartDoc(pd.hDC, &di);
    if (nError == SP_ERROR)
    {
    errhandler("StartDoc", hwnd);
    goto Error;
    }

    // Inform the driver that the application is about to begin
    // sending data.

    nError = StartPage(pd.hDC);
    if (nError <= 0)
    {
    errhandler("StartPage", hwnd);
    goto Error;
    }

    // Retrieve the number of pixels-per-logical-inch in the
    // horizontal and vertical directions for the display upon which
    // the bitmap was created. These are likely the same as for
    // the present display, so we use those values here.

    hWinDC = GetDC(hWnd);
    fLogPelsX1 = (float) GetDeviceCaps(hWinDC, LOGPIXELSX);
    fLogPelsY1 = (float) GetDeviceCaps(hWindDC, LOGPIXELSY);

    // Retrieve the number of pixels-per-logical-inch in the
    // horizontal and vertical directions for the printer upon which
    // the bitmap will be printed.

    fLogPelsX2 = (float) GetDeviceCaps(pd.hDC, LOGPIXELSX);
    fLogPelsY2 = (float) GetDeviceCaps(pd.hDC, LOGPIXELSY);

    // Determine the scaling factors required to print the bitmap and
    // retain its original proportions.

    if (fLogPelsX1 > fLogPelsX2)
    fScaleX = (fLogPelsX1 / fLogPelsX2);
    else fScaleX = (fLogPelsX2 / fLogPelsX1);

    if (fLogPelsY1 > fLogPelsY2)
    fScaleY = (fLogPelsY1 / fLogPelsY2);
    else fScaleY = (fLogPelsY2 / fLogPelsY1);

    // Compute the coordinates of the upper left corner of the
    // centered bitmap.

    cWidthPels = GetDeviceCaps(pd.hDC, HORZRES);
    xLeft = ((cWidthPels / 2) - ((int) (((float) bmih.biWidth)
    * fScaleX)) / 2);
    cHeightPels = GetDeviceCaps(pd.hDC, VERTRES);
    yTop = ((cHeightPels / 2) - ((int) (((float) bmih.biHeight)
    * fScaleY)) / 2);

    // Use StretchDIBits to scale the bitmap and maintain
    // its original proportions (that is, if the bitmap was square
    // when it appeared in the application's client area, it should
    // also appear square on the page).

    if (StretchDIBits(pd.hDC, xLeft, yTop, (int) ((float) bmih.biWidth
    * fScaleX), (int) ((float) bmih.biHeight * fScaleY), 0, 0,
    bmih.biWidth, bmih.biHeight, lpBits, lpBitsInfo, iUsage,
    SRCCOPY) == GDI_ERROR)
    {
    errhandler("StretchDIBits Failed", hwnd);
    }


    // Retrieve the width of the string that specifies the full path
    // and filename for the file that contains the bitmap.

    GetTextExtentPoint32(pd.hDC, ofn.lpstrFile,
    ofn.nFileExtension + 3, &szMetric);

    // Compute the starting point for the text-output operation. The
    // string will be centered horizontally and positioned three lines
    // down from the top of the page.

    xLeft = ((cWidthPels / 2) - (szMetric.cx / 2));
    yTop = (szMetric.cy * 3);

    // Print the path and filename for the bitmap, centered at the top
    // of the page.

    TextOut(pd.hDC, xLeft, yTop, ofn.lpstrFile,
    ofn.nFileExtension + 3);

    // Determine whether the user has pressed the Cancel button in the
    // AbortPrintJob dialog box; if the button has been pressed, call
    // the AbortDoc function. Otherwise, inform the spooler that the
    // page is complete.

    nError = EndPage(pd.hDC);

    if (nError <= 0)
    {
    errhandler("EndPage", hwnd);
    goto Error;
    }

    // Inform the driver that document has ended.

    nError = EndDoc(pd.hDC);
    if (nError <= 0)
    errhandler("EndDoc", hwnd);

    Error:
    // Enable the application's window.

    EnableWindow(hwnd, TRUE);

    // Remove the AbortPrintJob dialog box.

    DestroyWindow(hdlgCancel);

    // Delete the printer DC.

    DeleteDC(pd.hDC);

    Because the pixels on a screen typically have different dimensions than the dots on a printer, it is necessary to scale bitmapped images to obtain a WYSIWYG effect. This is done by obtaining horizontal and vertical scaling factors and then applying those factors to the width and height values passed to the StretchDIBits function. In the sample application, the scaling factors were obtained by retrieving the horizontal and vertical logical-pixel count for the two devices. Once the scaling factors were obtained, they were used to adjust the bitmap width and height.

    To center the bitmap on the page, the application first computed the width and height of the scaled bitmap. (The bitmap was scaled to maintain the original proportions of the image.) These values were divided by two and then subtracted from half of the width and height of the page. The result defines the coordinates of the upper-left corner of the bitmap.

    To center the text at the top of the page, the application called the GetTextExtentPoint32 function to retrieve the width and height of the string specifying the path names and file names. Once these values were obtained, the application used the height to position the string three lines down the page and the width to position the string horizontally centered on the page.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  3. #3

    Thread Starter
    Hyperactive Member AAG's Avatar
    Join Date
    Aug 2000
    Location
    United States
    Posts
    411
    thanx, i'll try it out in a few minutes....
    This Business Is Binary. Your a 1 or a 0. Alive or Dead. (AntiTrust)

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