Sign in to follow this  
Followers 0
Ragemonkey

Print Screen - Printer

16 posts in this topic

Hi guys,

I was asked to make a small program that takes a screen shot of a page and prints it on the default printer.

Being quite new with Autoit ive come up with a couple of lines but can't work out if you can print straight from the clipboard where the printscreen would be or if i need to dump it in msword to print it.

here is the two lines i have so far, if anyone could point me in the right direction please.

WinActivate( "Panel" [, "Quotations"] )
Send("{PRINTSCREEN}")

The first things i figured was to activate the window and then printscreen it.

cheers


AutoIT [L] PlatesI have found these posts really usefull, maybe you will too.Martins Print UDF

Share this post


Link to post
Share on other sites



The first things i figured was to activate the window and then printscreen it.

See a _ScreenCapture_CaptureWnd() function in the help.

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

Hi guys,

I was asked to make a small program that takes a screen shot of a page and prints it on the default printer.

Being quite new with Autoit ive come up with a couple of lines but can't work out if you can print straight from the clipboard where the printscreen would be or if i need to dump it in msword to print it.

here is the two lines i have so far, if anyone could point me in the right direction please.

WinActivate( "Panel" [, "Quotations"] )
Send("{PRINTSCREEN}")

The first things i figured was to activate the window and then printscreen it.

cheers

One method is in this post which has a printout on the default printer of a screen shot. There might be some scaling needed and you can use _GDIPlus_GraphicsDrawImageRectRect for that.

There is also my printing udf (see my signature) which might be easier to use because you can specify portrait or landscape, and specify the position and size of the image to be printed along with any other text or graphics and the scaling is taken care of automatically. You can use _ScreenCapture_Capture to take a screen shot of part of the screen such as a window. Then you can print that image using my udf.

EDIT: Ah yes, rasim had a better function with _ScreenCaptute_CaptureWnd than ScreenCapture_Capture, I'd forgotten that one.

Edited by martin

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

One method is in this post which has a printout on the default printer of a screen shot. There might be some scaling needed and you can use _GDIPlus_GraphicsDrawImageRectRect for that.

There is also my printing udf (see my signature) which might be easier to use because you can specify portrait or landscape, and specify the position and size of the image to be printed along with any other text or graphics and the scaling is taken care of automatically. You can use _ScreenCapture_Capture to take a screen shot of part of the screen such as a window. Then you can print that image using my udf.

EDIT: Ah yes, rasim had a better function with _ScreenCaptute_CaptureWnd than ScreenCapture_Capture, I'd forgotten that one.

Thanks for the reply guys.. I actually had a little difficulty with _ScreenCaptute_CaptureWnd so i tested the code below and this is working well. I'm going to have a look through your UDF now and see how i can print this (without saving the file)

#include <ScreenCapture.au3>

_Main()

Func _Main()
    Local $hBmp

    WinActivate( "Panel")

    $hBmp = _ScreenCapture_Capture ("")

    _ScreenCapture_SaveImage (@MyDocumentsDir & "\GDIPlus_Image.jpg", $hBmp)

EndFunc

AutoIT [L] PlatesI have found these posts really usefull, maybe you will too.Martins Print UDF

Share this post


Link to post
Share on other sites

Thanks for the reply guys.. I actually had a little difficulty with _ScreenCaptute_CaptureWnd

EndFunc

_ScreenCapture_CaptureWnd("Filname",WinGetHandle("Panel" ));where "Panel" is the title of the window to capture

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

_ScreenCapture_CaptureWnd("Filname",WinGetHandle("Panel" ));where "Panel" is the title of the window to capture
Thanks Martin. Do you have to save the file? i was having a look through the UDF and im seeing functions for opening and printing is that right?

AutoIT [L] PlatesI have found these posts really usefull, maybe you will too.Martins Print UDF

Share this post


Link to post
Share on other sites

Thanks Martin. Do you have to save the file? i was having a look through the UDF and im seeing functions for opening and printing is that right?

Yes save the file, say as screenshot.bmp, then use that filename for the printing function

_PrintImage($hp,"screenshot.bmp",$x,$y,$w,$h)


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

ok, sorry but im confused mate.. here is what i have, the script opens and closes but doesnt print

#include <ScreenCapture.au3>

Dim $vPath
$vPath = "E:\Scripts\Workshop\PrintMG.dll"
Dim $address
$address = "C:\Documents and Settings\John\Desktop\screenshot.jpg"
Dim $sErr
$sErr = ("Failed to open" & $vPath)
Dim $hDll
$hDll = DllOpen($vPath)


Func _PrintDllStart(ByRef $sErr, $vPath)
    
        If $hDll = -1 Then
        SetError(1)
        $sErr
        Return -1;failed
    EndIf

    Return $hDll;ok
    

