I am almost ready to release the first BETA of my FreeBASIC Windows Printing Library. It allows the creation of a PrinterObject which helps in dealing with printing to the printer. It also has some ancillary features like quick access to common dialogs like open/save/printer/pagesetup/fonts/colors.
'************************************************************************
' FBWinPrint v 0.12 - Windows Printing Library for FreeBASIC
' by Vincent DeCampo
'
' Rev 0.12 (09/19/2007)
'
' - Fixed using StretchBlt with printer DC
' - Added DrawBox, PrintWindow
' - Fixed PrintBMP Crash
'
' Rev 0.11 (09/17/2007)
'
' - Corrected scaling of font to device context
'
' Rev 0.10 (09/15/2007)
' PrintObj (Printer Object)
'
' -Properties
' .PrinterDC (the device context of the currently selected printer.)
' (This updates when the printer changes via Dialog method)
'
' .PrinterName (The Name of the currently selected printer.)
' (This updates when the printer changes via Dialog method)
'
' .PageWidth (The width of the page in device units
' (This updates when the printer changes via Dialog method)
'
' .PageHeight (The width of the page in device units
' (This updates when the printer changes via Dialog method)
'
' -Methods
' OpenFile() As String
' (Opens an OpenFile window and returns Selected Filename.)
' (Returns an empty ("") string when CANCEL is selected)
'
' SaveFile() As String
' (Opens a SaveFile window and returns Selected Filename.)
' (Returns an empty ("") string when CANCEL is selected)
'
' ColorPicker() As Integer
' (Opens a Color Picker window and returns Selected RGB value.)
' (Returns -1 when CANCEL is selected)
'
' Font(ByRef FontName As String, ByRef FontSize As Integer) As String
' (Opens a Font window and returns Selected Font Name.)
' (Returns an empty ("") string when CANCEL is selected)
'
' Dialog() As String
' (Opens a Print Dialog window and returns selected Printer.)
' (This method will update the .PrinterName And .PrinterDC properties)
' (Returns an empty ("") string when CANCEL is selected)
'
' PageSetup() As Integer
' (Opens a Page Setup window.)
' (Returns -1 when CANCEL is selected)
'
' DocStart(DOCTitle As String = "Untitled")
' (Begins a print job)
'
' PageStart()
' (Begins a new page of a print job)
'
' PageEnd()
' (Ends current page of a print job)
'
' DocEnd()
' (Sends print job to printer)
'
' DocAbort()
' (Aborts current print job)
'
' BltImageDC (srcDC As HDC, srcRECT As RECT Ptr, dstRECT As RECT Ptr)
' (Draws an image to the printer from an existing Bitmap DC)
' (dstRECT.Right/.Bottom contain destination width and height)
'
' PrintBMP (Filename As String, dstRECT As RECT)
' (Draws an image to the printer from an existing Bitmap Image File)
' (dstRECT.Right/.Bottom contain destination width and height)
'
' PPrint (X As Integer, Y As Integer, Text As String)
' (Prints Text to the printer at the specified x,y using the font
' (specified by the SetFont method.)
'
' SetFont(FontName As String, FontSize As Integer, FontColor As Integer)
' (Sets the Font, Size and Color to use with the PPrint method)
'
' DrawBox (X1 As Integer, Y1 As Integer, X2 As Integer, Y2 As Integer, LineColor As Integer)
' (Draws a colored box from X1,Y1 (upper left) to X2,Y2 (Lower Right)
' (Box is white filled and should be drawn first before adding internal text)
'
' PrintWindow (hWnd As HANDLE, X As Integer, Y As Integer, dX As Integer, dY As Integer)
' (Draws an copy of a window to the printer from an existing hWnd)
' (dX/dY contain destination width and height)
'
'************************************************************************
-Vince
Last edited by vdecampo on Sep 19, 2007 18:35, edited 3 times in total.
#Include "windows.bi"
#Include "FBWinPrint.bi"
Dim PO As PrinterObj
Dim FontName As String
Dim FontSize As Integer
With PO
Print .Dialog
Print .PrinterDC,.PrinterName,.PageWidth,.PageHeight
Print .Font(FontName, FontSize)
.DocStart
.PageStart
.SetFont (FontName, FontSize, 0)
.PPrint (10,10,"Test")
.PageEnd
.DocEnd
End With
Print "Print Job Sent To Printer."
Print "Press any key to exit."
Sleep
End
Cheers!
-Vince
Last edited by vdecampo on May 12, 2009 20:04, edited 2 times in total.
I have found an issue with using StretchBlt and the printer DC. I will find a resolution and fix the PrintBMP function. Could you post a snippet of your call to the function?
#Include "windows.bi"
#Include "FBWinPrint.bi"
Dim PO As PrinterObj
Dim FontName As String
Dim FontSize As Integer
Dim r As rect
r.top=10
r.left=10
r.bottom=400
r.right=600
With PO
Print .Dialog
Print .PrinterDC,.PrinterName,.PageWidth,.PageHeight
'Print .Font(FontName, FontSize)
.DocStart
.PageStart
.printbmp("setup.bmp",r)
'.SetFont (FontName, FontSize, 0)
'.PPrint (10,10,"Printer Error")
.PageEnd
.DocEnd
End With
Print "Print Job Sent To Printer."
Print "Press any key to exit."
Sleep
End
I fixed stretching of images and some potential crash situations plus added some other functions...
'************************************************************************
' FBWinPrint v 0.12 - Windows Printing Library for FreeBASIC
' by Vincent DeCampo
'
' Rev 0.12 (09/19/2007)
'
' - Fixed using StretchBlt with printer DC
' - Added DrawBox, PrintWindow
' - Fixed PrintBMP Crash
The new sample program uses a complete pathname to the BMP for the PrintBMP function. This seems to be necessary although I'm not sure why. If the file is in the EXE directory, it should find it, but it doesn't.
Compiler output:
C:/FreeBasic/inc/win/windef.bi(61) error 3: Expected End-of-Line, found 'ptr'
type PWINBOOL as integer ptr
^
C:/FreeBasic/inc/win/windef.bi(62) error 3: Expected End-of-Line, found 'ptr'
type LPWINBOOL as integer ptr
^
C:/FreeBasic/inc/win/windef.bi(64) error 3: Expected End-of-Line, found 'ptr'
type PBOOL as BOOL ptr
^
C:/FreeBasic/inc/win/windef.bi(65) error 3: Expected End-of-Line, found 'ptr'
type LPBOOL as BOOL ptr
^
C:/FreeBasic/inc/win/windef.bi(68) error 3: Expected End-of-Line, found 'ptr'
type PFLOAT as FLOAT ptr
^
C:/FreeBasic/inc/win/windef.bi(69) error 3: Expected End-of-Line, found 'ptr'
type PBYTE as UBYTE ptr
^
C:/FreeBasic/inc/win/windef.bi(70) error 3: Expected End-of-Line, found 'ptr'
type LPBYTE as UBYTE ptr
^
C:/FreeBasic/inc/win/windef.bi(71) error 3: Expected End-of-Line, found 'ptr'
type PINT as integer ptr
^
C:/FreeBasic/inc/win/windef.bi(72) error 3: Expected End-of-Line, found 'ptr'
type LPINT as integer ptr
^
C:/FreeBasic/inc/win/windef.bi(73) error 66: Incomplete type, before 'ptr'
type PWORD as WORD ptr
^
C:/FreeBasic/inc/win/windef.bi(73) error 123: Too many errors, exiting
Results:
Compilation failed
System:
FBIde: 0.4.6
fbc: FreeBASIC Compiler - Version 0.18.3 (12-28-2007) for win32 (target:win32)
OS: Windows XP (build 2600, Service Pack 2)