Announcement

Collapse
No announcement yet.

Print in landscape mode

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Martin Draper
    replied
    Thank you that was jsut waht I needed. I have modified my code
    to use you concept and it works just great.
    Here is the new code to help other people.

    #COMPILE EXE

    $INCLUDE "WIN32API.INC"
    #INCLUDE "comdlg32.inc"

    ' Lines per page
    %LPP = 66

    'The Following GLOBAL(s) Specify Controls for OUTPUT Patches!
    GLOBAL FontMetrix AS LONG
    GLOBAL LineCtr AS LONG
    GLOBAL PrinterOpen AS LONG
    GLOBAL hDC AS LONG 'hDC of printer
    GLOBAL szText AS ASCIIZ * 256
    GLOBAL tm AS TEXTMETRIC
    GLOBAL di AS docinfo
    GLOBAL hFont AS LONG, hFontOld AS LONG
    GLOBAL Lf AS LOGFONT
    GLOBAL dm AS DEVMODE

    'Global variable that point to the print position
    GLOBAL PositionX AS INTEGER
    GLOBAL PositionY AS INTEGER

    '
    '
    FUNCTION SetFont(LPFont AS STRING,LPSize AS INTEGER, Bold AS INTEGER, _
    Italic AS INTEGER, Underline AS INTEGER, Strikeout AS INTEGER ) AS INTEGER
    ' Create the font using device point sizes
    ' create a %PointSize font, or...
    '
    Lf.lfHeight = LPSize * GetDeviceCaps(hDC, %LOGPIXELSY) / 72

    ' create a scaled font FOR 60 lines/PAGE
    ' Lf.lfHeight = INT(GetDeviceCaps(hDC, %LOGPIXELSY) / 6)

    ' create a scaled font for %LPP lines/PAGE
    ' Lf.lfHeight = GetDeviceCaps(hDC, %VERTRES) \ %LPP
    '

    Lf.lfFaceName = LPFont
    IF Bold THEN lf.lfWeight=700 ELSE lf.lfWeight=400
    Lf.lfItalic=Italic
    lf.lfUnderline=Underline
    lf.lfStrikeOut=Strikeout

    hFont = CreateFontIndirect(LF)
    hFontOld = SelectObject(hDC, hFont)

    ' Get the font metrics in logical point sizes
    GetTextMetrics hDC, tm
    FontMetrix = tm.tmHeight + tm.tmExternalLeading
    FontMetrix = LF.lfHeight

    END FUNCTION

    FUNCTION Locate(Xcord AS INTEGER,Ycord AS INTEGER) AS INTEGER
    PositionX = Xcord
    PositionY = Ycord
    END FUNCTION

    FUNCTION WPrint(szText AS ASCIIZ) AS INTEGER
    textOut hDC, PositionX,PositionY, szText, LEN(szText)
    END FUNCTION

    FUNCTION CloseAPrinter () AS LONG
    ' clean up the GDI object to prevent a memory leak
    SelectObject hDC, hFontOld
    DeleteObject hFont
    EndPage hDC 'Ends the CURRENT PAGE ONLY!
    EndDoc hDC 'Ends the CURRENT DOCUMENT!

    ' delete the DC to prevent a memory leak
    DeleteDC hDC
    END FUNCTION

    FUNCTION PBMAIN()
    '
    ' Buffer to receive the Default Printer info
    '
    LOCAL PInfo5() AS PRINTER_INFO_5
    DIM PInfo5(0:2) AS PRINTER_INFO_5

    ' Now allocate the space to get the Default Printer data

    LOCAL dwNeeded AS LONG,dwReturned AS LONG, hPrn AS LONG, dmBuffer AS STRING
    LOCAL n AS LONG
    LOCAL DvModePtr AS DEVMODE PTR
    LOCAL pDevMode AS STRING
    '
    ' Get Default Printer
    '
    EnumPrinters %PRINTER_ENUM_DEFAULT, _ 'printer object types
    BYVAL %NULL,_ 'name of printer object
    5,_ 'information level
    BYVAL VARPTR(PInfo5(0)), _ 'printer information buffer
    SIZEOF(PInfo5(0)) * 3,_ 'size of printer information buffer
    dwNeeded&,_ 'bytes received or required
    dwReturned&
    '
    ' Open a Printer
    '
    OpenPrinter BYVAL PInfo5(0).pPrinterName, hPrn, BYVAL %NULL
    dwNeeded = DocumentProperties(%null,_ ' handle to window that displays DIALOG box
    hPrn,_ ' handle to printer object
    BYVAL PInfo5(0).pPrinterName,_ ' pointer to device name
    BYVAL %NULL,_ ' pointer to modified device mode structure
    BYVAL %NULL, _ ' pointer to original device mode structure
    0) ' mode flag
    '
    ' Create the DEVMODE structure for the printer
    '
    dmBuffer = SPACE$(dwNeeded)
    DvModePtr = STRPTR(dmBuffer)
    '
    ' assign the structure to the document
    '
    n = DocumentProperties(%null, _
    hPrn, _
    BYVAL PInfo5(0).pPrinterName,_
    BYVAL DvModePtr, _
    BYVAL %NULL, _
    %DM_OUT_BUFFER)
    '
    IF n <> %IDOK THEN
    dmBuffer = ""
    ClosePrinter hPrn
    ELSE
    '@DvModePtr.dmOrientation = %DMORIENT_LANDSCAPE
    @DvModePtr.dmOrientation = %DMORIENT_PORTRAIT
    'IF envelopeSize = 1 THEN '(4 1/8 x 9 1/2)
    ' @DvModePtr.dmPaperSize = %DMPAPER_ENV_10
    'ELSE '(3 7/8 x 7 1/2)
    ' @DvModePtr.dmPaperSize = %DMPAPER_ENV_MONARCH
    'END IF
    END IF
    '
    '
    '
    ' Create the printer context
    '
    hDC = CreateDC(BYVAL %Null, _ 'driver name it can be either the null-terminated string DISPLAY or the device name
    BYVAL PInfo5(0).pPrinterName, _ 'device name Pointer to a null-terminated character string that specifies the name of the specific output
    BYVAL %Null, _ 'This parameter is ignored for Win32-based applications
    BYVAL DvModePtr) 'optional Pointer to a DEVMODE structure

    IF ISFALSE hDC THEN EXIT FUNCTION
    '
    ' This section sets up the document info for the printing
    ' di is document info
    '
    di.cbsize = SIZEOF(di)
    szText = "Cypher Output"
    di.lpszDocName = VARPTR(szText)
    '
    ' start the process the document info to the handle for the printer
    '
    StartDoc hDC, di
    StartPage hDC
    '
    '
    SetTextAlign hDC, %TA_BASELINE OR %TA_NOUPDATECP OR %TA_LEFT
    SetBkMode hDC, %TRANSPARENT
    '
    PrinterOpen =1
    FUNCTION = 1
    '
    SetFont "Courier New",14,1,0,0,1
    locate 100,100
    WPRINT (" This is a test of the printer ")

    SetFont "Arial", 24,0,1,1,0
    locate 100,200
    wprint (" This is another test of the printer ")

    locate 100,300
    wprint ("dmSize = " + HEX$(DvModePtr))

    locate 100,400
    wprint ("dmSize = " + HEX$(@DvModePtr.dmSize))

    locate 100,500
    wprint ("dmFields = " + HEX$(@DvModePtr.dmFields))

    CLoseAPrinter

    ' PrintTest
    END FUNCTION

    ------------------

    Leave a comment:


  • Charles Dietz
    replied
    Martin, I just finished writing this function today for my program,
    and I think it should help you. The steps are as follows:

    1. call OpenPrinter to obtain handle to printer object, hPrn
    2. call DocumentProperties with last parameter = 0 to get buffer size
    3. call DocumentProperties again with %DM_OUT_BUFFER to fill buffer with
    default DEVMODE structure values
    4. make changes to DEVMODE structure values
    5. obtain printer DC with customized DEVMODE structure values

    defaultPrn is a global ASCIIZ * 64
    defaultPrn = pinfo5(0)[email protected]

    You can find more info from DocumentProperties in the win32.hlp file.

    Hope this helps...

    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

    Code:
    FUNCTION customPrnDC() AS LONG
       LOCAL dwNeeded AS LONG, hPrn AS LONG, dmBuffer AS STRING
       LOCAL n AS LONG, x AS DOUBLE, y AS DOUBLE
       LOCAL dmPtr AS DEVMODE PTR
       IF ISTRUE OpenPrinter(BYCOPY defaultPrn, hPrn, BYVAL %NULL) THEN
          dwNeeded = DocumentProperties(hEnvelope, hPrn, BYCOPY defaultPrn, BYVAL %NULL, BYVAL %NULL, 0)
          dmBuffer = SPACE$(dwNeeded): dmPtr = STRPTR(dmBuffer)
          n = DocumentProperties(hEnvelope, hPrn, BYCOPY defaultPrn, BYVAL dmPtr, BYVAL %NULL, %DM_OUT_BUFFER)
          IF n <> %IDOK THEN
             dmBuffer = "": ClosePrinter hPrn
          ELSE
             @dmPtr.dmOrientation = %DMORIENT_LANDSCAPE
             IF envelopeSize = 1 THEN '(4 1/8 x 9 1/2)
                @dmPtr.dmPaperSize = %DMPAPER_ENV_10
             ELSE '(3 7/8 x 7 1/2)
                @dmPtr.dmPaperSize = %DMPAPER_ENV_MONARCH
             END IF
          END IF
          customPrnDC = CreateDC(BYVAL %NULL, BYCOPY defaultPrn, BYVAL %NULL, BYVAL dmPtr)
       END IF
    END FUNCTION

    ------------------

    Leave a comment:


  • Martin Draper
    started a topic Print in landscape mode

    Print in landscape mode

    I am trying to write a basic program for printing in landscape mode and I think I am setting everything correctly but it still prints in portrait mode

    This is what I have so far
    #COMPILE EXE

    $INCLUDE "WIN32API.INC"
    #INCLUDE "comdlg32.inc"

    ' Lines per page
    %LPP = 66

    'The Following GLOBAL(s) Specify Controls for OUTPUT Patches!
    GLOBAL FontMetrix AS LONG
    GLOBAL LineCtr AS LONG
    GLOBAL PrinterOpen AS LONG
    GLOBAL hDC AS LONG 'hDC of printer
    GLOBAL szText AS ASCIIZ * 256
    GLOBAL tm AS TEXTMETRIC
    GLOBAL di AS docinfo
    GLOBAL hFont AS LONG, hFontOld AS LONG
    GLOBAL Lf AS LOGFONT
    GLOBAL dm AS DEVMODE

    'Global variable that point to the print position
    GLOBAL PositionX AS INTEGER
    GLOBAL PositionY AS INTEGER

    '
    '
    FUNCTION SetFont(LPFont AS STRING,LPSize AS INTEGER, Bold AS INTEGER, _
    Italic AS INTEGER, Underline AS INTEGER, Strikeout AS INTEGER ) AS INTEGER
    ' Create the font using device point sizes
    ' create a %PointSize font, or...
    '
    Lf.lfHeight = LPSize * GetDeviceCaps(hDC, %LOGPIXELSY) / 72

    ' create a scaled font FOR 60 lines/PAGE
    ' Lf.lfHeight = INT(GetDeviceCaps(hDC, %LOGPIXELSY) / 6)

    ' create a scaled font for %LPP lines/PAGE
    ' Lf.lfHeight = GetDeviceCaps(hDC, %VERTRES) \ %LPP
    '

    Lf.lfFaceName = LPFont
    IF Bold THEN lf.lfWeight=700 ELSE lf.lfWeight=400
    Lf.lfItalic=Italic
    lf.lfUnderline=Underline
    lf.lfStrikeOut=Strikeout

    hFont = CreateFontIndirect(LF)
    hFontOld = SelectObject(hDC, hFont)

    ' Get the font metrics in logical point sizes
    GetTextMetrics hDC, tm
    FontMetrix = tm.tmHeight + tm.tmExternalLeading
    FontMetrix = LF.lfHeight

    END FUNCTION

    FUNCTION Locate(Xcord AS INTEGER,Ycord AS INTEGER) AS INTEGER
    PositionX = Xcord
    PositionY = Ycord
    END FUNCTION

    FUNCTION WPrint(szText AS ASCIIZ) AS INTEGER
    textOut hDC, PositionX,PositionY, szText, LEN(szText)
    END FUNCTION

    FUNCTION CloseAPrinter () AS LONG
    ' clean up the GDI object to prevent a memory leak
    SelectObject hDC, hFontOld
    DeleteObject hFont
    EndPage hDC 'Ends the CURRENT PAGE ONLY!
    EndDoc hDC 'Ends the CURRENT DOCUMENT!

    ' delete the DC to prevent a memory leak
    DeleteDC hDC
    END FUNCTION

    FUNCTION PBMAIN()

    LOCAL Var AS WORD
    '
    ' Open a Printer
    '
    LOCAL DvModePtr AS DEVMODE PTR
    LOCAL pDevMode AS STRING
    '
    ' create DEVMODE for printer
    '
    pDevMode = SPACE$(LEN(DEVMODE)) ' allocate string space for the DEVMODE
    DvModePtr = STRPTR(pDevMode) ' setup a pointer the allocated space for DEVMODE
    '
    ' set device mode properties
    '
    @DvModePtr.dmSize = LEN(DEVMODE)
    @DvModePtr.dmFields = %DM_ORIENTATION
    @DvModePtr.dmOrientation = %DMORIENT_LANDSCAPE
    '
    ' Buffer to receive the Default Printer info
    '
    LOCAL PInfo5() AS PRINTER_INFO_5

    ' Now allocate the space to get the Default Printer data
    DIM PInfo5(0:2) AS PRINTER_INFO_5, dwNeeded&, dwReturned&

    EnumPrinters %PRINTER_ENUM_DEFAULT, _ 'printer object types
    BYVAL %NULL,_ 'name of printer object
    5,_ 'information level
    BYVAL VARPTR(PInfo5(0)), _ 'printer information buffer
    SIZEOF(PInfo5(0)) * 3,_ 'size of printer information buffer
    dwNeeded&,_ 'bytes received or required
    dwReturned& 'number of printers enumerated
    '
    ' Create the printer context
    '
    hDC = CreateDC(BYVAL %Null, _ 'driver name it can be either the null-terminated string DISPLAY or the device name
    BYVAL PInfo5(0).pPrinterName, _ 'device name Pointer to a null-terminated character string that specifies the name of the specific output
    BYVAL %Null, _ 'This parameter is ignored for Win32-based applications
    BYVAL DvModePtr) 'optional Pointer to a DEVMODE structure

    IF ISFALSE hDC THEN EXIT FUNCTION
    '
    ' This section sets up the document info for the printing
    ' di is document info
    '
    di.cbsize = SIZEOF(di)
    szText = "Cypher Output"
    di.lpszDocName = VARPTR(szText)
    '
    ' start the process the document info to the handle for the printer
    '
    StartDoc hDC, di
    StartPage hDC
    '
    '
    SetTextAlign hDC, %TA_BASELINE OR %TA_NOUPDATECP OR %TA_LEFT
    SetBkMode hDC, %TRANSPARENT
    '
    PrinterOpen =1
    FUNCTION = 1
    '
    SetFont "Courier New",14,1,0,0,1
    locate 100,100
    WPRINT (" This is a test of the printer ")

    SetFont "Arial", 24,0,1,1,0
    locate 100,200
    wprint (" This is another test of the printer ")

    locate 100,300
    wprint ("dmSize = " + HEX$(DvModePtr))

    locate 100,400
    wprint ("dmSize = " + HEX$(@DvModePtr.dmSize))

    locate 100,500
    wprint ("dmFields = " + HEX$(@DvModePtr.dmFields))

    CLoseAPrinter

    ' PrintTest
    END FUNCTION


Working...
X