Jump to content

Marking Rectangle by window position


Miliardsto
 Share

Recommended Posts

Hello . I implemented script like this

But the vars $iX1 $iY1 are counting from the desktop start and I want it coords by window

This not work with this script Opt("MouseCoordMode", 0)

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

    ; Create transparent GUI with Cross cursor
    $hCross_GUI = GUICreate("Test", @DesktopWidth, @DesktopHeight - 20, 0, 0, $WS_POPUP, $WS_EX_TOPMOST)
    WinSetTrans($hCross_GUI, "", 8)
    GUISetState(@SW_SHOW, $hCross_GUI)
    GUISetCursor(3, 1, $hCross_GUI)

    Global $hRectangle_GUI = GUICreate("", @DesktopWidth, @DesktopHeight, 0, 0, $WS_POPUP, $WS_EX_TOOLWINDOW + $WS_EX_TOPMOST)
    GUISetBkColor(0x00FF00)

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

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

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

        $aMouse_Pos = MouseGetPos()

        $hMaster_Mask = _WinAPI_CreateRectRgn(0, 0, 0, 0)
        $hMask = _WinAPI_CreateRectRgn($iX1,  $aMouse_Pos[1], $aMouse_Pos[0],  $aMouse_Pos[1] + 1) ; Bottom of rectangle
        _WinAPI_CombineRgn($hMaster_Mask, $hMask, $hMaster_Mask, 2)
        _WinAPI_DeleteObject($hMask)
        $hMask = _WinAPI_CreateRectRgn($iX1, $iY1, $iX1 + 1, $aMouse_Pos[1]) ; Left of rectangle
        _WinAPI_CombineRgn($hMaster_Mask, $hMask, $hMaster_Mask, 2)
        _WinAPI_DeleteObject($hMask)
        $hMask = _WinAPI_CreateRectRgn($iX1 + 1, $iY1 + 1, $aMouse_Pos[0], $iY1) ; Top of rectangle
        _WinAPI_CombineRgn($hMaster_Mask, $hMask, $hMaster_Mask, 2)
        _WinAPI_DeleteObject($hMask)
        $hMask = _WinAPI_CreateRectRgn($aMouse_Pos[0], $iY1, $aMouse_Pos[0] + 1,  $aMouse_Pos[1]) ; Right of rectangle
        _WinAPI_CombineRgn($hMaster_Mask, $hMask, $hMaster_Mask, 2)
        _WinAPI_DeleteObject($hMask)
        ; Set overall region
        _WinAPI_SetWindowRgn($hRectangle_GUI, $hMaster_Mask)

        If WinGetState($hRectangle_GUI) < 15 Then GUISetState()
        Sleep(10)

        ; Get second mouse position
    global $iX2 = Round($aMouse_Pos[0], 2)
    global $iY2 = Round($aMouse_Pos[1], 2)

    ; 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

    If _IsPressed("01", $UserDLL) = 0 Then
        _GDIPlus_Startup ()
        $hDC = _WinAPI_GetWindowDC(0)
        $hGraphic = _GDIPlus_GraphicsCreateFromHDC($hDC)
        ;Stworz pedzel i kolor, width
        ;$hPen = _GDIPlus_PenCreate(0x00FF00)
        ; Rysowanie linii
        ;_GDIPlus_GraphicsDrawLine($hGraphic,$iX1,$iY1,$iX2,$iY2,$hPen)

        ; middle of rectangle
        $xMiddle = ( $iX1 + $iX2 ) / 2
        $yMiddle = ( $iY1 + $iY2 ) / 2

        _GDIPlus_GraphicsDrawRect( $hGraphic, $xMiddle-20, $yMiddle-20, 40, 40)
    EndIf

    WEnd


    GUIDelete($hRectangle_GUI)
    GUIDelete($hCross_GUI)
    DllClose($UserDLL)


EndFunc   ;==>Mark_Rect

 

Link to comment
Share on other sites

Both work for me also long as the window is active:

 

#NoTrayIcon
#include <EditConstants.au3>
#include <FontConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <StringConstants.au3>
#include <WindowsConstants.au3>

Opt("MouseCoordMode", 0) ; Per Window
; Opt("MouseCoordMode", 2) ; Per Ctrl

Main()

Func Main()

    Local $hGUI = GUICreate("Test Window", 280, 75, @DesktopWidth - 300, 10, BitXOR($GUI_SS_DEFAULT_GUI, $WS_MINIMIZEBOX), $WS_EX_TOOLWINDOW + $WS_EX_TOPMOST)
    Local $hOutput = GUICtrlCreateLabel("", 10, 10, 260, 65, $SS_SUNKEN + $SS_CENTER + $SS_CENTERIMAGE)
    GUICtrlSetFont($hOutput, 14, $FW_SEMIBOLD)
    GUISetState(@SW_SHOW, $hGUI)

    WinActivate("Test Window")

    Local $aMouse[2]
    While 1
        $hMsg = GUIGetMsg()
        $aMouse = MouseGetPos()
        GUICtrlSetData($hOutput, $aMouse[0] & ", " & $aMouse[1])
        Select
            Case $hMsg = $GUI_EVENT_CLOSE
                ExitLoop
        EndSelect
    WEnd

    GUIDelete($hGUI)

EndFunc

 

Edited by rcmaehl

My UDFs are generally for me. If they aren't updated for a while, it means I'm not using them myself. As soon as I start using them again, they'll get updated.

My Projects

WhyNotWin11
Cisco FinesseGithubIRC UDFWindowEx UDF

 

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

×
×
  • Create New...