Jump to content

Draw simple black rectangle on screen with mouse selection a keep it on top (above all) with hotkey to hide/show


Radim
 Share

Recommended Posts

Hi,

I am totaly new to AutoIt. I need cover frequently a part of screen with solid color rectangle.

I found this script that select part of screen with mouse: 

 

now I need to draw black rectangle (no window border, or title) on that place with posibility to hide a show it back with some hotkey as (for example) Ctrl+Alt+B .

This rectangle box has to be still on top, above all windows. Please help, thanks.

Link to comment
Share on other sites

Link to comment
Share on other sites

You are lucky because I have created exactly such application for myself some time ago :-)

 

Code and INI names are in Czech language, use translator ...

Instead of Hotkeys for show/hide it uses Tray icon -> doubleclick=show/hide

 

Frame2.au3

; 3.2.12.1
#AutoIt3Wrapper_Icon=obdelnik3.ico
#AutoIt3Wrapper_Res_Language=1029
#AutoIt3Wrapper_Res_Description=Obdélník2
#AutoIt3Wrapper_Res_Comment=Zobrazení vyplněného obdélníku
#AutoIt3Wrapper_Res_FileVersion=1.0
#AutoIt3Wrapper_Res_LegalCopyright=Copyright © Petr Zedník 2021
;~ #AutoIt3Wrapper_Res_Field=Verze AutoIt|%AutoItVer%
://////=__=
#AutoIt3Wrapper_run_obfuscator=y
#Obfuscator_parameters=/SF /SV /CV=1 /CF=1 /CS=0 /CN=0 /OM
#AutoIt3Wrapper_UseX64=n
;~ #AutoIt3Wrapper_UseUpx=n

#include <Constants.au3>
#include <WindowsConstants.au3>
#include <GUIConstantsEx.au3>
#include <ButtonConstants.au3>

Opt("GUIOnEventMode",1)
Opt("TrayAutoPause",0)
Opt("TrayMenuMode",1) ; no default menu (Paused/Exit)

;~ Global $akt_rozliseni
Global $user32 = DllOpen("user32.dll")

If @Compiled Then
    $ico = @ScriptName
    $ico_id = 0
Else
    $ico = "obdelnik3.ico"
    $ico_id = -1
EndIf

$pripona = StringRight(@ScriptName, 4) ; .exe nebo .au3
$ini = StringReplace(@ScriptName,$pripona,'.ini')

$iniZobrazitObdelnik = IniRead($ini, "Nastaveni", "ZobrazitObdelnik", "1")
$visible = $iniZobrazitObdelnik
;~ $iniRozliseni = IniRead($ini, "Nastaveni", "Rozliseni", "800x600")
;~ If Not StringInStr($iniRozliseni, 'x') Then $iniRozliseni = '800x600'
$iniBarva = IniRead($ini, "Nastaveni", "Barva", "0x000000")
$PoziceX = IniRead($ini, "Nastaveni", "PoziceX", "0")
$PoziceY = IniRead($ini, "Nastaveni", "PoziceY", "0")
$iniSirka = IniRead($ini, "Nastaveni", "Sirka", "100")
If $iniSirka == '' Then $iniSirka = 100
$Sirka = $iniSirka
$iniVyska = IniRead($ini, "Nastaveni", "Vyska", "100")
If $iniVyska == '' Then $iniVyska = 100
$Vyska = $iniVyska

$ZobrazitItem = TrayCreateItem("Zobrazit/Skrýt")
TrayItemSetState($ZobrazitItem,$TRAY_DEFAULT)
;~ TrayCreateItem("")
;~ $rozliseni = IniReadSection($ini, "Rozliseni")
;~ If @error Then
;~  Dim $rozliseni[1][1]
;~  $rozliseni[0][0] = 0
;~ Else
;~     For $i = 1 To $rozliseni[0][0]
;~         $rozliseni[$i][0] = TrayCreateItem(StringReplace($rozliseni[$i][1],'x',' x '),-1,-1,1)
;~      If $rozliseni[$i][1] = $iniRozliseni Then TrayItemSetState($rozliseni[$i][0], $TRAY_CHECKED)
;~     Next
;~ EndIf
TrayCreateItem("")
;~ $NastaveniItem = TrayCreateItem("Nastavení")
;~ TrayItemSetState($NastaveniItem ,$TRAY_DISABLE)
$OAplikaciItem = TrayCreateItem("O aplikaci")
TrayCreateItem("")
$KonecItem = TrayCreateItem("Konec")
TraySetIcon($ico)
TraySetState()
TraySetClick(8) ; Pressing secondary mouse button

