Jump to content

Printing foreground window


Recommended Posts

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.

Link to comment
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

Link to comment
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.

Edited by BigDod


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

Link to comment
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.

Link to comment
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

Link to comment
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)
Link to comment
Share on other sites

  • 5 weeks later...

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).

Link to comment
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

Link to comment
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).

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
Link to comment
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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

  • Moderators

:P Nice!

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

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.

Link to comment
Share on other sites

[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
Link to comment
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

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...