Sign in to follow this  
Followers 0
PartyPooper

Printing foreground window

25 posts in this topic

I'm using AutoIt to create graphs in a GUI window and would like to add a "print graph" button. Not too fussed out the window borders being in the printout because they are insignificant and was wondering if there a better way to print the foregound window than:

WinActivate($WindowTitle)

Send ("!{PRINTSCREEN}")

Run("mspaint.exe", "", @SW_MAXIMIZE)

WinWaitActive("untitled - Paint")

Send ("^v")

Sleep(2000)

Send ("^p")

where $WindowTitle is the foreground window you want printed

Thanks.

Share this post


Link to post
Share on other sites



Anyone?

Share this post


Link to post
Share on other sites

for the screen capture part, u might want to look at this:

http://www.autoitscript.com/forum/index.ph...16952&hl=plugin


The speed of sound is defined by the distance from door to computer divided by the time interval needed to close the media player and pull up your pants when your mom shouts "OH MY GOD WHAT ARE YOU DOING!!!"

Share this post


Link to post
Share on other sites

not sure how to do exactly that... but this is a cleaner way of doing it your way

WinActivate("?TITLE?")
Send ("!{PRINTSCREEN}")
Run("mspaint.exe", "", @SW_HIDE)
Sleep(2000)
ControlSend ("untitled - Paint", "", "", "^v")
Sleep(2000)
ControlSend ("untitled - Paint", "", "", "^p")

8)


NEWHeader1.png

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

Here is an example of printing the whole screen using LazyCat's capture plugin which can be down loaded http://www.autoitscript.com/fileman/users/Lazycat/. You will also need to down load and install Irfanview which can be downloaded Here

DllCall("captdll.dll", "int", "CaptureScreen", "str", "dump_full.jpg", "int", 85)
$prog = FileGetShortName("C:\Program Files\irfanview\i_view32.exe")
Runwait ($prog & " dump_full.jpg /print")

There is also a CaptureRegion option which may suit your needs better.

Edit

You may even be able to ask LazyCat if it is possible to add a PrintActive option to the dll.

Edited by BigDod


Get Beta versions Here Get latest SciTE editor Here AutoIt 1-2-3 by Valuater - A great starting point.

Time you enjoyed wasting is not wasted time ......T.S. Elliot
Suspense is worse than disappointment................Robert Burns
God help the man who won't help himself, because no-one else will...........My Grandmother

Share this post


Link to post
Share on other sites

for the screen capture part, u might want to look at this:

http://www.autoitscript.com/forum/index.ph...16952&hl=plugin

Yes, saw LazyCat's plugin but it screen shots the entire screen or a region so it would've required more code to identify the exact region I wanted printed.

Thanks for the response anyway.

Share this post


Link to post
Share on other sites

not sure how to do exactly that... but this is a cleaner way of doing it your way

WinActivate("?TITLE?")
Send ("!{PRINTSCREEN}")
Run("mspaint.exe", "", @SW_HIDE)
Sleep(2000)
ControlSend ("untitled - Paint", "", "", "^v")
Sleep(2000)
ControlSend ("untitled - Paint", "", "", "^p")

8)

Ah great, thanks Valuater, I didn't know about ControlSend. I wasn't happy sending keystrokes into the blue because having once used Automate, I realised the pitfalls of that method.

Share this post


Link to post
Share on other sites

Here is an example of printing the whole screen using LazyCat's capture plugin which can be down loaded http://www.autoitscript.com/fileman/users/Lazycat/. You will also need to down load and install Irfanview which can be downloaded Here

DllCall("captdll.dll", "int", "CaptureScreen", "str", "dump_full.jpg", "int", 85)
$prog = FileGetShortName("C:\Program Files\irfanview\i_view32.exe")
Runwait ($prog & " dump_full.jpg /print")

There is also a CaptureRegion option which may suit your needs better.

Edit

You may even be able to ask LazyCat if it is possible to add a PrintActive option to the dll.

Already looked at his plugin and it looks great but I wanted to avoid users having to download an additional program just to get screen shots. B)

Thanks anyway

Share this post


Link to post
Share on other sites

A minor improvement over my original script thanks to Valuater:

GUISetCursor(15, 1)
    WinActivate($WindowTitle)
    Send ("!{PRINTSCREEN}")
    Run("mspaint.exe", "", @SW_HIDE)
    WinWait("untitled - Paint")
    Sleep(200)
    ControlSend ("untitled - Paint", "", "", "^v")
    Sleep(200)
    ControlSend ("untitled - Paint", "", "", "^p")
    WinSetState("untitled - Paint", "",@SW_SHOW)
    GUISetCursor(2)

Share this post


Link to post
Share on other sites

Just an update to the MS Paint screen capturing routine I use:

