Jump to content

Selection Box?


Recommended Posts

I've been searching for something like this for a couple days now and I would really like to be able to make something like this.

It's where you basicllay draw a box on your screen and it takes down the coords and saves them to a variable then you can search for a color within the selected region.

Here is a code I found that Melba23 posted and it has the basic idea of what I want but I dont want it to take a picture instead make the selection within the area.

#include <GuiConstantsEx.au3>
#include <WindowsConstants.au3>
#Include <ScreenCapture.au3>
#Include <Misc.au3>

Global $iX1, $iY1, $iX2, $iY2, $aPos, $sMsg, $sBMP_Path

; Create GUI
$hMain_GUI = GUICreate("Select Rectangle", 240, 50)

$hRect_Button   = GUICtrlCreateButton("Mark Area",  10, 10, 80, 30)
$hCancel_Button = GUICtrlCreateButton("Cancel",    150, 10, 80, 30)

GUISetState()

While 1

    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE, $hCancel_Button
            FileDelete(@ScriptDir & "\Rect.bmp")
            Exit
        Case $hRect_Button
            GUISetState(@SW_HIDE, $hMain_GUI)
            Mark_Rect()
            ; Capture selected area
            $sBMP_Path = @ScriptDir & "\Rect.bmp"
            _ScreenCapture_Capture($sBMP_Path, $iX1, $iY1, $iX2, $iY2, False)
            GUISetState(@SW_SHOW, $hMain_GUI)
            ; Display image
            $hBitmap_GUI = GUICreate("Selected Rectangle", $iX2 - $iX1 + 1, $iY2 - $iY1 + 1, 100, 100)
            $hPic = GUICtrlCreatePic(@ScriptDir & "\Rect.bmp", 0, 0, $iX2 - $iX1 + 1, $iY2 - $iY1 + 1)
            GUISetState()

    EndSwitch

WEnd

; -------------

Func Mark_Rect()

    Local $aMouse_Pos, $hMask, $hMaster_Mask, $iTemp
    Local $UserDLL = DllOpen("user32.dll")

    Global $hRectangle_GUI = GUICreate("", @DesktopWidth, @DesktopHeight, 0, 0, $WS_POPUP, $WS_EX_TOOLWINDOW + $WS_EX_TOPMOST)
    _GUICreateInvRect($hRectangle_GUI, 0, 0, 1, 1)
    GUISetBkColor(0)
    WinSetTrans($hRectangle_GUI, "", 50)
    GUISetState(@SW_SHOW, $hRectangle_GUI)
    GUISetCursor(3, 1, $hRectangle_GUI)

    ; Wait until mouse button pressed
    While Not _IsPressed("01", $UserDLL)
        Sleep(10)
    WEnd

    ; Get first mouse position
    $aMouse_Pos = MouseGetPos()
    $iX1 = $aMouse_Pos[0]
    $iY1 = $aMouse_Pos[1]

    ; Draw rectangle while mouse button pressed
    While _IsPressed("01", $UserDLL)

        $aMouse_Pos = MouseGetPos()

        ; Set in correct order if required
        If $aMouse_Pos[0] < $iX1 Then
            $iX_Pos = $aMouse_Pos[0]
            $iWidth = $iX1 - $aMouse_Pos[0]
        Else
            $iX_Pos = $iX1
            $iWidth = $aMouse_Pos[0] - $iX1
        EndIf
        If $aMouse_Pos[1] < $iY1 Then
            $iY_Pos = $aMouse_Pos[1]
            $iHeight = $iY1 - $aMouse_Pos[1]
        Else
            $iY_Pos = $iY1
            $iHeight = $aMouse_Pos[1] - $iY1
        EndIf

        _GUICreateInvRect($hRectangle_GUI, $iX_Pos, $iY_Pos, $iWidth, $iHeight)

        Sleep(10)

    WEnd

    ; Get second mouse position
    $iX2 = $aMouse_Pos[0]
    $iY2 = $aMouse_Pos[1]

    ; Set in correct order if required
    If $iX2 < $iX1 Then
        $iTemp = $iX1
        $iX1 = $iX2
        $iX2 = $iTemp
    EndIf
    If $iY2 < $iY1 Then
        $iTemp = $iY1
        $iY1 = $iY2
        $iY2 = $iTemp
    EndIf

    GUIDelete($hRectangle_GUI)
    DllClose($UserDLL)

