Windows Print Dialog - xharbour

This is a discussion on Windows Print Dialog - xharbour ; Hi, This code is about a windows print dialog. Works fine in a little program ( hereinbelow ), but in an app works only once and causes a GPF. // // Testing a windows print dialog // ********************** FUNCTION MAIN( ...

+ Reply to Thread
Results 1 to 2 of 2

Windows Print Dialog

  1. Default Windows Print Dialog

    Hi,

    This code is about a windows print dialog. Works fine in a little
    program ( hereinbelow ), but in an app works only once and causes a
    GPF.

    //
    // Testing a windows print dialog
    //

    **********************
    FUNCTION MAIN( cFile )
    **********************
    local nBt1

    nBt1 := wvw_pbcreate( , 18, 35, 18, 46, " Print " ,, {||
    PrintFile( cFile ) } )

    @ 10, 10 say "Click OK Button to print or hit ESC to end."
    inkey(0)
    RETURN NIL

    ***************************
    FUNCTION PRINTFILE( cFile )
    ***************************
    local cPrinter, cText, nLines, cLine, nA
    private oPrinter := win32prn()

    //
    // Calls a C function
    //
    cPrinter := PrinterSetup()

    if empty( cPrinter)
    Alert("Cancel print")
    else
    Alert("selected printer = " + cPrinter)
    oPrinter:New( cPrinter )
    oPrinter:Landscape := .F.
    oPrinter:FormType := 9 // A4
    oPrinter:Copies := 1
    oPrinter:SetPrintQuality(-1) // DRAFT

    if !oPrinter:Create()
    Alert("Error win32prn:create()")
    return NIL
    endif
    if !oPrinter:StartDoc("Printing xHarbour")
    Alert("Error win32prn:startdoc()")
    return NIL
    endif
    oPrinter:SetDefaultFont()
    oPrinter:setfont(,,17,,,,255)
    oPrinter:NewLine()
    oPrinter:TextOut(space(80),.T.)
    cText := memoread( cFile )
    nLines := mlcount( cText, 80 )
    for nA := 1 to nLines
    cLine := memoline( cText, 80, nA, 1, .F. )
    if left( alltrim( cLine ), 1 ) == chr(12)
    oPrinter:NewPage()
    else
    oPrinter:TextOut( cLine, .T. )
    endif
    next
    oPrinter:EndDoc()
    oPrinterestroy()
    Alert("Print OK")
    endif
    RETURN NIL

    #pragma BEGINDUMP

    #define _WIN32_WINNT 0x0400

    #include "windows.h"
    #include "shlobj.h"
    #include "math.h"
    #include "hbapi.h"
    #include "hbvm.h"
    #include "hbstack.h"
    #include "hbapiitm.h"
    #include "hbapigt.h"

    HB_FUNC ( PRINTERSETUP )
    {
    PRINTDLG pd;
    LPDEVNAMES lpDevNames;

    pd.lStructSize = sizeof( PRINTDLG );
    pd.hwndOwner = GetActiveWindow();
    pd.Flags = PD_PRINTSETUP | PD_USEDEVMODECOPIES |
    PD_RETURNDC;
    pd.hDevNames = NULL;

    if ( PrintDlg( &pd ) )
    {
    lpDevNames = (LPDEVNAMES) GlobalLock( pd.hDevNames );
    if( lpDevNames )
    {
    hb_retc( ( LPSTR ) lpDevNames + lpDevNames-
    >wDeviceOffset );

    GlobalUnlock( pd.hDevNames );
    }
    else
    hb_retc( "" );
    }
    else
    hb_retc( "" );
    }

    #pragma ENDDUMP

    Best Regards,

    M.Angeiras

  2. Default Re: Windows Print Dialog

    Hi, Miguel

    You should only cleaned up properly.
    And set the Windows version properly, I think.
    If you are usint in console mode, you must set the window handle with
    a different way.

    I tested only under Windows Vista, ok?
    It is working perfectly here from within my App.



    #pragma BEGINDUMP
    #define WINVER 0x0600 // for Vista
    #define _WIN32_WINNT 0x0600 // for Vista

    #include "windows.h"
    #include "hbapi.h"

    HB_FUNC( GETCONSOLEWINDOWHANDLE )
    {
    HWND hwnd;
    AllocConsole();
    hwnd = FindWindowA("ConsoleWindowClass",NULL);
    hb_retnl( (LONG) hwnd );
    }

    HB_FUNC( PRINTERSETUP )
    {
    PRINTDLG pd;
    LPDEVNAMES lpDevNames;
    HWND hwnd;
    LPSTR lpPrinterName;


    // setting the handle
    hwnd =(HWND) hb_parnl(1);

    // 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 = 0xFFFF;
    pd.nToPage = 0xFFFF;
    pd.nMinPage = 1;
    pd.nMaxPage = 0xFFFF;

    if (PrintDlg(&pd)==TRUE)
    {
    lpDevNames = (LPDEVNAMES) GlobalLock( pd.hDevNames );
    if( lpDevNames )
    {
    GlobalUnlock( pd.hDevNames );
    lpPrinterName = ( LPSTR ) lpDevNames + lpDevNames->wDeviceOffset;
    hb_retc((LPSTR)lpPrinterName);
    // Delete DC when done.
    DeleteDC(pd.hDC);
    // Cleanup properly.
    if (pd.hDevMode)
    GlobalFree( pd.hDevMode );
    if (pd.hDevNames)
    GlobalFree( pd.hDevNames );
    }
    else
    hb_retc( "" );
    }
    else
    hb_retc( "" );
    }
    #pragma ENDDUMP


    Reinaldo Henrique


    angeiras escreveu:

    > Hi,
    >
    > This code is about a windows print dialog. Works fine in a little
    > program ( hereinbelow ), but in an app works only once and causes a
    > GPF.
    >
    > //
    > // Testing a windows print dialog
    > //
    >
    > **********************
    > FUNCTION MAIN( cFile )
    > **********************
    > local nBt1
    >
    > nBt1 := wvw_pbcreate( , 18, 35, 18, 46, " Print " ,, {||
    > PrintFile( cFile ) } )
    >
    > @ 10, 10 say "Click OK Button to print or hit ESC to end."
    > inkey(0)
    > RETURN NIL
    >
    > ***************************
    > FUNCTION PRINTFILE( cFile )
    > ***************************
    > local cPrinter, cText, nLines, cLine, nA
    > private oPrinter := win32prn()
    >
    > //
    > // Calls a C function
    > //
    > cPrinter := PrinterSetup()
    >
    > if empty( cPrinter)
    > Alert("Cancel print")
    > else
    > Alert("selected printer = " + cPrinter)
    > oPrinter:New( cPrinter )
    > oPrinter:Landscape := .F.
    > oPrinter:FormType := 9 // A4
    > oPrinter:Copies := 1
    > oPrinter:SetPrintQuality(-1) // DRAFT
    >
    > if !oPrinter:Create()
    > Alert("Error win32prn:create()")
    > return NIL
    > endif
    > if !oPrinter:StartDoc("Printing xHarbour")
    > Alert("Error win32prn:startdoc()")
    > return NIL
    > endif
    > oPrinter:SetDefaultFont()
    > oPrinter:setfont(,,17,,,,255)
    > oPrinter:NewLine()
    > oPrinter:TextOut(space(80),.T.)
    > cText := memoread( cFile )
    > nLines := mlcount( cText, 80 )
    > for nA := 1 to nLines
    > cLine := memoline( cText, 80, nA, 1, .F. )
    > if left( alltrim( cLine ), 1 ) == chr(12)
    > oPrinter:NewPage()
    > else
    > oPrinter:TextOut( cLine, .T. )
    > endif
    > next
    > oPrinter:EndDoc()
    > oPrinterestroy()
    > Alert("Print OK")
    > endif
    > RETURN NIL
    >
    > #pragma BEGINDUMP
    >
    > #define _WIN32_WINNT 0x0400
    >
    > #include "windows.h"
    > #include "shlobj.h"
    > #include "math.h"
    > #include "hbapi.h"
    > #include "hbvm.h"
    > #include "hbstack.h"
    > #include "hbapiitm.h"
    > #include "hbapigt.h"
    >
    > HB_FUNC ( PRINTERSETUP )
    > {
    > PRINTDLG pd;
    > LPDEVNAMES lpDevNames;
    >
    > pd.lStructSize = sizeof( PRINTDLG );
    > pd.hwndOwner = GetActiveWindow();
    > pd.Flags = PD_PRINTSETUP | PD_USEDEVMODECOPIES |
    > PD_RETURNDC;
    > pd.hDevNames = NULL;
    >
    > if ( PrintDlg( &pd ) )
    > {
    > lpDevNames = (LPDEVNAMES) GlobalLock( pd.hDevNames );
    > if( lpDevNames )
    > {
    > hb_retc( ( LPSTR ) lpDevNames + lpDevNames-
    > >wDeviceOffset );

    > GlobalUnlock( pd.hDevNames );
    > }
    > else
    > hb_retc( "" );
    > }
    > else
    > hb_retc( "" );
    > }
    >
    > #pragma ENDDUMP
    >
    > Best Regards,
    >
    > M.Angeiras


+ Reply to Thread