EndFunc

WinActivate( "Panel")
_ScreenCapture_Capture($address)
    
Func _PrintImage($hDll,$address,$TopX,$TopY,$wid,$ht)
    $vDllAns = DllCall($hDll,'int','Image','str',$address,'int',$TopX,'int',$TopY,'int',$Wid,'Int',$Ht)
    If @error = 0 Then Return $vDllAns[0]
    SetError(1)
    Return -1
    
EndFunc

AutoIT [L] PlatesI have found these posts really usefull, maybe you will too.Martins Print UDF

Share this post


Link to post
Share on other sites

Martin, could you maybe provide some example code aligned with what i am asking for. sorry, im trying to do this myself but im confused!

thank you


AutoIT [L] PlatesI have found these posts really usefull, maybe you will too.Martins Print UDF

Share this post


Link to post
Share on other sites

Martin, could you maybe provide some example code aligned with what i am asking for. sorry, im trying to do this myself but im confused!

thank you

Yes, I think you are confused. :)

But don't worry, hopefully it will soon start to make sense.

First the udf you are using is the old version. Download the printmgv2_62 from here.

When you have extracted the dll and the udf save the following script in the same folder and run it

#include <ScreenCapture.au3>
;#include "E:\Scripts\Workshop\PrintMG.dll"
#include "PrintMGV2.au3"
Local $mmssgg

Dim $address = @TempDir & "\screenshot.jpg"
WinActivate( "Panel")
_ScreenCapture_Capture($address)

$hp = _PrintDllStart($mmssgg);this must always be called first
if $hp = 0 then
    consolewrite("Error from dllstart = " & $mmssgg & @CRLF)
    Exit
endif
;if you don't want the default printer use one of these
;$newselect = _PrintSelectPrinter($hp,$printername);sets printername to be used
;or
;_PrintSetPrinter($hp);lets you choose the printer


_PrintPageOrientation($hp,0);landscape

_PrintStartPrint($hp)

;get printer paper dimensions
$pght = _PrintGetpageheight($hp) - _PrintGetYOffset($hp)
$pgwd = _PrintGetpageWidth($hp) - _PrintGetXOffset($hp)

;write a title
_PrintSetFont($hp,'Arial',18,0,'bold,underline')
$Title = "Screenshot for ragemonkey"
$tw = _PrintGetTextWidth($hp,$Title)
$th = _PrintGetTextHeight($hp,$title)
_PrintText($hp,$title,Int($pgwd/2 - $tw/2),_PrintGetYOffset($hp))


;We'll fit the image into only one half of the paper
;the image is of the whole screen so will the paper ht be enough?

$MonitorAspect = @DesktopWidth/@DesktopHeight
;Assume we will use at most 80% of paper height
$PrintImageHt = $pght * 0.8
$PrintImageWid = $pgwd*0.5

$HalPAgeAspect = ($pgwd*0.5)/($pght * 0.8)

If $HalPAgeAspect > $MonitorAspect Then
;we must change the printed image width to match the available paper height
    $PrintImageWidth = $printImageHt*$MonitorAspect
Else
;We set the height to suit the half page width
    $printImageHt = $printImageWid/$MonitorAspect
EndIf
;set image 10mm below bottom of title
_PrintImage($hp,$address,_PrintGetXOffset($hp),_PrintGetYOffset($hp) + $th + 100 ,$PrintImageWid,$PrintImageHt)

_PrintEndPrint($hp)
_printDllClose($hp)

It's a quick example so not much error checking. The example in the udf thread shows how to do more.


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

Martin you are a legend!!! thank you for going to all the effort. I would send you a case of beer if i knew your address :)

I have downloaded and extracted printMGv2 to the \AutoIt3\Include directory and replaced the dll in my E:\Scripts\Workshop directory. created a new script called "Martin" >_< and executed.

This is the error i recieve straight up.

>"C:\Program Files\AutoIt3\SciTE\..\autoit3.exe" /ErrorStdOut "E:\Scripts\Workshop\Martin.au3"

E:\Scripts\Workshop\martin.au3 (124) : ==> Unterminated string.:

$sErr = 'Failed to open ' & $vPath'

>Exit code: 1 Time: 0.209

The confusing thing about this is there is no line 124 in the script it only goes to 58. Do you know why this would happen?

oh, since i put the udf in the include directory i now have

#include <PrintMGV2.au3>
instead of
#include "PrintMGV2.au3"
i think this is right

AutoIT [L] PlatesI have found these posts really usefull, maybe you will too.Martins Print UDF

Share this post


Link to post
Share on other sites

ok, so i worked out line 124 is in the UDF. The function below is where the error is

