xHarbour Reference Documentation > Class Reference (textmode) xHarbour Developers Network  

Win32Prn()

Creates a new Win32Prn object.

Syntax

Win32Prn():new( [<cPrinterName>] ) --> oWin32Prn

Arguments

<cPrinterName>
This is a character string holding the name of the printer to use for printing. It defaults to the return value of GetDefaultPrinter().

Return

The function returns a new Win32Prn object and method :new() initializes the object.

Description

Objects of class Win32Prn provide the means for printing using the Windows Graphical Device Interface (GDI). For this reason, an application using Win32Prn objects must be created as GUI application. Text mode applications, or console applications, cannot use the Win32Prn class.

After a Win32Prn object is initialized with the :new() method, it must request system recources with its :create() method. Before this method is called, a print job can be configured with instance variables of a Win32Prn object. They define the paper format, print orientation or number of copies to print. A Win32Prn object can only be used for printing when its :create() method is called.

A print job is started with the method :startDoc() after which various print output methods can be called. They define the actual output that appears on paper. :startDoc() defines the document name to print for the Windows GDI printer spooler and initiates a print job. The print job must be finished with method :endDoc(). Print output is sent to the printer after :endDoc() is called.

When a print job is finished, a Win32Prn object must release GDI system resources with its :destroy() method. As a result, the following programming pattern must be applied for successful GDI printing:

   oPrinter := Win32Prn():new()      // creates the object
   oPrinter:create()                 // requests system resources
   oPrinter:startDoc()               // begins a document
   oPrinter:textOut( "Hello World" ) // print output methods
   oPrinter:endDoc()                 // ends document definition and prints
   oPrinter:destroy()                // releases system resources

Coordinate system for GDI printing

The coordinate system has its origin in the upper left corner of a page. X and Y coordinates are measured in pixels. The X coordinate increases from left to right (horizontal coordinate), and the Y coordinate increases from the top to the bottom of a page (vertical coordinate).

To support the DOS Clipper dot-matrix printing style, the Win32Prn class has methods allowing for a row/column oriented positioning of print output. These methods emulate functions of the same name, like :pRow(), :pCol() or :setPrc().

Fonts

The default font is a DOS Clipper compatible fixed font (Courier New). A list of all available fonts can be obtained with method :getFonts(). Text is printed using the currently selected font, which is defined with method :setFont().

Printing text

Text is output at the current print position with method :textOut(), or at a particular X/Y coordinate with method :textOutAt(). The text color can be selected with method :setColor().

Printing lines

A variety of methods are available for drawing basic graphical elements, such as :arc(), :box() or :line(). The color and line style for these graphical elements is defined with method :setPen(). This method creates a pen capable of drawing lines.

Colors

Colors for GDI printing must be provided as numeric RGB color values. The console mode SetColor() strings cannot be used as color definition. RGB color values are calculated from three numeric values between 0 and 255. They define the intensity for Red, Green and Blue. An RGB color value can be calulated as follows:

   FUNCTION RGB( nRed, nGreen, nBlue )
   RETURN ( nRed + ( nGreen * 256 ) + ( nBlue * 65536 ) )

Color and text settings

:bitmapsOk
Indicates if bitmap images can be printed.
:bkColor
Numeric background color value.
:numColors
Number of colors supported by the GDI device (printer).
:penColor
Numeric color value of current pen.
:penStyle
Numeric style of current pen.
:penWidth
Numeric width of current pen.
:posX
Current position of print head in X direction (horizontal).
:posY
Current position of print head in Y direction (vertical).
:textAlign
Numeric value for text alignment.
:textColor
Numeric value for text color (foreground).

Font settings

:charHeight
Numeric height of characters for the current font.
:charWidth
Numeric width of characters for the current font.
:fontName
Character string describing the currently selected font.
:fontPointSize
Numeric point size of the current font.
:fontWidth
Array describing the width of the current font.
:lineHeight
Numeric line height of current font.
:pixelsPerInchX
Number of pixels per inch in X direction (horizontal)
:pixelsPerInchY
Number of pixels per inch in Y direction (vertical)
:setFontOk
Indicates success of setting a font.