; pozn: skutecny rozmer se nastavi az v ZmenaRozliseni()
$gui = GUICreate("Obdélník2", $Sirka, $Vyska, $PoziceX, $PoziceY, BitOR($WS_POPUP,$WS_SIZEBOX), BitOR($WS_EX_TOPMOST,$WS_EX_TOOLWINDOW))
GUISetOnEvent ($GUI_EVENT_PRIMARYDOWN, "Drag" )
GUISetBkColor($iniBarva)
;~ $pom = StringSplit($iniRozliseni,'x')
;~ ZmenaRozliseni($pom[1], $pom[2])
;~ ZmenaRozliseni($Sirka, $Vyska)
TraySetToolTip('Obdélník2 ' & $Sirka & ' x ' & $Vyska)
If $iniZobrazitObdelnik = '1' Then GUISetState(@SW_SHOW, $gui)

$about = GuiCreate("O aplikaci",210,150,-1,-1,BitOR($WS_CAPTION,$WS_SYSMENU),-1,$gui) ;$WS_EX_TOOLWINDOW
GUISetIcon($ico, $ico_id, $about)
GUISetOnEvent ($GUI_EVENT_CLOSE, "OAplikaciOK" )
GUICtrlCreateIcon ($ico,$ico_id,11,11)
GUICtrlCreateLabel ("Obdélník2 1.0",59,11,135,20)
GUICtrlSetFont (-1,10, 800, 0, "Arial") ; bold
GUICtrlCreateLabel ("(c) 2021" & @CRLF & @CRLF & "Petr Zedník",59,30,135,40)
$email = GUICtrlCreateLabel ("petr.zednik@volny.cz",59,70,135,15)
GuiCtrlSetFont($email, 8.5, -1, 4) ; underlined
GuiCtrlSetColor($email,0x0000ff)
GuiCtrlSetCursor($email,0)
GUICtrlSetOnEvent(-1, "OAplikaciEmail")
$www = GUICtrlCreateLabel ("www.volny.cz/petr.zednik/",59,85,135,15)
GuiCtrlSetFont($www, 8.5, -1, 4) ; underlined
GuiCtrlSetColor($www,0x0000ff)
GuiCtrlSetCursor($www,0)
GUICtrlSetOnEvent(-1, "OAplikaciWWW")
GUICtrlCreateButton ("OK",65,115,75,23,BitOr($GUI_SS_DEFAULT_BUTTON, $BS_DEFPUSHBUTTON))
GUICtrlSetState (-1, $GUI_FOCUS)
GUICtrlSetOnEvent(-1, "OAplikaciOK")

While 1
    $msg = TrayGetMsg()
    Select
        Case $msg = $ZobrazitItem
            Zobrazit()
;~         Case $msg = $NastaveniItem
;~             Nastaveni()
        Case $msg = $OAplikaciItem
            OAplikaci()
        Case $msg = $KonecItem
            Ukonceni()
    EndSelect

;~     For $i = 1 To $rozliseni[0][0]
;~         If $msg = $rozliseni[$i][0] Then
;~          $pom = StringSplit($rozliseni[$i][1],'x',1)
;~          ZmenaRozliseni($pom[1],$pom[2])
;~      EndIf
;~     Next
WEnd

Exit

Func Zobrazit()
    MouseUp("primary") ; osetreni chyby pri doubleclick na default tray ikone & Drag

    $visible = Not $visible

    If $visible Then
        GUISetState(@SW_SHOW, $gui)
    Else
        GUISetState(@SW_HIDE, $gui)
    EndIf
EndFunc

;~ Func ZmenaRozliseni($pSirka, $pVyska)
;~  TraySetToolTip('Obdélník2 ' & $pSirka & ' x ' & $pVyska)
;~  $akt_rozliseni = $pSirka & 'x' & $pVyska

;~  WinMove($gui, "", Default, Default, $pSirka, $pVyska)
;~ EndFunc