EndFunc   ;==>Mark_Rect

Func _GUICreateInvRect($hWnd, $iX, $iY, $iW, $iH)

    $hMask_1 = _WinAPI_CreateRectRgn(0, 0, @DesktopWidth, $iY)
    $hMask_2 = _WinAPI_CreateRectRgn(0, 0, $iX, @DesktopHeight)
    $hMask_3 = _WinAPI_CreateRectRgn($iX + $iW, 0, @DesktopWidth, @DesktopHeight)
    $hMask_4 = _WinAPI_CreateRectRgn(0, $iY + $iH, @DesktopWidth, @DesktopHeight)

    _WinAPI_CombineRgn($hMask_1, $hMask_1, $hMask_2, 2)
    _WinAPI_CombineRgn($hMask_1, $hMask_1, $hMask_3, 2)
    _WinAPI_CombineRgn($hMask_1, $hMask_1, $hMask_4, 2)

    _WinAPI_DeleteObject($hMask_2)
    _WinAPI_DeleteObject($hMask_3)
    _WinAPI_DeleteObject($hMask_4)

    _WinAPI_SetWindowRgn($hWnd, $hMask_1, 1)

EndFunc

Kind of like when you go on your desktop and hold down a button on your mouse and it makes a selection, I want something like that but where it saves the coords from one point to another point.

I've messed around with Melba's code for quite awhile now but I havn't been able to modify it to make it do what I want, any help would be awesome.

Thanks.

Link to comment
Share on other sites

Hi,
Maybe this (replace only the main while with this code) :

While 1
 
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE, $hCancel_Button
            Exit
        Case $hRect_Button
            GUISetState(@SW_HIDE, $hMain_GUI)
            Mark_Rect()
 
            ConsoleWrite("Left: " & $iX1 & ", Top: " & $iY1 & ", Width: " & $iX2 & ", Height: " & $iY2 & @Lf)
            GUISetState(@SW_SHOW, $hMain_GUI)
    EndSwitch
 
WEnd

_
Br, FireFox.

Edited by FireFox
Link to comment
Share on other sites

Perfect, thank you for the quick reponse  :thumbsup:

I was a little confused at first as to what the coords were for X and Y but I figured it out.

Here it is a little clearer.

While 1
 
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE, $hCancel_Button
            Exit
        Case $hRect_Button
            GUISetState(@SW_HIDE, $hMain_GUI)
            Mark_Rect()
 
            ConsoleWrite("Top Left Corner X:" & $iX1 & ", Y:" & $iY1 & ",  Bottom Right Corner X:" & $iX2 & ", Y:" & $iY2)
            GUISetState(@SW_SHOW, $hMain_GUI)
    EndSwitch
 
WEnd

EDIT: I didn't want to make a new post so I'm just editing this one. Is there any way to use the Pixelsearch function like this: Pixelsearch(5, 5, 5, 5, $var)

I have everything working perfect but I need it to be able to read the input box and find the color the user inputs. Any suggestions?

Edited by gottygolly
Link to comment
Share on other sites

You said to look at the input box function but i dont know exactly what to look for.

Here is the code i currently have.

#include <GuiConstantsEx.au3>
#include <WindowsConstants.au3>
#Include <ScreenCapture.au3>
#Include <Misc.au3>

Global $iX1, $iY1, $iX2, $iY2, $aPos, $sMsg, $sBMP_Path, $ibr, $ib, $pixcolor

; Create GUI
$hMain_GUI = GUICreate("Select Rectangle", 240, 50)

$hRect_Button = GUICtrlCreateButton("Mark Area",10, 10, 80, 30)
$color = GUICtrlCreateButton("Hex",95,10,50,30)
$hCancel_Button = GUICtrlCreateButton("Cancel",150, 10, 80, 30)

GUISetState()

While 1

Switch GUIGetMsg()
Case $GUI_EVENT_CLOSE, $hCancel_Button
Exit
Case $hRect_Button
GUISetState(@SW_HIDE, $hMain_GUI)
Mark_Rect()

