Jump to content

select area with drag box


Recommended Posts

I'm sure this is on the forum somewhere but I can't seem to find the right search words. I'm working with a screenshot script and would like to be able to select and area for a screen shot. Click drag a box on the screen then let go and I could record the coordinates. I can't seem to figure out how create a click and drag box. Anyone have any idea?

Thanks.

Link to comment
Share on other sites

Here's a cheap way to do it:

#include<Misc.au3>

$first = True
$in = False

While 1 
    While _IsPressed('01') 
        If $first then 
            $pos = MouseGetPos() 
            $in = True
            $first = False
        EndIf
    WEnd
    
    If $in then 
        $secondPos = MouseGetPos()
        MsgBox(0, 'Coords', 'First pos: (' & $pos[0] & ', ' & $pos[1] & ')' & @CRLF & 'Second pos: (' & $secondPos[0] & ', ' & $secondPos[1] & ')')
        $in = False 
        $first = True
    EndIf 
WEnd
My Programs[list][*]Knight Media Player[*]Multiple Desktops[*]Daily Comics[*]Journal[/list]
Link to comment
Share on other sites

Ok this seems silly. I'm sure there is a better way to do it. But I'm having trouble "thinking outside the box". The only way I could think of to accomplish it was to setup gui's. A few mild problems but this is what I have so far. Unless anyone else has any better ideas, as I'd love to hear them.

#include <GUIConstants.au3>
#include<Misc.au3>

;T
$BoxTop = GUICreate('',2,2,0,0,$WS_POPUP,$WS_EX_TOPMOST);WHLT
GUISetBkColor(0x00ff00);Green

;R
$BoxRight = GUICreate('',2,2,2,0,$WS_POPUP,$WS_EX_TOPMOST,$BoxTop)
GUISetBkColor(0x00ff00)

;B
$BoxBottom = GUICreate('',2,2,2,2,$WS_POPUP,$WS_EX_TOPMOST,$BoxTop);WHLT
GUISetBkColor(0x00ff00)

;L
$BoxLeft = GUICreate('',2,2,0,2,$WS_POPUP,$WS_EX_TOPMOST,$BoxTop);WHLT
GUISetBkColor(0x00ff00)