;~ Func Nastaveni()
;~ EndFunc

Func OAplikaci()
    GUISetState(@SW_SHOW, $about)
EndFunc

Func OAplikaciOK()
    GUISetState(@SW_HIDE, $about)
EndFunc

Func OAplikaciEmail()
    Run(@ComSpec & " /c " & 'start mailto:petr.zednik@volny.cz?subject=Obdelnik2', "", @SW_HIDE)
EndFunc

Func OAplikaciWWW()
    Run(@ComSpec & " /c " & 'start www.volny.cz/petr.zednik/', "", @SW_HIDE)
EndFunc

Func Ukonceni()
    ; pokud se zmenila poloha okna, tak to zapsat do INI souboru
    ; pozn: toto nemuze byt v OnAutoItExit, protoze v tom okamziku uz je okno zavrene
    $pos = WinGetPos($gui)
    If Not @error  Then
        If $PoziceX <> $pos[0] Then IniWrite($ini, "Nastaveni", "PoziceX", $pos[0])
        If $PoziceY <> $pos[1] Then IniWrite($ini, "Nastaveni", "PoziceY", $pos[1])
    EndIf
    $cpos = WinGetClientSize($gui)
    If Not @error  Then
        If $iniSirka <> $cpos[0] Then IniWrite($ini, "Nastaveni", "Sirka", $cpos[0])
        If $iniVyska <> $cpos[1] Then IniWrite($ini, "Nastaveni", "Vyska", $cpos[1])
    EndIf

;~  If $akt_rozliseni <> $iniRozliseni Then IniWrite($ini, "Nastaveni", "Rozliseni", $akt_rozliseni)

    DllClose($user32)
    Exit
EndFunc

Func Drag()
    DllCall($user32,"int","SendMessage","hWnd", $gui,"int",$WM_NCLBUTTONDOWN,"int", $HTCAPTION,"int", 0)
EndFunc

It's based on my previous older project Frame which has rectangle with hole inside based on common (configurable) resolutions.

 

 

Obdelnik3.ico

Edited by Zedna
Link to comment
Share on other sites

Here is cleaned and simplified previous code, removed unneccesary Wrapper directives + comments + "About dialog" related code:

EDIT: + added some English comments

Frame2_simple.au3

; 3.2.12.1
#AutoIt3Wrapper_Icon=obdelnik3.ico
#AutoIt3Wrapper_UseX64=n

#include <Constants.au3>
#include <WindowsConstants.au3>
#include <GUIConstantsEx.au3>
#include <ButtonConstants.au3>

Opt("GUIOnEventMode",1)
Opt("TrayAutoPause",0)
Opt("TrayMenuMode",1) ; no default menu (Paused/Exit)

Global $user32 = DllOpen("user32.dll")

If @Compiled Then
    $ico = @ScriptName
    $ico_id = 0
Else
    $ico = "obdelnik3.ico"
    $ico_id = -1
EndIf

$pripona = StringRight(@ScriptName, 4) ; extension: .exe or .au3
$ini = StringReplace(@ScriptName,$pripona,'.ini')

$iniZobrazitObdelnik = IniRead($ini, "Nastaveni", "ZobrazitObdelnik", "1") ; show frame
$visible = $iniZobrazitObdelnik
$iniBarva = IniRead($ini, "Nastaveni", "Barva", "0x000000") ; color
$PoziceX = IniRead($ini, "Nastaveni", "PoziceX", "0") ; X position
$PoziceY = IniRead($ini, "Nastaveni", "PoziceY", "0") ; Y position
$iniSirka = IniRead($ini, "Nastaveni", "Sirka", "100") ; width
If $iniSirka == '' Then $iniSirka = 100
$Sirka = $iniSirka
$iniVyska = IniRead($ini, "Nastaveni", "Vyska", "100") ; height
If $iniVyska == '' Then $iniVyska = 100
$Vyska = $iniVyska

$ZobrazitItem = TrayCreateItem("Zobrazit/Skrýt") ; show/hide
TrayItemSetState($ZobrazitItem,$TRAY_DEFAULT)
TrayCreateItem("")
$KonecItem = TrayCreateItem("Konec") ; quit
TraySetIcon($ico)
TraySetState()
TraySetClick(8) ; Pressing secondary mouse button