ConsoleWrite("Top Left Corner X:" & $iX1 & ", Y:" & $iY1 & ", Bottom Right Corner X:" & $iX2 & ", Y:" & $iY2)
GUISetState(@SW_SHOW, $hMain_GUI)
            MsgBox(4096,"Your Coords","Your coords are :" & "Top Left Corner X:" & $iX1 & ", Y:" & $iY1 & ", Bottom Right Corner X:" & $iX2 & ", Y:" & $iY2)
     input()
            EndSwitch
WEnd
Func input()
    $ib = InputBox("Hex","Type in the Hex number of the color you wish to find. Example: '0xFFFFFF' for white or 'FFFFFF' for white")
    $ibr = GUICtrlRead($ib)
MsgBox(4096,"HotKey","Press F11 to start")
HotKeySet("{F11}","findpix")
EndFunc

Func color()
$mousepos = MouseGetPos()
$pixcolor = PixelGetColor($mousepos[0],$mousepos[1])
MsgBox(4096,"Color Hex","Your Color Hex is : " & "0x"&Hex($pixcolor,6))
GUIDelete($color)
EndFunc

func findpix()
$location = PixelSearch ($iX1,$iY1,$iX2,$iY2,GUICtrlRead($ibr))
If IsArray($location) = True Then
MouseMove($location[0],$location[1], 5)
    EndIf
EndFunc
; -------------

Func Mark_Rect()

Local $aMouse_Pos, $hMask, $hMaster_Mask, $iTemp
Local $UserDLL = DllOpen("user32.dll")

Global $hRectangle_GUI = GUICreate("", @DesktopWidth, @DesktopHeight, 0, 0, $WS_POPUP, $WS_EX_TOOLWINDOW + $WS_EX_TOPMOST)
_GUICreateInvRect($hRectangle_GUI, 0, 0, 1, 1)
GUISetBkColor(0)
WinSetTrans($hRectangle_GUI, "", 50)
GUISetState(@SW_SHOW, $hRectangle_GUI)
GUISetCursor(3, 1, $hRectangle_GUI)

; Wait until mouse button pressed
While Not _IsPressed("01", $UserDLL)
Sleep(10)
WEnd

; Get first mouse position
$aMouse_Pos = MouseGetPos()
$iX1 = $aMouse_Pos[0]
$iY1 = $aMouse_Pos[1]

; Draw rectangle while mouse button pressed
While _IsPressed("01", $UserDLL)

$aMouse_Pos = MouseGetPos()

; Set in correct order if required
If $aMouse_Pos[0] < $iX1 Then
$iX_Pos = $aMouse_Pos[0]
$iWidth = $iX1 - $aMouse_Pos[0]
Else
$iX_Pos = $iX1
$iWidth = $aMouse_Pos[0] - $iX1
EndIf
If $aMouse_Pos[1] < $iY1 Then
$iY_Pos = $aMouse_Pos[1]
$iHeight = $iY1 - $aMouse_Pos[1]
Else
$iY_Pos = $iY1
$iHeight = $aMouse_Pos[1] - $iY1
EndIf

_GUICreateInvRect($hRectangle_GUI, $iX_Pos, $iY_Pos, $iWidth, $iHeight)

Sleep(10)

WEnd

; Get second mouse position
$iX2 = $aMouse_Pos[0]
$iY2 = $aMouse_Pos[1]

; Set in correct order if required
If $iX2 < $iX1 Then
$iTemp = $iX1
$iX1 = $iX2
$iX2 = $iTemp
EndIf
If $iY2 < $iY1 Then
$iTemp = $iY1
$iY1 = $iY2
$iY2 = $iTemp
EndIf

GUIDelete($hRectangle_GUI)
DllClose($UserDLL)

EndFunc ;==>Mark_Rect

Func _GUICreateInvRect($hWnd, $iX, $iY, $iW, $iH)

$hMask_1 = _WinAPI_CreateRectRgn(0, 0, @DesktopWidth, $iY)
$hMask_2 = _WinAPI_CreateRectRgn(0, 0, $iX, @DesktopHeight)
$hMask_3 = _WinAPI_CreateRectRgn($iX + $iW, 0, @DesktopWidth, @DesktopHeight)
$hMask_4 = _WinAPI_CreateRectRgn(0, $iY + $iH, @DesktopWidth, @DesktopHeight)

