Jump to content
Sign in to follow this  
YellowLab

Printing my GUI

Recommended Posts

YellowLab

I have a GUI that I would like to print. I have hacked up the code in ptrex's post here:

http://www.autoitscript.com/forum/index.php?showtopic=78757

(which in turn was a mash-up of some other posts) and got a function that prints directly to the default printer. However, I need to ensure the orientation of the paper is landscape. Is this possible?

Here is the test code (Needs : PrintWinAPI.au3)

CODE

#include <WindowsConstants.au3>

#include <GDIPlus.au3>

#Include "PrintWinAPI.au3"

$sTitle="Test"

$sVersion=" 0.1"

$test=GUICreate($sTitle&$sVersion,200,200,200,200)

$button=GUICtrlCreateButton("Print",60,90,80,20)

GUISetState(@SW_SHOW,$test)

Do

$nMsg=GUIGetMsg()

If $nMsg=-3 Then Exit

Until $nMsg=$button

_Print()

;http://www.autoitscript.com/forum/index.php?showtopic=78757

Func _Print()

Local $WM_PRINT = 0x317

Local $PRF_CHILDREN = 0x10; Draw all visible child windows.

Local $PRF_CLIENT = 0x4 ; Draw the window's client area.

Local $PRF_OWNED = 0x20 ; Draw all owned windows.

Local $PRF_NONCLIENT = 0x2 ; Draw the window's Title area.

Local $PRF_ERASEBKGND = 0x8 ; Erases the background before drawing the window

Local $hWnd = WinGetHandle($sTitle&$sVersion)

Local $pos = WinGetPos($sTitle&$sVersion)

_GDIPlus_StartUp()

Local $Width = $pos[2]

Local $Height = $pos[3]

Local $hDC = _WinAPI_GetDC($hWnd)

Local $memDC = _WinAPI_CreateCompatibleDC($hDC)

Local $memBmp = _WinAPI_CreateCompatibleBitmap($hDC, $Width, $Height)

_WinAPI_SelectObject ($memDC, $memBmp)

Local $Ret = _SendMessage($hWnd, $WM_PAINT, $memDC, 0)

$Ret = _SendMessage($hWnd, $WM_PRINT, $memDC, BitOR($PRF_CHILDREN , $PRF_CLIENT, $PRF_OWNED, $PRF_NONCLIENT, $PRF_ERASEBKGND))

Local $hImage = _GDIPlus_BitmapCreateFromHBITMAP ($memBmp)

;Get Default Printer

Local $s_PrinterName = _GetDefaultPrinter()

; Create a printer device context

Local $hPrintDc = _WinAPI_CreateDC("winspool", $s_PrinterName)

Local $hGraphic = _GDIPlus_GraphicsCreateFromHDC ($hPrintDc)

; get pixel and twips info

Local $PixelsPerInchY = _WinAPI_GetDeviceCaps($hPrintDc, $__WINAPCONSTANT_LOGPIXELSY); Get Pixels Per Inch Y

Local $TwipsPerPixelY = 1440 / $PixelsPerInchY

Local $PixelsPerInchX = _WinAPI_GetDeviceCaps($hPrintDc, $__WINAPCONSTANT_LOGPIXELSX); Get Pixels Per Inch X

Local $TwipsPerPixelX = 1440 / $PixelsPerInchX

; get page width and height

Local $PageWidth = _WinAPI_GetDeviceCaps($hPrintDc, $HORZRES); Get width, in millimeters, of the physical screen

Local $PageHeight = _WinAPI_GetDeviceCaps($hPrintDc, $VERTRES); Get height, in millimeters, of the physical screen.

; set docinfo

Local $s_DocName = "Printing from AutoIt with WinAPI"

Local $DocName = DllStructCreate("char DocName[" & StringLen($s_DocName & chr(0)) & "]")

DllStructSetData($DocName, "DocName", $s_DocName & chr(0)); Size of DOCINFO structure

Local $DOCINFO = DllStructCreate($tagDOCINFO); Structure for Print Document info

DllStructSetData($DOCINFO, "Size", 20); Size of DOCINFO structure

DllStructSetData($DOCINFO, "DocName", DllStructGetPtr($DocName)); Set name of print job (Optional)

; start new print doc

Local $result = _WinAPI_StartDoc($hPrintDc, $DOCINFO)

; start new page

$result = _WinAPI_StartPage($hPrintDc)

; Draw one image in another

_GDIPLus_GraphicsDrawImageRect($hGraphic, $hImage, 50, 100, $Width, $Height)

; Draw a frame around the inserted image

_GDIPlus_GraphicsDrawRect ($hGraphic, 50, 100, $Width, $Height)