Page metrics

:bottomMargin
Numeric bottom margin of page.
:leftMargin
Numeric left margin of a page.
:pageHeight
Numeric page dimension in Y direction (vertical or height).
:pageWidth
Numeric page dimension in X direction (horizontal or width).
:rightMargin
Numeric right margin of a page.
:topMargin
Numeric top margin of a page.

Print job configuration

:binNumber
Numeric paper bin to use for printing.
:copies
Number of copies to print.
:formType
Numeric paper format to print on.
:landscape
Logical print orientation.

Printer configuration

:havePrinted
Logical flag indicating if text or bitmap was printed.
:hPrinterDc
Numeric handle to the printer device context.
:printerName
Character string describing the currently selected printer.
:printing
Logical flag indicating if a print job is in progress.

Clipper DOS compatibility

:inch_To_PosX( <nInches> ) --> nPosX
Calculates a horizontal X position from inches.
:inch_To_PosY( <nInches> ) --> nPosY
Calculates a vertical Y position from inches.
:mm_To_PosX( <nMilliMeters> ) --> nPosX
Calculates a horizontal X position from millimeters.
:mm_To_PosY( <nMilliMeters> ) --> nPosY
Calculates a vertical Y position from millimeters.
:maxCol() --> nMaxCol
Number of fixed font characters that fit into one line
:maxRow() --> nMaxRow
Number of text lines fit on one page.
:pCol() --> nPrinterColumn
Emulates the PCol() function.
:pRow() --> nPrinterRow
Emulates the PRow() function.
:setPrc( <nRow>, <nCol> ) --> NIL
Changes the current print position using row/column coordinates.

Document methods

:endDoc( [<lAbortDoc>] ) --> lSuccess
Terminates the current print job.
:endPage( [<lNewPage>] ) --> lSuccess
Marks the end of a page.
:newLine() --> nPosY
Moves the current print position to the beginning of the next line.
:newPage() --> lSuccess
Starts a new page.
:startDoc( [<cDocumentName>] ) --> lSuccess
Instructs the spooler to begin with a new document.
:startPage() --> lSuccess
Starts a new page and reconfigures page settings.

Font and text metrics

:getCharHeight() --> nPixels
Returns the average character height of the current font in pixels.
:getCharWidth() --> nPixels
Returns the average character width of the current font in pixels.
:getTextHeight( <cString> ) --> nPixelsHeight
Returns the height of a character string in pixels.
:getTextWidth( <cString> ) --> nPixelsWidth
Returns the width of a character string in pixels.

Font methods

:bold( [<nNewFontWeight>] ) --> nOldFontWeight
Queries or changes the font weight for bold printing.
:charSet( [<nNewCharSet>] ) --> nOldCharSet
Queries or changes the character set for text printing.
:getFonts() --> aFontInfo
Queries information about available fonts.
:italic( [<lNewSetting>] ) --> lOldSetting
Queries or sets italic printing.
:underLine( [<lNewSetting>] ) --> lOldSetting
Queries or sets underlined printing.
:setDefaultFont() --> lSuccess
Sets the default font.
:setFont( ... ) --> lSuccess
Selects the current font.

Printer methods

:getDeviceCaps( <nCaps> ) --> nDeviceCaps
Queries device capabilities.
:setDuplexType( [<nNewDuplex>] ) --> nOldDuplex
Queries or changes the duplex printing mode.
:setPrintQuality( [<nNewPrintQuality>] ) --> nOldPrintQuality
Queries or changes the printing quality.

Print output methods