Func _PrintDllStart(ByRef $sErr, $vPath = 'printmg.dll')
    
    $hDll = DllOpen($vPath)
    If $hDll = -1 Then
        SetError(1)
        $sErr = 'Failed to open ' & $vPath'
        Return -1;failed
    EndIf

    Return $hDll;ok
    
    
EndFunc

AutoIT [L] PlatesI have found these posts really usefull, maybe you will too.Martins Print UDF

Share this post


Link to post
Share on other sites

###############OMFG#######################

Martin, i changed the function in the UDF as below and the script worked!!!!!!!!!!!!!!!!!!!!!!!!!!!

Func _PrintDllStart(ByRef $sErr, $vPath = 'printmg.dll')
    
    $hDll = DllOpen($vPath)
    If $hDll = -1 Then
        SetError(1)
        $sErr = ("Failed to open" & $vPath)
        Return -1;failed
    EndIf

    Return $hDll;ok
    
    
EndFunc

Thank you sooo much Martin!!! You have helped me greatly and I will keep going with this and modify to perfection*. I will keep on posting here my results for others to learn aswell.

/yell Martin for Prezzident!


AutoIT [L] PlatesI have found these posts really usefull, maybe you will too.Martins Print UDF

Share this post


Link to post
Share on other sites

I have changed the print options so that a full page image with title prints instead. here is the code.

#include <ScreenCapture.au3>
;#include "E:\Scripts\Workshop\PrintMG.dll"
#include <PrintMGV2.au3>
Local $mmssgg

Dim $address = @TempDir & "\screenshot.jpg"
WinActivate( "Panel")
_ScreenCapture_Capture($address)

$hp = _PrintDllStart($mmssgg);this must always be called first
if $hp = 0 then
    consolewrite("Error from dllstart = " & $mmssgg & @CRLF)
    Exit
endif
;if you don't want the default printer use one of these
;$newselect = _PrintSelectPrinter($hp,$printername);sets printername to be used
;or
;_PrintSetPrinter($hp);lets you choose the printer


_PrintPageOrientation($hp,0);landscape

_PrintStartPrint($hp)

;get printer paper dimensions
$pght = _PrintGetpageheight($hp) - _PrintGetYOffset($hp)
$pgwd = _PrintGetpageWidth($hp) - _PrintGetXOffset($hp)

;write a title
_PrintSetFont($hp,'Arial',18,0,'bold,underline')
$Title = "Back Order Parts"
$tw = _PrintGetTextWidth($hp,$Title)
$th = _PrintGetTextHeight($hp,$title)
_PrintText($hp,$title,Int($pgwd/2 - $tw/2),_PrintGetYOffset($hp))


;We'll fit the image into only one half of the paper
;the image is of the whole screen so will the paper ht be enough?

$MonitorAspect = @DesktopWidth/@DesktopHeight
;Assume we will use at most 80% of paper height
$PrintImageHt = $pght * 0.8
;~ $PrintImageWid = $pgwd*0.5
$PrintImageWid = $pgwd * 1.0

$HalPAgeAspect = ($pgwd*0.5)/($pght * 0.8)

If $HalPAgeAspect > $MonitorAspect Then
;we must change the printed image width to match the available paper height
    $PrintImageWidth = $printImageHt*$MonitorAspect
;~ Else
;~;We set the height to suit the half page width
;~   $printImageHt = $printImageWid/$MonitorAspect
EndIf
;set image 10mm below bottom of title
_PrintImage($hp,$address,_PrintGetXOffset($hp),_PrintGetYOffset($hp) + $th + 100 ,$PrintImageWid,$PrintImageHt)

_PrintEndPrint($hp)
_printDllClose($hp)

AutoIT [L] PlatesI have found these posts really usefull, maybe you will too.Martins Print UDF

Share this post


Link to post
Share on other sites

###############OMFG#######################

Martin, i changed the function in the UDF as below and the script worked!!!!!!!!!!!!!!!!!!!!!!!!!!!

Func _PrintDllStart(ByRef $sErr, $vPath = 'printmg.dll')
    
    $hDll = DllOpen($vPath)
    If $hDll = -1 Then
        SetError(1)
        $sErr = ("Failed to open" & $vPath)
        Return -1;failed
    EndIf

    Return $hDll;ok
    
    
EndFunc

Thank you sooo much Martin!!! You have helped me greatly and I will keep going with this and modify to perfection*. I will keep on posting here my results for others to learn aswell.

/yell Martin for Prezzident!

Well done Ragemonkey. Thanks for pointing out the error in my udf. The line should actually be

$sErr = 'Failed to open' & $vPath

but your change gives the same result. I have corrected the downlod now.


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

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  
Followers 0