Displaying the Print Dialog Box

You can use the PrintDlg() API function to display the Print dialog box.  This process is exactly what the TPrintDialog component wraps.  All that's required is the initialization of a PRINTDLG structure and a single call to PrintDlg() function.  You can even set up a hook procedure to manipulate or customize the controls on the dialog box.

KEYWORDS: PrintDlg(), PRINTDLG. 
 



 

//---------------------------------------------------------------------------

UINT APIENTRY PrintHookProc(HWND hdlg, UINT uiMsg, WPARAM wParam,
                            LPARAM lParam);

//---------------------------------------------------------------------------

void __fastcall TForm1::PrintDialogButtonClick(TObject *Sender)
{
    PRINTDLG pd;
    ZeroMemory(&pd, sizeof(PRINTDLG));

    // initialize PRINTDLG
    pd.lStructSize = sizeof(PRINTDLG);
    pd.hwndOwner = Handle;
    pd.nCopies = 1;
    pd.nFromPage = 0xFFFF;
    pd.nToPage = 0xFFFF;
    pd.nMinPage = 1;
    pd.nMaxPage = 0xFFFF;
    pd.Flags = PD_ENABLEPRINTHOOK;
    pd.lpfnPrintHook = PrintHookProc;

    if (PrintDlg(&pd))
    {
        // do some printing...
        
        if (pd.hDevMode) GlobalFree(pd.hDevMode);
        if (pd.hDevNames) GlobalFree(pd.hDevNames);
    }
}
//---------------------------------------------------------------------------

UINT APIENTRY PrintHookProc(HWND hdlg, UINT uiMsg, WPARAM wParam,
    LPARAM lParam)
{
    if (uiMsg == WM_INITDIALOG)
    {
        // manipulate some controls in the hook procedure
        // for example, disable the "Properties" button
        HWND HPropButton = GetDlgItem(hdlg, psh2);
        EnableWindow(HPropButton, false);
    }
    return 0L;
}