:arc( <nX1>, <nY1>, <nX2>, <nY2> ) --> lSuccess
Prints an arc.
:box( ... ) --> lSuccess
Prints a box.
:drawBitmap( <oWin32Bmp> ) --> lSuccess
Prints a bitmap image.
:ellipse( <nX1>, <nY1>, <nX2>, <nY2> ) --> lSuccess
Prints an ellipse.
:fillRect( ... ) --> NIL
Prints a filled rectangle.
:line( <nX1>, <nY1>, <nX2>, <nY2> ) --> lSuccess
Prints a line
:setBkMode( <nMode> ) --> lSuccess
Selects the background color mix mode.
:setColor( <nClrText>, [<nClrPane>], [<nAlign>} ) --> NIL
Defines text color and alignment.
:setPen( <nStyle>, <nWidth>, <nColor> ) --> nPenHandle
Defines the pen for drawing lines.
:setPos( [<nXPos>], [<nYPos>] ) --> aOldPos
Queries or changes the current print position.
:textAtFont( ... ) --> lSuccess
Prints text at the specified X/Y position and restores previous font settings.
:textOut( ... ) --> lSuccess
Prints text at the current print position.
:textOutAt( ... ) --> lSuccess
Prints text at the specified X/Y position.

System resources

:create() --> lSuccess
Requests system resources for a GDI printer.
:destroy() --> lSuccess
Releases system resources of a GDI printer.

Info

See also:SET PRINTER, Win32Bmp()
Category: Object functions , Printer functions , xHarbour extensions
Header:wingdi.ch
Source:rtl\win32prn.prg
LIB:xhb.lib
DLL:xhbdll.dll

Example

// The example demonstrates basic print output using different
// fonts, colors and graphical elements.
// Note: create the EXE using the -gui switch

   #include "WinGdi.ch"

   #define  RGB_BLACK       RGB(   0,  0,  0 )
   #define  RGB_RED         RGB( 255,  0,  0 )
   #define  RGB_GREEN       RGB(   0,255,  0 )
   #define  RGB_BLUE        RGB(   0,  0,255 )
   #define  RGB_CYAN        RGB(   0,255,255 )
   #define  RGB_YELLOW      RGB( 255,255,  0 )
   #define  RGB_MAGENTA     RGB( 255,  0,255 )
   #define  RGB_WHITE       RGB( 255,255,255 )


   PROCEDURE Main
      LOCAL cPrinter := "EPSON Stylus DX5000 Series"
      LOCAL oPrinter
      LOCAL aFonts, cFont, nFont

      // Create printer object and configure print job
      oPrinter           := Win32Prn():new( cPrinter )
      oPrinter:landscape := .F.
      oPrinter:formType  := DMPAPER_A4
      oPrinter:copies    := 1

      // Create device context
      IF .NOT. oPrinter:create()
         Alert( "Cannot create device context" )
         QUIT
      ENDIF

      // Create print job
      IF .NOT. oPrinter:startDoc( "xHarbour test page" )
         Alert( "Cannot create document" )
         QUIT
      ENDIF

      // Text in fixed font
      oPrinter:textOut( "Text in default font" )
      oPrinter:bold( FW_EXTRABOLD )
      oPrinter:textOut( oPrinter:fontName )
      oPrinter:bold( FW_NORMAL )
      oPrinter:newLine()

      aFonts := oPrinter:getFonts()
      nFont  := AScan( aFonts, ;
                       {|a| "ARIAL" $ Upper(a[1]) } )

      cFont  := aFonts[nFont,1]

      // Text in proportional font
      oPrinter:setFont( cFont )
      oPrinter:textOut( "Text in Arial font" )
      oPrinter:bold( FW_EXTRABOLD )
      oPrinter:textOut( oPrinter:fontName )
      oPrinter:bold( FW_NORMAL )
      oPrinter:newLine()

      // Colored text
      oPrinter:setColor( RGB_YELLOW, RGB_BLUE )
      oPrinter:textOut( "Yellow on Blue" )
      oPrinter:newLine()

      // Draw colored line across page
      oPrinter:setPen( PS_DASH, 5, RGB_GREEN )
      oPrinter:line( oPrinter:posX, ;
                     oPrinter:posY, ;
                     oPrinter:rightMargin, ;
                     oPrinter:posY  )

      // Send output to printer
      oPrinter:endDoc()

      // Release GDI device context
      oPrinter:destroy()
   RETURN


   FUNCTION RGB( nRed, nGreen, nBlue )
   RETURN ( nRed + ( nGreen * 256 ) + ( nBlue * 65536 ) )

Copyright © 2006-2007 xHarbour.com Inc. All rights reserved.
http://www.xHarbour.com
Created by docmaker.exe