$gui = GUICreate("Obdélník2", $Sirka, $Vyska, $PoziceX, $PoziceY, BitOR($WS_POPUP,$WS_SIZEBOX), BitOR($WS_EX_TOPMOST,$WS_EX_TOOLWINDOW)) ; Frame2
GUISetOnEvent ($GUI_EVENT_PRIMARYDOWN, "Drag" )
GUISetBkColor($iniBarva)
TraySetToolTip('Obdélník2 ' & $Sirka & ' x ' & $Vyska) ; Frame2 width x height
If $iniZobrazitObdelnik = '1' Then GUISetState(@SW_SHOW, $gui) ; show frame

While 1
    $msg = TrayGetMsg()
    Select
        Case $msg = $ZobrazitItem
            Zobrazit() ; show
        Case $msg = $KonecItem
            Ukonceni() ; quit
    EndSelect
WEnd

Func Zobrazit() ; show
    MouseUp("primary") ; osetreni chyby pri doubleclick na default tray ikone & Drag

    $visible = Not $visible

    If $visible Then
        GUISetState(@SW_SHOW, $gui)
    Else
        GUISetState(@SW_HIDE, $gui)
    EndIf
EndFunc

Func Ukonceni() ; quit
    ; pokud se zmenila poloha okna, tak to zapsat do INI souboru
    ; pozn: toto nemuze byt v OnAutoItExit, protoze v tom okamziku uz je okno zavrene
    $pos = WinGetPos($gui)
    If Not @error  Then
        If $PoziceX <> $pos[0] Then IniWrite($ini, "Nastaveni", "PoziceX", $pos[0]) ; X position
        If $PoziceY <> $pos[1] Then IniWrite($ini, "Nastaveni", "PoziceY", $pos[1]) ; Y position
    EndIf
    $cpos = WinGetClientSize($gui)
    If Not @error  Then
        If $iniSirka <> $cpos[0] Then IniWrite($ini, "Nastaveni", "Sirka", $cpos[0]) ; width
        If $iniVyska <> $cpos[1] Then IniWrite($ini, "Nastaveni", "Vyska", $cpos[1]) ; height
    EndIf

    DllClose($user32)
    Exit
EndFunc

Func Drag()
    DllCall($user32,"int","SendMessage","hWnd", $gui,"int",$WM_NCLBUTTONDOWN,"int", $HTCAPTION,"int", 0)
EndFunc

 

Edited by Zedna
Link to comment
Share on other sites

On 9/22/2022 at 5:33 PM, Radim said:

I need cover frequently a part of screen with solid color rectangle

Here is a very basic script that could help, the rectangle is draggable, resizable and stays on top of other windows. Esc is the key to close the script when the rectangle is selected (an Accelerator key has been used, which is better than a HotKey in this kind of script) :

#include <GuiconstantsEx.au3>
#include <WindowsConstants.au3>

Local $hGUI = GUICreate("", 100, 100, -1, -1, BitOr($WS_POPUP, $WS_SIZEBOX), $WS_EX_TOPMOST)
GUISetBkColor(0x000000)

Local $iLabel = GUICtrlCreateLabel("", 0, 0, 100, 100, -1, $GUI_WS_EX_PARENTDRAG)
Local $idDummy_Esc = GUICtrlCreateDummy()

Local $aAccelKeys[1][2] = [["{ESC}", $idDummy_Esc]]
GUISetAccelerators($aAccelKeys)
GUISetState()

While 1
    Switch GUIGetMsg()
        Case $idDummy_Esc
            GUIDelete()
            Exit
    EndSwitch
WEnd

Though the script doesn't cover all OP's requirements [ "hide and show it back with some hotkey as (for example) Ctrl+Alt+B ] I thought it could be useful for some users.
Good luck :)
 

Edited by pixelsearch
typo
Link to comment
Share on other sites

3 hours ago, pixelsearch said:

I thought it could be useful for some users.

@pixelsearchThank you. :)

Very simple and useful.

taurus905

"Never mistake kindness for weakness."-- Author Unknown --"The highest point to which a weak but experienced mind can rise is detecting the weakness of better men."-- Georg Lichtenberg --Simple Obfuscator (Beta not needed.), Random names for Vars and Funcs

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