_WinAPI_CombineRgn($hMask_1, $hMask_1, $hMask_2, 2)
_WinAPI_CombineRgn($hMask_1, $hMask_1, $hMask_3, 2)
_WinAPI_CombineRgn($hMask_1, $hMask_1, $hMask_4, 2)

_WinAPI_DeleteObject($hMask_2)
_WinAPI_DeleteObject($hMask_3)
_WinAPI_DeleteObject($hMask_4)

_WinAPI_SetWindowRgn($hWnd, $hMask_1, 1)

EndFunc

When I edit the PixelSearch function from this:

$location = PixelSearch ($iX1,$iY1,$iX2,$iY2,GUICtrlRead($ibr))

to this:

$location = PixelSearch ($iX1,$iY1,$iX2,$iY2,0xFFFFFF)

it still finds White but it won't work with the first code and it also won't work like this:

$location = PixelSearch ($iX1,$iY1,$iX2,$iY2,$ibr)

$ibr is the GuiCtrlRead() of the InputBox but it still won't work.

(When I make the selection on the screen with the thing you helped me with it will still find White inside that selection but it won't work with a variable there).

Again sorry for bothering you about this but I really want to get this to work because i've been working on it for a couple hours in total and still can't get it to do what I want it to do.

Edited by gottygolly
Link to comment
Share on other sites

According to the helpfile, the color must be a decimal or hex value, and here you pass a string (returned by GUICtrlRead).

So, if you write only the hex value without the prefix ("0x") do :

$location = PixelSearch ($iX1,$iY1,$iX2,$iY2, Number("0x" & GUICtrlRead($ibr)))

;or
$location = PixelSearch ($iX1,$iY1,$iX2,$iY2, Dec(Hex("0x" & GUICtrlRead($ibr), 6))) 

Otherwise remove the prefix :

$location = PixelSearch ($iX1,$iY1,$iX2,$iY2, Number(GUICtrlRead($ibr)))

;or
$location = PixelSearch ($iX1,$iY1,$iX2,$iY2, Dec(Hex(GUICtrlRead($ibr), 6)))

Br, FireFox.

Link to comment
Share on other sites

I'm having another error...

When I press F11 to start the script that's supposed to move the mouse to the specified color it just goes to the first coordinates (Top left of the the selected area) instead of actually going to the color, anyone know why this happens?

Heres the code again:

#include <GuiConstantsEx.au3>
#include <WindowsConstants.au3>
#Include <ScreenCapture.au3>
#Include <Misc.au3>

Global $iX1, $iY1, $iX2, $iY2, $aPos, $sMsg, $sBMP_Path, $ibr, $ib, $pixcolor

; Create GUI
$hMain_GUI = GUICreate("Select Rectangle", 240, 50)

$hRect_Button   = GUICtrlCreateButton("Mark Area",10, 10, 80, 30)
$color = GUICtrlCreateButton("Hex",95,10,50,30)
$hCancel_Button = GUICtrlCreateButton("Cancel",150, 10, 80, 30)

GUISetState()

While 1
 
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE, $hCancel_Button
            Exit
        Case $hRect_Button
            GUISetState(@SW_HIDE, $hMain_GUI)
            Mark_Rect()
 
            ConsoleWrite("Top Left Corner X:" & $iX1 & ", Y:" & $iY1 & ",  Bottom Right Corner X:" & $iX2 & ", Y:" & $iY2)
            GUISetState(@SW_SHOW, $hMain_GUI)
            MsgBox(4096,"Your Coords","Your coords are :" & "Top Left Corner X:" & $iX1 & ", Y:" & $iY1 & ",  Bottom Right Corner X:" & $iX2 & ", Y:" & $iY2)
      input()
            EndSwitch
WEnd
Func input()
    $ib = InputBox("Hex","Type in the Hex number of the color you wish to find. Example: '0xFFFFFF' for white or 'FFFFFF' for white")
    $ibr = GUICtrlRead($ib)
 MsgBox(4096,"HotKey","Press F11 to start")
 HotKeySet("{F11}","findpix")
 EndFunc
 
 Func color()
   $mousepos = MouseGetPos()
   $pixcolor = PixelGetColor($mousepos[0],$mousepos[1])
   MsgBox(4096,"Color Hex","Your Color Hex is : " & "0x"&Hex($pixcolor,6))
   GUIDelete($color)
EndFunc

func findpix()
   $location = PixelSearch ($iX1,$iY1,$iX2,$iY2, Number(GUICtrlRead($ibr)))
   If IsArray($location) = True Then
    MouseMove($location[0],$location[1], 5)
    EndIf
   EndFunc
; -------------

Func Mark_Rect()

    Local $aMouse_Pos, $hMask, $hMaster_Mask, $iTemp
    Local $UserDLL = DllOpen("user32.dll")

    Global $hRectangle_GUI = GUICreate("", @DesktopWidth, @DesktopHeight, 0, 0, $WS_POPUP, $WS_EX_TOOLWINDOW + $WS_EX_TOPMOST)
    _GUICreateInvRect($hRectangle_GUI, 0, 0, 1, 1)
    GUISetBkColor(0)
    WinSetTrans($hRectangle_GUI, "", 50)
    GUISetState(@SW_SHOW, $hRectangle_GUI)
    GUISetCursor(3, 1, $hRectangle_GUI)

    ; Wait until mouse button pressed
    While Not _IsPressed("01", $UserDLL)
        Sleep(10)
    WEnd

    ; Get first mouse position
    $aMouse_Pos = MouseGetPos()
    $iX1 = $aMouse_Pos[0]
    $iY1 = $aMouse_Pos[1]

    ; Draw rectangle while mouse button pressed
    While _IsPressed("01", $UserDLL)

        $aMouse_Pos = MouseGetPos()

        ; Set in correct order if required
        If $aMouse_Pos[0] < $iX1 Then
            $iX_Pos = $aMouse_Pos[0]
            $iWidth = $iX1 - $aMouse_Pos[0]
        Else
            $iX_Pos = $iX1
            $iWidth = $aMouse_Pos[0] - $iX1
        EndIf
        If $aMouse_Pos[1] < $iY1 Then
            $iY_Pos = $aMouse_Pos[1]
            $iHeight = $iY1 - $aMouse_Pos[1]
        Else
            $iY_Pos = $iY1
            $iHeight = $aMouse_Pos[1] - $iY1
        EndIf

        _GUICreateInvRect($hRectangle_GUI, $iX_Pos, $iY_Pos, $iWidth, $iHeight)

        Sleep(10)

    WEnd

    ; Get second mouse position
    $iX2 = $aMouse_Pos[0]
    $iY2 = $aMouse_Pos[1]

    ; Set in correct order if required
    If $iX2 < $iX1 Then
        $iTemp = $iX1
        $iX1 = $iX2
        $iX2 = $iTemp
    EndIf
    If $iY2 < $iY1 Then
        $iTemp = $iY1
        $iY1 = $iY2
        $iY2 = $iTemp
    EndIf

    GUIDelete($hRectangle_GUI)
    DllClose($UserDLL)

EndFunc   ;==>Mark_Rect

Func _GUICreateInvRect($hWnd, $iX, $iY, $iW, $iH)

    $hMask_1 = _WinAPI_CreateRectRgn(0, 0, @DesktopWidth, $iY)
    $hMask_2 = _WinAPI_CreateRectRgn(0, 0, $iX, @DesktopHeight)
    $hMask_3 = _WinAPI_CreateRectRgn($iX + $iW, 0, @DesktopWidth, @DesktopHeight)
    $hMask_4 = _WinAPI_CreateRectRgn(0, $iY + $iH, @DesktopWidth, @DesktopHeight)

    _WinAPI_CombineRgn($hMask_1, $hMask_1, $hMask_2, 2)
    _WinAPI_CombineRgn($hMask_1, $hMask_1, $hMask_3, 2)
    _WinAPI_CombineRgn($hMask_1, $hMask_1, $hMask_4, 2)

    _WinAPI_DeleteObject($hMask_2)
    _WinAPI_DeleteObject($hMask_3)
    _WinAPI_DeleteObject($hMask_4)

    _WinAPI_SetWindowRgn($hWnd, $hMask_1, 1)

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