$first = True
$in = False
While 1
    
    If Not (WinActive($BoxTop)) Then WinActivate($BoxTop)
    
    While _IsPressed('01')
        If $first then
            $pos = MouseGetPos()
            $left = $pos[0]
            $top = $pos[1]
            $right = $left+2
            $bottom = $top+2
            
            $in = True
            $first = False
            
            GUISetState (@SW_SHOW,$BoxBottom) 
            GUISetState (@SW_SHOW,$BoxLeft) 
            GUISetState (@SW_SHOW,$BoxRight) 
            GUISetState (@SW_SHOW,$BoxTop) 
            
        ElseIf $in Then
            $secondPos = MouseGetPos()
            
            Select
                Case $secondPos[0] < $pos[0] And $secondPos[1] < $pos[1]
                    WinMove($BoxTop,'',$secondPos[0],$pos[1],$pos[0]-$secondPos[0],2);LTWH
                    WinMove($BoxBottom,'',$secondPos[0]+2,$secondPos[1],$pos[0]-$secondPos[0],2);LTWH
                    WinMove($BoxRight,'',$secondPos[0],$secondPos[1],2,$pos[1]-$secondPos[1]);LTWH
                    WinMove($BoxLeft,'',$pos[0],$secondPos[1]+2,2,$pos[1]-$secondPos[1]);LTWH
                Case $secondPos[0] < $pos[0] And $secondPos[1] > $pos[1]
                    WinMove($BoxLeft,'',$pos[0],$pos[1],2,$secondPos[1]-$pos[1]);LTWH
                    WinMove($BoxRight,'',$secondPos[0],$pos[1]+2,2,$secondPos[1]-$pos[1]);LTWH
                    WinMove($BoxTop,'',$secondPos[0],$pos[1],$pos[0]-$secondPos[0],2);LTWH
                    WinMove($BoxBottom,'',$secondPos[0]+2,$secondPos[1],$pos[0]-$secondPos[0],2);LTWH
                Case $secondPos[0] > $pos[0] And $secondPos[1] < $pos[1]
                    WinMove($BoxTop,'',$pos[0],$pos[1],$secondPos[0]-$pos[0]+2,2);LTWH
                    WinMove($BoxBottom,'',$pos[0],$secondPos[1],$secondPos[0]-$pos[0],2);LTWH
                    WinMove($BoxRight,'',$secondPos[0],$secondPos[1],2,$pos[1]-$secondPos[1]);LTWH
                    WinMove($BoxLeft,'',$pos[0],$secondPos[1]+2,2,$pos[1]-$secondPos[1]);LTWH
                Case Else
                    WinMove($BoxTop,'',$pos[0],$pos[1],$secondPos[0]-$pos[0],2);LTWH
                    WinMove($BoxRight,'',$secondPos[0],$pos[1],2,$secondPos[1]-$pos[1]);LTWH
                    WinMove($BoxBottom,'',$pos[0]+2,$secondPos[1],$secondPos[0]-$pos[0],2);LTWH
                    WinMove($BoxLeft,'',$pos[0],$pos[1]+2,2,$secondPos[1]-$pos[1]);LTWH
            EndSelect
        EndIf
    WEnd
    
    If $in then
        GUISetState (@SW_HIDE,$BoxBottom) 
        GUISetState (@SW_HIDE,$BoxLeft) 
        GUISetState (@SW_HIDE,$BoxRight) 
        GUISetState (@SW_HIDE,$BoxTop) 
        
        WinMove($BoxTop,'',0,0,2,2);LTWH
        WinMove($BoxRight,'',2,0,2,2);LTWH
        WinMove($BoxBottom,'',2,2,2,2);LTWH
        WinMove($BoxLeft,'',0,2,2,2);LTWH
        
        $secondPos = MouseGetPos()
        ;MsgBox(0, 'Coords', 'First pos: (' & $pos[0] & ', ' & $pos[1] & ')' & @CRLF & 'Second pos: (' & $secondPos[0] & ', ' & $secondPos[1] & ')')
        ToolTip('First pos: (' & $pos[0] & ', ' & $pos[1] & ')' & @CRLF & 'Second pos: (' & $secondPos[0] & ', ' & $secondPos[1] & ')',0,0)
        $in = False
        $first = True
    EndIf
WEnd
Edited by stampy
Link to comment
Share on other sites

I'm sure this is on the forum somewhere but I can't seem to find the right search words. I'm working with a screenshot script and would like to be able to select and area for a screen shot. Click drag a box on the screen then let go and I could record the coordinates. I can't seem to figure out how create a click and drag box. Anyone have any idea?

Thanks.

Here's a simple way to show a dragged area.

#include<Misc.au3>
#include <windowsconstants.au3>


$area = GUICreate("", 1, 1, 1, 1, $WS_POPUP, $WS_EX_TOPMOST)
GUISetBkColor(0x0000ff, $area)
WinSetTrans($area, "", 80)
GUISetState()
While 1
    If _IsPressed("1") Then
        $mp = MouseGetPos()
        WinMove($area, "", $mp[0], $mp[1], 1, 1)
        
        While _IsPressed('01')
            $pos = MouseGetPos()
            $lefts = Order($mp[0], $pos[0])
            $tops = Order($mp[1], $pos[1])
            WinMove($area, "", $lefts[0], $tops[0], $lefts[1], $tops[1])
            ConsoleWrite($lefts[0] & ', ' & $tops[0] & ', ' & $lefts[1] & ', ' & $tops[1] & @CRLF)
        WEnd
    EndIf


WEnd

Func Order($a, $b)
    Dim $res[2]
    If $a < $b Then
        $res[0] = $a
        $res[1] = $b - $a
    Else
        $res[0] = $b
        $res[1] = $a - $b
    EndIf
    Return $res
    
EndFunc  ;==>Order
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.
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...