; ------------------------ End of Story -----------------------

; End the page

$result = _WinAPI_EndPage($hPrintDc)

; End the print job

$result = _WinAPI_EndDoc($hPrintDc)

; Delete the printer device context

_WinAPI_DeleteDC($hPrintDc)

; End Rest of Resources

_GDIPlus_GraphicsDispose ($hGraphic)

_GDIPlus_ImageDispose ($hImage)

_WinAPI_ReleaseDC($hWnd, $hDC)

_WinAPI_DeleteDC($memDC)

_WinAPI_DeleteObject ($memBmp)

_GDIPlus_ShutDown()

EndFunc

;------------------------------ Get Default printer --------------------------------

Func _GetDefaultPrinter()

Local $szDefPrinterName

Local $Size

Local $namesize = DllStructCreate("dword")

DllCall("winspool.drv", "int", "GetDefaultPrinter", "str", '', "ptr", DllStructGetPtr($namesize))

Local $pname = DllStructCreate("char[" & DllStructGetData($namesize, 1) & "]")

DllCall("winspool.drv", "int", "GetDefaultPrinter", "ptr", DllStructGetPtr($pname), "ptr", DllStructGetPtr($namesize))

Return DllStructGetData($pname, 1);msgbox(0,dllstructgetdata($namesize,1),DllStructGetData($pname,1))

EndFunc;==>GetDefaultPrinter1

#EndRegion

Thanks,

Bob


You can't see a rainbow without first experiencing the rain.

Share this post


Link to post
Share on other sites
martin

I have a GUI that I would like to print. I have hacked up the code in ptrex's post here:

http://www.autoitscript.com/forum/index.php?showtopic=78757

(which in turn was a mash-up of some other posts) and got a function that prints directly to the default printer. However, I need to ensure the orientation of the paper is landscape. Is this possible?

Here is the test code (Needs : PrintWinAPI.au3)

CODE

#include <WindowsConstants.au3>

#include <GDIPlus.au3>

#Include "PrintWinAPI.au3"

$sTitle="Test"

$sVersion=" 0.1"

$test=GUICreate($sTitle&$sVersion,200,200,200,200)

$button=GUICtrlCreateButton("Print",60,90,80,20)

GUISetState(@SW_SHOW,$test)

Do

$nMsg=GUIGetMsg()

If $nMsg=-3 Then Exit

Until $nMsg=$button

_Print()

;http://www.autoitscript.com/forum/index.php?showtopic=78757

Func _Print()

Local $WM_PRINT = 0x317

Local $PRF_CHILDREN = 0x10; Draw all visible child windows.

Local $PRF_CLIENT = 0x4 ; Draw the window's client area.

Local $PRF_OWNED = 0x20 ; Draw all owned windows.

Local $PRF_NONCLIENT = 0x2 ; Draw the window's Title area.

Local $PRF_ERASEBKGND = 0x8 ; Erases the background before drawing the window

Local $hWnd = WinGetHandle($sTitle&$sVersion)

Local $pos = WinGetPos($sTitle&$sVersion)

_GDIPlus_StartUp()

Local $Width = $pos[2]

Local $Height = $pos[3]

Local $hDC = _WinAPI_GetDC($hWnd)

Local $memDC = _WinAPI_CreateCompatibleDC($hDC)

Local $memBmp = _WinAPI_CreateCompatibleBitmap($hDC, $Width, $Height)

_WinAPI_SelectObject ($memDC, $memBmp)

Local $Ret = _SendMessage($hWnd, $WM_PAINT, $memDC, 0)

$Ret = _SendMessage($hWnd, $WM_PRINT, $memDC, BitOR($PRF_CHILDREN , $PRF_CLIENT, $PRF_OWNED, $PRF_NONCLIENT, $PRF_ERASEBKGND))

Local $hImage = _GDIPlus_BitmapCreateFromHBITMAP ($memBmp)

;Get Default Printer

Local $s_PrinterName = _GetDefaultPrinter()

; Create a printer device context

Local $hPrintDc = _WinAPI_CreateDC("winspool", $s_PrinterName)

Local $hGraphic = _GDIPlus_GraphicsCreateFromHDC ($hPrintDc)

; get pixel and twips info

Local $PixelsPerInchY = _WinAPI_GetDeviceCaps($hPrintDc, $__WINAPCONSTANT_LOGPIXELSY); Get Pixels Per Inch Y

Local $TwipsPerPixelY = 1440 / $PixelsPerInchY

Local $PixelsPerInchX = _WinAPI_GetDeviceCaps($hPrintDc, $__WINAPCONSTANT_LOGPIXELSX); Get Pixels Per Inch X

