Jump to content

CapturePro


Jordban
 Share

Recommended Posts

Hello,

CapturePro is a simple little program that will continuously take screen shots of the whole screen or a specified section. This is my first ever Auto It script. I have done a lot of PHP/MySQL, Flash AS, C++, and Java in the past, so I find the syntex very annoying. However I think we have a wonderful manual, and great functions! I can't wait to see what can be done with this program.

The code:

; +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
; Program Name: Capture Pro                                                  +
; Description: Saves the entire scrren (or a window) in JPEG format.            +
; Author: Jordan Berry (Jordban)                                                +
; Date: Jull 22, 2006                                                          +
; +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

; +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
; Script Init                                                                  +
; +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#include <GUIConstants.au3>

Dim $filenames, $qualitys, $intervals, $xs, $ys, $widths, $heights, $i = 0, $capture = 0, $setboxflag = 0, $message = 0

If Not FileExists("captured") Then
    DirCreate("captured")
EndIf

; GUI setup
Opt("GUIOnEventMode", 1)
$mainwindow = GUICreate("Capture Pro Version 0.1", 275, 198)
GUISetIcon("icon.ico")

; labels
GUICtrlCreateLabel("Filename:", 8, 11)
GUICtrlCreateLabel("Interval:", 8, 33)
GUICtrlCreateLabel("(1000 = 1 second)", 122, 33)
GUICtrlCreateLabel("Quality:", 8, 55)
GUICtrlCreateLabel("(0-100)", 93, 55)
GUICtrlCreateLabel("Start X:", 8, 77)
GUICtrlCreateLabel("Start Y:", 8, 99)
GUICtrlCreateLabel("Width:", 8, 121)
GUICtrlCreateLabel("Height:", 8, 143)

; input
$filename = GUICtrlCreateInput("screen_capture_", 55, 8, 120, 20)
$interval = GUICtrlCreateInput("1000", 55, 30, 60, 20)
$quality = GUICtrlCreateInput("60", 55, 52, 30, 20)
$x = GUICtrlCreateInput("0", 55, 74, 40, 20)
$y = GUICtrlCreateInput("0", 55, 96, 40, 20)
$width = GUICtrlCreateInput("0", 55, 118, 40, 20)
$height = GUICtrlCreateInput("0", 55, 140, 40, 20)

; buttons
$startselected = GUICtrlCreateButton("Capture Region", 40, 168, 100, 22)
$startwhole = GUICtrlCreateButton("Capture All", 160, 168, 100, 22)
$setbox = GUICtrlCreateButton("Set Selected Capture Region", 110, 100, 150, 22)
$stopcapture = GUICtrlCreateButton("Stop Capture", 140, 134, 80, 22)

; styles
GUICtrlSetStyle($interval, $ES_NUMBER)
GUICtrlSetStyle($quality, $ES_NUMBER)
GUICtrlSetStyle($x, $ES_NUMBER)
GUICtrlSetStyle($y, $ES_NUMBER)
GUICtrlSetStyle($width, $ES_NUMBER)
GUICtrlSetStyle($height, $ES_NUMBER)
GUICtrlSetStyle($filename, $ES_OEMCONVERT)

; events
GUISetOnEvent($GUI_EVENT_CLOSE, "Exitp")
GuiCtrlSetOnEvent($setbox, "SetBox")
GuiCtrlSetOnEvent($startselected, "StartCaptureSelected")
GuiCtrlSetOnEvent($startwhole, "StartCaptureWhole")
GuiCtrlSetOnEvent($stopcapture, "StopCapturing")

GUISetState(@SW_SHOW)

; +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
; Main Loop                                                                  +
; +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
While 1
    sleep(10)
    If $setboxflag = 1 Then
        If $message = 1 Then
            ToolTip("Put mouse at top left corner of box." & @LF & "Then push space.")
        ElseIf $message = 2 Then
            ToolTip("Put mouse at bottom right corner of box." & @LF & "Then push space.")
        EndIf
    EndIf
    If $capture = 1 Then
        Sleep($intervals)
        $i = $i + 1
        CaptureScreen("captured/" & $filenames & $i & ".jpg", $xs, $ys, $widths, $heights, $qualitys)
    EndIf
WEnd
; +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
; Functions                                                                  +
; +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Func CaptureScreen($filename, $x, $y, $width, $height, $quality)
    Return DllCall("captdll.dll", "int", "CaptureRegion", "str", $filename, "int", $x, "int", $y, "int", $width, "int", $height, "int", $quality)
EndFunc  ;==>CaptureScreen

Func Exitp()
    Exit
EndFunc  ;==>Exitp

Func CordSet()
    If $setboxflag = 1 Then
        If $message = 1 Then
            $pos = MouseGetPos()
            Global $tempx = $pos[0]
            Global $tempy = $pos[1]
            GUICtrlSetData($x, $tempx)
            GUICtrlSetData($y, $tempy)
            $message = 2
        Else
            $pos = MouseGetPos()
            $tempwidth = $pos[0] - $tempx
            $tempheight = $pos[1] - $tempy
            If $tempwidth < 0 Then
                $tempwidth = $tempwidth * - 1
            EndIf
            If $tempheight < 0 Then
                $tempheight = $tempheight * - 1
            EndIf
            GUICtrlSetData($width, $tempwidth)
            GUICtrlSetData($height, $tempheight)
            ToolTip("")
            $setboxflag = 0;
            GUISetState(@SW_SHOW)
            HotKeySet("{SPACE}")
            
            
        EndIf
    EndIf
    
EndFunc  ;==>CordSet

Func SetBox()
    $message = 1
    $setboxflag = 1;
    GUISetState(@SW_HIDE)
    HotKeySet("{SPACE}", "CordSet")
