Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

Drawing a rectangle on screen


  • Please log in to reply
7 replies to this topic
oyy5408
  • Members
  • 134 posts
  • Last active: Feb 05 2016 05:10 PM
  • Joined: 29 Jun 2009
OnMessage(0x14, "WM_ERASEBKGND")
Gui, -Caption +ToolWindow
Gui, +LastFound
WinSet, TransColor, Black
; Create the pen here so we don't need to create/delete it every time.
RedPen := DllCall("CreatePen", "int", PS_SOLID:=0, "int", 5, "uint", 0xff)
return

WM_ERASEBKGND(wParam, lParam)
{
    global x1, y1, x2, y2, RedPen
    Critical 50
    if A_Gui = 1
    {
        ; Retrieve stock brush.
        blackBrush := DllCall("GetStockObject", "int", BLACK_BRUSH:=0x4)
        ; Select pen and brush.
        oldPen := DllCall("SelectObject", "uint", wParam, "uint", RedPen)
        oldBrush := DllCall("SelectObject", "uint", wParam, "uint", blackBrush)
        ; Draw rectangle.
        DllCall("Rectangle", "uint", wParam, "int", 0, "int", 0, "int", x2-x1, "int", y2-y1)
        ; Reselect original pen and brush (recommended by MS).
        DllCall("SelectObject", "uint", wParam, "uint", oldPen)
        DllCall("SelectObject", "uint", wParam, "uint", oldBrush)
        return 1
    }
}