Local $TwipsPerPixelX = 1440 / $PixelsPerInchX

; get page width and height

Local $PageWidth = _WinAPI_GetDeviceCaps($hPrintDc, $HORZRES); Get width, in millimeters, of the physical screen

Local $PageHeight = _WinAPI_GetDeviceCaps($hPrintDc, $VERTRES); Get height, in millimeters, of the physical screen.

; set docinfo

Local $s_DocName = "Printing from AutoIt with WinAPI"

Local $DocName = DllStructCreate("char DocName[" & StringLen($s_DocName & chr(0)) & "]")

DllStructSetData($DocName, "DocName", $s_DocName & chr(0)); Size of DOCINFO structure

Local $DOCINFO = DllStructCreate($tagDOCINFO); Structure for Print Document info

DllStructSetData($DOCINFO, "Size", 20); Size of DOCINFO structure

DllStructSetData($DOCINFO, "DocName", DllStructGetPtr($DocName)); Set name of print job (Optional)

; start new print doc

Local $result = _WinAPI_StartDoc($hPrintDc, $DOCINFO)

; start new page

$result = _WinAPI_StartPage($hPrintDc)

; Draw one image in another

_GDIPLus_GraphicsDrawImageRect($hGraphic, $hImage, 50, 100, $Width, $Height)

; Draw a frame around the inserted image

_GDIPlus_GraphicsDrawRect ($hGraphic, 50, 100, $Width, $Height)

; ------------------------ End of Story -----------------------

; End the page

$result = _WinAPI_EndPage($hPrintDc)

; End the print job

$result = _WinAPI_EndDoc($hPrintDc)

; Delete the printer device context

_WinAPI_DeleteDC($hPrintDc)

; End Rest of Resources

_GDIPlus_GraphicsDispose ($hGraphic)

_GDIPlus_ImageDispose ($hImage)

_WinAPI_ReleaseDC($hWnd, $hDC)

_WinAPI_DeleteDC($memDC)

_WinAPI_DeleteObject ($memBmp)

_GDIPlus_ShutDown()

EndFunc

;------------------------------ Get Default printer --------------------------------

Func _GetDefaultPrinter()

Local $szDefPrinterName

Local $Size

Local $namesize = DllStructCreate("dword")

DllCall("winspool.drv", "int", "GetDefaultPrinter", "str", '', "ptr", DllStructGetPtr($namesize))

Local $pname = DllStructCreate("char[" & DllStructGetData($namesize, 1) & "]")

DllCall("winspool.drv", "int", "GetDefaultPrinter", "ptr", DllStructGetPtr($pname), "ptr", DllStructGetPtr($namesize))

Return DllStructGetData($pname, 1);msgbox(0,dllstructgetdata($namesize,1),DllStructGetData($pname,1))

EndFunc;==>GetDefaultPrinter1

#EndRegion

Thanks,

Bob

AFAIK no one has written anything to set landscape or portrait in AutoIt pure yet. I did start a function but somehow never went back to it to finish it. My print UDF allows you to set the orientation with a single command but it uses my dll.

Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.

Share this post


Link to post
Share on other sites
YellowLab

@martin

Thanks. I used your UDF and got the print to work by taking/saving a screenshot. Is it possible to pass the handle of the screenshot image in memory to the _PrintImage command rather than saving it to disk as is the case in my original code?

Thanks,

Bob


You can't see a rainbow without first experiencing the rain.

Share this post


Link to post
Share on other sites
martin

@martin

Thanks. I used your UDF and got the print to work by taking/saving a screenshot. Is it possible to pass the handle of the screenshot image in memory to the _PrintImage command rather than saving it to disk as is the case in my original code?

Thanks,

Bob

Yes it's possible. I'll have a look in the next 2 or 3 days but I'll restrict it to bitmap images. I imagine I'll do something to allow printing from an area of the bitmap to a position and size on the paper.

_PrintImageFomDC($hDC,$topX, $TopY,$Width, $Height,$PrintTopX,$PrintTopY,$PrintWidth,$PrintHeight)


Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.

Share this post


Link to post
Share on other sites
YellowLab

Yes it's possible. I'll have a look in the next 2 or 3 days but I'll restrict it to bitmap images. I imagine I'll do something to allow printing from an area of the bitmap to a position and size on the paper.

_PrintImageFomDC($hDC,$topX, $TopY,$Width, $Height,$PrintTopX,$PrintTopY,$PrintWidth,$PrintHeight)

Thank you martin for your updates to the dll and udf. This solution works great!

http://www.autoitscript.com/forum/index.php?showtopic=51054

Bob


You can't see a rainbow without first experiencing the rain.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×