Func OnScreenShot()
    Local $WindowTitle; selected window for capturing
    Local $CaptureDirectory = @ScriptDir & "\Screenshots\"; where to store screen caps
    GUISetCursor(15, 1)
    Do
        ProcessClose("mspaint.exe")
    Until Not ProcessExists("mspaint.exe")
    WinActivate($WindowTitle)
    Send("!{PRINTSCREEN}")
    Run("mspaint.exe", "", @SW_HIDE)
    WinWait("untitled - Paint")
    Sleep(500)
    ControlSend("untitled - Paint", "", "", "^v")
    Sleep(500)
    ControlSend("untitled - Paint", "", "", "^s")
    WinWait("Save As", "")
    WinSetState ("Save As", "untitled", @SW_HIDE)
    Sleep(500)
    ControlSend("Save As", "", "Edit1", $CaptureDirectory & @YEAR & "-" & @MON & "-" & @MDAY & "_" & @HOUR & @MIN & "-" & @SEC & ".bmp")
    ControlClick("Save As", "", "Button2")
    opt("WinTitleMatchMode", 2)
    WinClose(" - Paint")
    opt("WinTitleMatchMode", 1)
    GUISetCursor(2, 0)
EndFunc

It save the captures in the screenshot directory with a filename format of 'yyyy-mm-dd_hhmm-ss.bmp' (eg: 2005-12-17_2305-15.bmp). It's almost a transparent operation (the Save As window flashes up for a few milliseconds).

Share this post


Link to post
Share on other sites

looks good

8)


NEWHeader1.png

Share this post


Link to post
Share on other sites

Thanks Valuater. I'm hoping I'll work out a way to make it a fully transparent operation. The only thing I add to it when I use it is a couple of extra lines to update an AutoIt GUI statusbar.

P.S. if any devs read this, if possible, can you change the Topic from 'Printing Foreground Window' to 'Screen Capturing Foreground Window'. I don't know why I typed that topic - musta had too many beers when I posted the damn thing :P

Share this post


Link to post
Share on other sites

#13 ·  Posted (edited)

Just an update to the MS Paint screen capturing routine I use:

Func OnScreenShot()
    Local $WindowTitle; selected window for capturing
    Local $CaptureDirectory = @ScriptDir & "\Screenshots\"; where to store screen caps
    GUISetCursor(15, 1)
    Do
        ProcessClose("mspaint.exe")
    Until Not ProcessExists("mspaint.exe")
    WinActivate($WindowTitle)
    Send("!{PRINTSCREEN}")
    Run("mspaint.exe", "", @SW_HIDE)
    WinWait("untitled - Paint")
    Sleep(500)
    ControlSend("untitled - Paint", "", "", "^v")
    Sleep(500)
    ControlSend("untitled - Paint", "", "", "^s")
    WinWait("Save As", "")
    WinSetState ("Save As", "untitled", @SW_HIDE)
    Sleep(500)
    ControlSend("Save As", "", "Edit1", $CaptureDirectory & @YEAR & "-" & @MON & "-" & @MDAY & "_" & @HOUR & @MIN & "-" & @SEC & ".bmp")
    ControlClick("Save As", "", "Button2")
    opt("WinTitleMatchMode", 2)
    WinClose(" - Paint")
    opt("WinTitleMatchMode", 1)
    GUISetCursor(2, 0)
EndFunc

It save the captures in the screenshot directory with a filename format of 'yyyy-mm-dd_hhmm-ss.bmp' (eg: 2005-12-17_2305-15.bmp). It's almost a transparent operation (the Save As window flashes up for a few milliseconds).

This script just grabs and saves so why use paint ?? Totally useles to use paint here too slow too much interaction.

Already looked at his plugin and it looks great but I wanted to avoid users having to download an additional program just to get screen shots.

Just add the captdll.dll in the exe with fileinstall its only 46kbyte works much better then crappy paint way trust me

http://www.autoitscript.com/fileman/users/Lazycat/

edit: I see you did not even want to print only capture then the hole usage of paint is totally useless.

Then

DirCreate ( @scriptdir & "\screenshots" )
FileInstall ( "captplugin.dll", @scriptdir & "" ,1)
$CaptureDirectory = @ScriptDir & "\Screenshots\"; where to store screen caps
$CaptureFile = "Dump" & @YEAR & "-" & @MON & "-" & @MDAY & "_" & @HOUR & @MIN & "-" & @SEC & ".jpg"
$hPlugin = PluginOpen(@scriptdir & "\captplugin.dll")
CaptureScreen($CaptureDirectory & $CaptureFile, 85)
PluginClose($hPlugin)

With plugin hmm thats new plugin thing but it works faster then old dll

much smaller better and more direct then using paint and printscreen old less problems with people perssing buttons at wrong moment

Edited by MrSpacely

Share this post


Link to post
Share on other sites

This script just grabs and saves so why use paint ?? Totally useles to use paint here too slow too much interaction.

Because I said:

Already looked at his plugin and it looks great but I wanted to avoid users having to download an additional program just to get screen shots.

I wanted to keep the resulting exe as small as possible and use a program that is already installed on the users computer.

Just add the captdll.dll in the exe with fileinstall its only 46kbyte works much better then crappy paint way trust me

http://www.autoitscript.com/fileman/users/Lazycat/

edit: I see you did not even want to print only capture then the hole usage of paint is totally useless.

Then