+LButton::
[color=#FF0000]	coordmode, mouse, screen[/color]
    MouseGetPos, xorigin, yorigin
;	msgbox, %xorigin%, %yorigin%
    SetTimer, rectangle, 10
return

rectangle:
[color=#FF0000]	coordmode, mouse, screen[/color]
    MouseGetPos, x2, y2
;	msgbox, %xorigin%, %yorigin%
    ; Has the mouse moved?
    if (x1 y1) = (x2 y2)
        return
   
    ; Allow dragging to the left of the click point.
    if (x2 < xorigin) {
        x1 := x2
        x2 := xorigin
    } else
        x1 := xorigin
   
    ; Allow dragging above the click point.
    if (y2 < yorigin) {
        y1 := y2
        y2 := yorigin
    } else
        y1 := yorigin
   
    Gui, Show, % "NA X" x1 " Y" y1 " W" x2-x1 " H" y2-y1
    Gui, +LastFound
    DllCall("RedrawWindow", "uint", WinExist(), "uint", 0, "uint", 0, "uint", 5)
return

+LButton Up::
    SetTimer, rectangle, Off
    Gui, Cancel
[color=#FF0000]	coordmode, mouse, window[/color]
return

Hi,

I would like to draw a rectangle over a certain portion of a window which will let user see in which area he/she is going to do the imagesearch.
So, I have done everything but the drawing a rectangle part. I found a similar post but this is closest so far. url]http://www.autohotkey.com/community/viewtopic.php?t=31969[/url]

What Lexikos is just phenomenal, but it was starting to draw rectangle from different origin. So, I added coordmode lines in the code to match the mouse cursor better. (indicated in red) However, I still can't get the rectangle to be on top of everywindow in the screen. I basically want the rectangle the user will be drawing stays at top-most layer.

Pleaes advise me how this could work.

Thank you everyone for the support.

Pulover
  • Members
  • 1596 posts
  • Last active: Apr 06 2016 04:00 AM
  • Joined: 20 Apr 2012
I think this should do:
Gui, +LastFound [color=#FF0000]+AlwaysOnTop[/color]
This is very good. I think I will use it in my app too. The one I made cannot draw right-up. :lol:

Rodolfo U. Batista
Pulover's Macro Creator - Automation Tool (Recorder & Script Writer) | Class_LV_Rows - Copy, Cut, Paste and Drag ListViews | Class_Toolbar - Create and modify | Class_Rebar - Adjustable GUI controls

Join the New AutoHotkey Forum!


oyy5408
  • Members
  • 134 posts
  • Last active: Feb 05 2016 05:10 PM
  • Joined: 29 Jun 2009
Thanks! That works!

I have one more question though:
How would I be able to get the coordinates of the box? I tried with a_guiheight/width, a_guix/y but no variables were found.

Pulover
  • Members
  • 1596 posts
  • Last active: Apr 06 2016 04:00 AM
  • Joined: 20 Apr 2012
Those variables are for specific events. Use WinGetPos instead.
OnMessage(0x14, "WM_ERASEBKGND")
Gui, -Caption +ToolWindow
Gui, +LastFound
WinSet, TransColor, Black
; Create the pen here so we don't need to create/delete it every time.
RedPen := DllCall("CreatePen", "int", PS_SOLID:=0, "int", 5, "uint", 0xff)
return

WM_ERASEBKGND(wParam, lParam)
{
    global x1, y1, x2, y2, RedPen
    Critical 50
    if A_Gui = 1
    {
        ; Retrieve stock brush.
        blackBrush := DllCall("GetStockObject", "int", BLACK_BRUSH:=0x4)
        ; Select pen and brush.
        oldPen := DllCall("SelectObject", "uint", wParam, "uint", RedPen)
        oldBrush := DllCall("SelectObject", "uint", wParam, "uint", blackBrush)
        ; Draw rectangle.
        DllCall("Rectangle", "uint", wParam, "int", 0, "int", 0, "int", x2-x1, "int", y2-y1)
        ; Reselect original pen and brush (recommended by MS).
        DllCall("SelectObject", "uint", wParam, "uint", oldPen)
        DllCall("SelectObject", "uint", wParam, "uint", oldBrush)
        return 1
    }
}

+LButton::
   coordmode, mouse, screen
    MouseGetPos, xorigin, yorigin
;   msgbox, %xorigin%, %yorigin%
    SetTimer, rectangle, 10
return

rectangle:
   coordmode, mouse, screen
    MouseGetPos, x2, y2
;   msgbox, %xorigin%, %yorigin%
    ; Has the mouse moved?
    if (x1 y1) = (x2 y2)
        return
   
    ; Allow dragging to the left of the click point.
    if (x2 < xorigin) {
        x1 := x2
        x2 := xorigin
    } else
        x1 := xorigin
   
    ; Allow dragging above the click point.
    if (y2 < yorigin) {
        y1 := y2
        y2 := yorigin
    } else
        y1 := yorigin
   
    Gui, Show, % "NA X" x1 " Y" y1 " W" x2-x1 " H" y2-y1
    Gui, +LastFound +AlwaysOnTop
	WinGetPos, X, Y, W, H
	Tooltip % "X: " X ", Y: " Y "`nWidth: " W ", Height: " H
    DllCall("RedrawWindow", "uint", WinExist(), "uint", 0, "uint", 0, "uint", 5)
return

+LButton Up::
    SetTimer, rectangle, Off
    Gui, Cancel
   coordmode, mouse, window
   tooltip
   msgbox % "X: " X ", Y: " Y "`nWidth: " W ", Height: " H
return

Rodolfo U. Batista
Pulover's Macro Creator - Automation Tool (Recorder & Script Writer) | Class_LV_Rows - Copy, Cut, Paste and Drag ListViews | Class_Toolbar - Create and modify | Class_Rebar - Adjustable GUI controls

Join the New AutoHotkey Forum!


oyy5408
  • Members
  • 134 posts
  • Last active: Feb 05 2016 05:10 PM
  • Joined: 29 Jun 2009
Thank you, that's just perfect.
If anyone wants to keep the rectangle visible on the screen, remove "Gui, Cancel" under +LButton Up:: or simply add a semicolon in front of it.

oyy5408
  • Members
  • 134 posts
  • Last active: Feb 05 2016 05:10 PM
  • Joined: 29 Jun 2009
Ok, I have further questions now. In order for me to apply this script to my condition, I have to initiate it only when certain hot key is pressed.

For example:
OnMessage(0x14, "WM_ERASEBKGND")
Gui, -Caption +ToolWindow
Gui, +LastFound
WinSet, TransColor, Black
; Create the pen here so we don't need to create/delete it every time.
RedPen := DllCall("CreatePen", "int", PS_SOLID:=0, "int", 5, "uint", 0xff)
return

WM_ERASEBKGND(wParam, lParam)
{
    global x1, y1, x2, y2, RedPen
    Critical 50
    if A_Gui = 1
    {
        ; Retrieve stock brush.
        blackBrush := DllCall("GetStockObject", "int", BLACK_BRUSH:=0x4)
        ; Select pen and brush.
        oldPen := DllCall("SelectObject", "uint", wParam, "uint", RedPen)
        oldBrush := DllCall("SelectObject", "uint", wParam, "uint", blackBrush)
        ; Draw rectangle.
        DllCall("Rectangle", "uint", wParam, "int", 0, "int", 0, "int", x2-x1, "int", y2-y1)
        ; Reselect original pen and brush (recommended by MS).
        DllCall("SelectObject", "uint", wParam, "uint", oldPen)
        DllCall("SelectObject", "uint", wParam, "uint", oldBrush)
        return 1
    }
}

[color=#FF0000]f8::
msgbox, Press "ESC" key to exit search mode
Loop,
{
	if getkeystate("LButton", "P")=1
	{
		coordmode, mouse, screen,
		mousegetpos, xorigin, yorigin
		settimer, rectangle, 10
	}
	if getkeystate("ESC", "P")=1
		break
}
return

;+LButton::
;   coordmode, mouse, screen
;    MouseGetPos, xorigin, yorigin
;    SetTimer, rectangle, 10
;return
[/color]

rectangle:
   coordmode, mouse, screen
    MouseGetPos, x2, y2
;   msgbox, %xorigin%, %yorigin%
    ; Has the mouse moved?
    if (x1 y1) = (x2 y2)
        return
   
    ; Allow dragging to the left of the click point.
    if (x2 < xorigin) {
        x1 := x2
        x2 := xorigin
    } else
        x1 := xorigin
   
    ; Allow dragging above the click point.
    if (y2 < yorigin) {
        y1 := y2
        y2 := yorigin
    } else
        y1 := yorigin
   
    Gui, Show, % "NA X" x1 " Y" y1 " W" x2-x1 " H" y2-y1
    Gui, +LastFound +AlwaysOnTop
   WinGetPos, recX, recY, recW, recH
   ;Tooltip % "X: " X ", Y: " Y "`nWidth: " W ", Height: " H
    DllCall("RedrawWindow", "uint", WinExist(), "uint", 0, "uint", 0, "uint", 5)
return

+LButton Up::
    SetTimer, rectangle, Off
    ;Gui, Cancel
   coordmode, mouse, window
   tooltip
   msgbox % "X: " recX ", Y: " recY "`nWidth: " recW ", Height: " recH
return

Highlighted portion is what I have changed but doesn't work as I want it to.

just me
  • Members
  • 1496 posts
  • Last active: Nov 03 2015 04:32 PM
  • Joined: 28 May 2011
F8::
   If GetKeyState("LButton", "P") {
      CoordMode, Mouse, Screen
      MouseGetPos, OriginX, OriginY
      SetTimer, Rectangle, 10
      While GetKeyState("LButton", "P")
         Sleep, 10
      SetTimer, Rectangle, Off
      ; Gui, Cancel
      CoordMode, Mouse, Window
      ToolTip
      MsgBox % "X: " recX ", Y: " recY "`nWidth: " recW ", Height: " recH
   }
Return
?

Edit: I still prefer the WinSet, Region method:
; Should work with all current AHK versions/builds
#NoEnv
SetBatchLines, -1 ; For speed in general
SetWinDelay, -1   ; For speed of WinMove
BW := 3           ; Border width (and height) in pixels
BC := "Red"       ; Border color
; ______________________________________________________________________________________________________________________
FirstCall := True
CoordMode, Mouse, Screen
Gui, -Caption +ToolWindow +LastFound +AlwaysOnTop
Gui, Color, %BC%
Return
; ______________________________________________________________________________________________________________________
Esc::
ExitApp
; ______________________________________________________________________________________________________________________
+LButton::
    MouseGetPos, OriginX, OriginY
    SetTimer, DrawRectangle, 10
Return
; ______________________________________________________________________________________________________________________
+LButton Up::
   SetTimer, DrawRectangle, Off
   FirstCall := True
   Gui, Cancel
   ToolTip
   MsgBox, 0, Coordinates, X = %X1%  -  Y = %Y1%  -  W = %W1%  -  H = %H1%
Return
; ______________________________________________________________________________________________________________________
DrawRectangle:
   MouseGetPos, X2, Y2
   ; Has the mouse moved?
   If (XO = X2) And (YO = Y2)
      Return
   Gui, +LastFound
   XO := X2, YO := Y2
   ; Allow dragging to the left of the click point.
   If (X2 < OriginX)
      X1 := X2, X2 := OriginX
   Else
      X1 := OriginX
   ; Allow dragging above the click point.
   If (Y2 < OriginY)
      Y1 := Y2, Y2 := OriginY
   Else
      Y1 := OriginY
   ; Draw the rectangle
   W1 := X2 - X1, H1 := Y2 - Y1
   W2 := W1 - BW, H2 := H1 - BW
   WinSet, Region, 0-0 %W1%-0 %W1%-%H1% 0-%H1% 0-0  %BW%-%BW% %W2%-%BW% %W2%-%H2% %BW%-%H2% %BW%-%BW%
   If (FirstCall) {
      Gui, Show, NA x%X1% y%Y1% w%W1% h%H1%
      FirstCall := False
   }
   WinMove, , , X1, Y1, W1, H1
   ; ToolTip, %X1% - %Y1% - %X2% - %Y2%
Return

Prefer ahkscript.org for the time being.


  • Guests
  • Last active:
  • Joined: --
Thank you, that works too.
I added few hotkeys "Hotkey," commands to control +LButton command being triggered.