EndFunc  ;==>SetBox

Func StartCaptureSelected()
    $filenames = GUICtrlRead($filename)
    $intervals = GUICtrlRead($interval)
    $qualitys = GUICtrlRead($quality)
    $xs = GUICtrlRead($x)
    $ys = GUICtrlRead($y)
    $widths = GUICtrlRead($width)
    $heights = GUICtrlRead($height)
    If ValidateInput() = 0 Then
        $capture = 1;
    EndIf
EndFunc  ;==>StartCaptureSelected

Func StartCaptureWhole()
    $filenames = GUICtrlRead($filename)
    $qualitys = GUICtrlRead($quality)
    $intervals = GUICtrlRead($interval)
    $xs = 0
    $ys = 0
    $widths = @DesktopWidth
    $heights = @DesktopHeight
    If ValidateInput() = 0 Then
        $capture = 1;
    EndIf
EndFunc  ;==>StartCaptureWhole
Func ValidateInput()
    $errorflag = 0
    $errorlog = ""
    If $qualitys < 0 Or $qualitys > 100 Then
        $errorflag = 1
        $errorlog = $errorlog & @LF & "Quality must be between 0 and 100."
    EndIf
    If $intervals < 0 Then
        $errorflag = 1
        $errorlog = $errorlog & @LF & "Interval can't be less than 0."
    EndIf
    
    If $errorflag = 1 Then
        MsgBox(16, "Error(s)", "These error(s) were found:" & $errorlog)
        Return 1
    Else
        Return 0
    EndIf
EndFunc  ;==>ValidateInput

Func StopCapturing()
    If $i = 0 Then
        MsgBox(16, "Stop?", "Haven't started yet!")
    Else
        $tempi = $i
        $i = 0
        $capture = 0
        MsgBox(64, "Success!", $tempi & " Screenshots were taken.")
    EndIf
EndFunc  ;==>StopCapturing

Maybe someone can find this useful =)

Please feel free to comment on my code, technique, etc.

Thanks,

Jordan

Oh and yes the DLL is from http://www.autoitscript.com/fileman/users/Lazycat/dlls.html

EDIT:

Removed uneeded ELSE and added small sleep to main loop.

CapturePro.au3

captdll.dll

icon.ico

Edited by Jordban
Link to comment
Share on other sites

  • Moderators

Nice job, it's evident you have coding experience... I didn't go through the whole thing, just a couple of things I noticed that will make sense to you.

If
;Something
Else; This doesn't need to be here if there is nothing more to do if the condition isn't true.
EndIf

AutoIt goes through it's code quickly, so you might look at putting a sleep() in your while loops, even if it's a Sleep(10), this will save on cpu hoggage.

If your going to have a variable within a function only, no need to declare it Global when in actuality it's in a Local scope.

But I think it's a fine job. :D

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

Welcome Jordban,

I find au3 syntax a lot easier than other progs, but then again, that's a matter of taste.

Couple of tips

1) definitely use a small sleep in the main loop

2) single line sentences shorten code

If Not FileExists("captured") Then DirCreate("captured")

3) in CordSet() you might preferably want to use a select..case as you are evaluating a condition for the value of $message, and altering it's value within the if

Otherwise, nice. Lazycat made a similar udf, it's in the scripts and scraps. I'm after a tool that grabs stuff from background windows.

IVAN

Link to comment
Share on other sites

@SmOke_N

The else statement thing is well, just personal preference. I would only do it if the second block of code is related to the first block, it ties them together, making it easier for me to read. I don't think you realized this is just more of a personal preference thing, instead of a misunderstanding of the else statement. Also that way the second block cant be exicuted if the first block is exicuted. Thanks for the kind words :D

@ivan

I'm glad you find it easier. I think it really just has to do with your previous experience. I'm used to using { } and () more often, instead of typing out then wend, etc. However, I do like the use of variables and constant strings within a function parameter, Flash does this, and I think it's wonderful. Thanks for the advice; I’ll add a small sleep. The one line code, didn't do this because of lack of syntax know-how. But now I know, thanks.

I'm actually after the same program, please tell me if you find anything.

Edited by Jordban
Link to comment
Share on other sites

  • Moderators

Jordban, I was only speaking that if you weren't going to put anything after the "Else" it was unnecessary to use it. It performs an unnecessary "extra" look for something that isn't there.

This is the only place you did it, so I don't think it was intentional on your behalf:

Func CordSet()
    If $setboxflag = 1 Then
        If $message = 1 Then
            $pos = MouseGetPos()
            Global $tempx = $pos[0]
            Global $tempy = $pos[1]
            GUICtrlSetData($x, $tempx)
            GUICtrlSetData($y, $tempy)
            $message = 2
        Else
            $pos = MouseGetPos()
            $tempwidth = $pos[0] - $tempx
            $tempheight = $pos[1] - $tempy
            If $tempwidth < 0 Then
                $tempwidth = $tempwidth * - 1
            EndIf
            If $tempheight < 0 Then
                $tempheight = $tempheight * - 1
            EndIf
            GUICtrlSetData($width, $tempwidth)
            GUICtrlSetData($height, $tempheight)
            ToolTip("")
            $setboxflag = 0;
            GUISetState(@SW_SHOW)
            HotKeySet("{SPACE}")
            
            
        EndIf
    Else
        
    EndIf
EndFunc ;==>CordSet
Also, if you don't like typing out all the keywords, use the latest SciTe... When typing in Keywords, all you have to do is type in 1 or 2 letters and you can hit enter for the correct keyword to be inserted. Makes the coding a tad faster like your used to. Edited by SmOke_N

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

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