DirCreate ( @scriptdir & "\screenshots" )
FileInstall ( "captplugin.dll", @scriptdir & "" ,1)
$CaptureDirectory = @ScriptDir & "\Screenshots\"; where to store screen caps
$CaptureFile = "Dump" & @YEAR & "-" & @MON & "-" & @MDAY & "_" & @HOUR & @MIN & "-" & @SEC & ".jpg"
$hPlugin = PluginOpen(@scriptdir & "\captplugin.dll")
CaptureScreen($CaptureDirectory & $CaptureFile, 85)
PluginClose($hPlugin)

With plugin hmm thats new plugin thing but it works faster then old dll

much smaller better and more direct then using paint and printscreen old less problems with people perssing buttons at wrong moment

Yes but if you read:

Yes, saw LazyCat's plugin but it screen shots the entire screen or a region so it would've required more code to identify the exact region I wanted printed.

you would see that it wasn't what I was after. I was after something simple to capture the current foreground window without having to play around with trying to identify regions etc.

Thanks anyways.

Share this post


Link to post
Share on other sites

#15 ·  Posted (edited)

lets make everybody happy... 8)

HotKeySet("{F9}", "OnScreenShot")
HotKeySet("{ESC}", "Terminate")

While 1
    Sleep(20)
WEnd


Func OnScreenShot()
    DirCreate ( @scriptdir & "\screenshots" ) 
    If Not FileExists(@scriptdir & "\captplugin.dll") Then FileInstall( "captplugin.dll", @scriptdir & "" ,1)
    $Cwin1 = WinGetHandle(""); gets the handle of the active window
    $Cwin2 = WinGetPos($Cwin1)
    $CaptureDirectory = @ScriptDir & "\Screenshots\"; where to store screen caps
    $CaptureFile = "Dump" & @YEAR & "-" & @MON & "-" & @MDAY & "_" & @HOUR & @MIN & "-" & @SEC & ".jpg"
    $hPlugin = PluginOpen(@scriptdir & "\captplugin.dll")
    $Ctest = CaptureRegion($CaptureDirectory & $CaptureFile, $Cwin2[0], $Cwin2[1], $Cwin2[2], $Cwin2[3],  85)
    If $Ctest = 0 then MsgBox(64,"Capture Error!", "The screen was not captured", 4)
    PluginClose($hPlugin)
EndFunc

Func Terminate()
    Exit
EndFunc

utilizes - file install - thus end user does NOT need to install anything

utilizes - DLL call - faster and more effective, less area for error/input by end user

utilizes - Regions - thus the pic you want is what you get WYSIWYG (active)

savvy....

8)

Edited by Valuater

NEWHeader1.png

Share this post


Link to post
Share on other sites

:P Nice!

P.S. - misspelled word in MsgBox(64, "Ca*ture Error@", "The Screen was not captured", 4).


[center]Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.[/center]

Share this post


Link to post
Share on other sites

thanks...

and... nice catch smoke

8)


NEWHeader1.png

Share this post


Link to post
Share on other sites

#18 ·  Posted (edited)

[qoute]you would see that it wasn't what I was after. I was after something simple to capture the current foreground window without having to play around with trying to identify regions etc.

[/qoute]

It seems my example was good allready. It was what you were after small simple and no code to identify the region because I used capture screen wich means the whole screen the thing you were after from the start

And about keeping the filesize down captdll is 45 kb or so. My example code also is about 6 lines wich also kept it simple

As for valuators example nice addon only the hot keys might be a problem because escape might be used lots. I think a small gui would work a bit better to close the program and just a systray item.

Edited by MrSpacely

Share this post


Link to post
Share on other sites

It seems my example was good allready. It was what you were after small simple and no code to identify the region because I used capture screen wich means the whole screen the thing you were after from the start

No, please re-read what I posted. I wanted to capture the FOREGROUND window, not the WHOLE screen, hence, my use of <Alt><Printscreen> in the code I posted. Capturing a screen or foreground window was simple and never a problem, it only required me to press (or simulate pressing) the PrintScreen or Alt-PrintScreen buttons and this would put what I wanted captured into memory. Getting it from memory to disk was the difficult part, hence, my request for a cleaner version.

I agree, MS Paint certainly isn't a great way to do this kind of thing, however, manipulating windows and sending keystrokes is what AutoIt was initially written to do so it only made sense to use it for this purpose.

The reason I rejected LazyCat plugin was because it required me to work out screen regions to capture. This meant I had to add code to accommodate differences in user screen resolutions and since I'm capturing a window, it suffers the same fate as you pointed out earlier - what happens when people press buttons or move windows at the wrong moment? Granted, the chances of this happening are minimal, but there is still a chance it could.

Valuater posted some code I'm yet to try. I'll have to modify it slightly because I need to save captures in BMP format so they can be sent to a printer later (I find JPG too lossy for printing quality), however, the code looks promising (thanks Valuater).

It's a pity AutoIt doesn't have a 'ClipSave' function, then none of this would be necessary :P

Share this post


Link to post
Share on other sites

the file "resolution" only needs to be a "negative" number to save the file as a bmp

8)


NEWHeader1.png

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