CENSOR BOX

Post your working scripts, libraries and tools for AHK v1.1 and older
User avatar
SpeedMaster
Posts: 494
Joined: 12 Nov 2016, 16:09

CENSOR BOX

04 Jul 2020, 06:54

Hello,
Sometimes you just want to hide parts of the screen with a color... :cry: :think:
Here's an easy way to do it. 8-)
The script creates a resizable box in the center of the screen that you can move to hide parts of the screen. You can create as many boxes as you want by pressing the F1 key.

shortcuts::
F1 add a new box
F2 color the box with the top left pixel
F2 Long press (> 0.6 sec) highlight current box in red
F3 freeze/unfreeze all boxes
F4 remove selected box
F5 hide/show all boxes
F6 make current box semi-transparent or opaque
Shift + Left Button show x-cross
Shift Up (while Left Button down) start drawing rectangle
ESC exit script

All boxes are resizable with the mouse

Suggestions to improve the script are welcome
Cheers

Code: Select all

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn  ; Enable warnings to assist with detecting common errors.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.

; =================================================================================================================================
; Name:           CENSOR BOX 
; Description:    Hide parts of the screen with a colored box
; credits:        Speedmaster, Lexikos, flyingDman, rommmcek, MancioDellaVega
; Topic:          https://www.autohotkey.com/boards/viewtopic.php?f=6&t=78160
; Sript version:  1.4
; AHK Version:    1.1.24.03 (A32/U32/U64)
; Tested on:      Win 7 
; shortcuts::     F1 add a new box
;                 F2 color the box with the top left pixel
;                 F2 Long press (> 0.6 sec) highlight box in red
;                 F3 freeze/unfreeze all boxes
;                 F4 remove selected box
;                 F5 hide/show all boxes
;                 F6 make current box semi transparent
;                 Shift + Left Button show x-cross
;                 Shift Up (while LButton down) start drawing rectangle
;                 ESC exit script
;                 all boxes are resizable with the mouse

#SingleInstance force

OnMessage(0x201, "WM_LBUTTONDOWN")
OnMessage(0x84, "WM_NCHITTEST")
OnMessage(0x83, "WM_NCCALCSIZE")
OnMessage(0x86, "WM_NCACTIVATE")
CoordMode, pixel, screen
CoordMode, mouse, screen

BW := 2           				; Border width (and height) in pixels
BC := "FF8800"       			; Border color
FirstCall := True
Gui, -Caption  +LastFound +AlwaysOnTop
Gui, Color, %BC%

Gui, Cross1: -Caption +AlwaysOnTop 
Gui, Cross1: Color, % bc
Gui, Cross2: -Caption +AlwaysOnTop 
Gui, Cross2: Color, % bc
count:= 0
Return

+LButton::
While GetKeyState("Shift", "P") {
    MouseGetPos, OriginX, OriginY
    ;OriginX-=8, OriginY-=5
    Gui, Cross1: show, % "NA y0 w1 x" OriginX " h" A_ScreenHeight
    Gui, Cross2: show, % "NA x0 h1 y" OriginY " w" A_ScreenWidth
    Sleep, 10
}
Gui, Cross1: Hide
Gui, Cross2: Hide
if !GetKeyState("LButton", "P")
    Return
WinGetActiveStats, Title, WindowWidth, WindowHeight, WindowX, WindowY
SetTimer, DrawRectangle, 20
Return

DrawRectangle: ; thx flyingDman
MouseGetPos, X2, Y2
Gui, +LastFound

X2<OriginX? (X1:=X2, X2:=OriginX): X1:=OriginX, Y2<OriginY? (Y1:=Y2, Y2:=OriginY): Y1:=OriginY
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
}
else WinMove, , , X1, Y1, W1, H1
Return

+LButton Up::
SetTimer, DrawRectangle, Off
Gui, Cancel
FirstCall := True
if GetKeyState("Shift", "P")
    Return
newbox:
count++
Gui box_%count%: -caption -DPIScale +AlwaysOnTop +resize ;+LabelGui +LastFound
        Gui box_%count%: Margin, 0,0
Gui box_%count%: color, black
Gui box_%count%: show, % "NA x" X1 " y" Y1 " w" W1-16 " h" H1-16, box_%count% ; offset (-16) might be PC/OS specific
currentgui:="box_" count
return

f1::
w1:=100
h1:=100
x1:=a_screenwidth//2-w1//2
y1:=a_screenheight//2-h1//2
goto newbox
return

F2::
    transparency:=100  ; 0-255 level of transparency (0 =Transparent , 255=full visibile)

    KeyWait, F2, U T0.6    ; Long press (> 0.6 sec) 
    If ErrorLevel   
    {
        Gui,%currentgui%: +LastFound
        WinSet,Transparent,%transparency% ; Set Box to transparency
        Gui, %currentgui%:Color, FF0000
    }
	else
	{
        WinGetPos,px,py,,, %currentgui%
        PixelGetColor, outcolor, % px-1, % py-1, RGB
        Gui,%currentgui%: +LastFound
        WinSet,Transparent,255 ; Set Box to No transparency
        Gui %currentgui%: color, % outcolor
    }
    
    KeyWait, F2, U T0.6   ;Wait no more than 0.6 sec for key release (also suppress auto-repeat)
return


f3::
freeze:=!freeze
if freeze
    loop, % count
        gui box_%a_index%: -resize
else
    loop, % count
        gui box_%a_index%: +resize
return

f4::
gui %currentgui%: Destroy ; Thx MancioDellaVega
return

F5::
hide:=!hide
if hide
    loop, % count {
            Gui, box_%a_index%:Default
            gui box_%a_index% : -caption
            gui box_%a_index%: hide   
        }
else
    loop, % count {
            Gui, box_%a_index%:Default
            GuiGetPos( X,Y,W,H, "box_" a_index )
            gui box_%a_index%: show
            WinMove, box_%a_index%,,x,y,w,h
        }
return

f6::
        transparency:=100  ; 0-255 level of transparency (0 =Transparent , 255=full visibile)
trans:=!Trans
if trans 
    {
        Gui,%currentgui%: +LastFound
        WinSet,Transparent, %transparency% ; Set Box to transparency
    }
    else {
        Gui,%currentgui%: +LastFound
        WinSet,Transparent, 255
    }
return


WM_LBUTTONDOWN() {
    global currentgui, freeze
    MouseGetPos, xpos, ypos 
¬:= (A_Gui) && currentgui:=a_gui
¬:= (A_Gui) && (!freeze)  && move()
}

move() { 
	PostMessage, 0xA1, 2 ; WM_NCLBUTTONDOWN
}

; thx Lexikos
; https://autohotkey.com/board/topic/23969-resizable-window-border/
; Sizes the client area to fill the entire window.
WM_NCCALCSIZE()
{
    if A_Gui
        return 0
}

; Prevents a border from being drawn when the window is activated.
WM_NCACTIVATE()
{
    if A_Gui
        return 1
}

; Redefine where the sizing borders are.  This is necessary since
; returning 0 for WM_NCCALCSIZE effectively gives borders zero size.
WM_NCHITTEST(wParam, lParam)
{
    static border_size = 6
    
    if !A_Gui
        return
    
    WinGetPos, gX, gY, gW, gH
    
    x := lParam<<48>>48, y := lParam<<32>>48
    
    hit_left    := x <  gX+border_size
    hit_right   := x >= gX+gW-border_size
    hit_top     := y <  gY+border_size
    hit_bottom  := y >= gY+gH-border_size
    
    if hit_top
    {
        if hit_left
            return 0xD
        else if hit_right
            return 0xE
        else
            return 0xC
    }
    else if hit_bottom
    {
        if hit_left
            return 0x10
        else if hit_right
            return 0x11
        else
            return 0xF
    }
    else if hit_left
        return 0xA
    else if hit_right
        return 0xB
    
    ; else let default hit-testing be done
}


;thx VxE's , Icarus
GuiGetPos( ByRef X, ByRef Y, ByRef W, ByRef H, GuiID=1 ) {
	Gui %GuiID%:+LastFound Exist
	IfWinExist
	{
		WinGetPos X, Y
		VarSetCapacity( rect, 16, 0 )
		DllCall("GetClientRect", uint, MyGuiHWND := WinExist(), uint, &rect )
		W := NumGet( rect, 8, "int" )
		H := NumGet( rect, 12, "int" )
	}
}


return
guiclose:
~esc::
exitapp

Last edited by SpeedMaster on 10 Oct 2020, 13:28, edited 6 times in total.
burque505
Posts: 1736
Joined: 22 Jan 2017, 19:37

Re: CENSOR BOX

04 Jul 2020, 13:43

Thank you, @SpeedMaster! Very useful for redacting before taking a screenshot. :bravo:
Regards,
burque505
AHKStudent
Posts: 1472
Joined: 05 May 2018, 12:23

Re: CENSOR BOX

04 Jul 2020, 17:26

this will get some good use, thanks
User avatar
elModo7
Posts: 217
Joined: 01 Sep 2017, 02:38
Location: Spain
Contact:

Re: CENSOR BOX

07 Jul 2020, 06:24

Woah! great for taking captures and removing info pre-screenshotting!
Ty!
User avatar
rommmcek
Posts: 1475
Joined: 15 Aug 2014, 15:18

Re: CENSOR BOX

08 Jul 2020, 09:56

SpeedMaster's awesome idea (again), flyingDman's lightweight rectangle and lightweight x-cross I'm using:

Code: Select all

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn  ; Enable warnings to assist with detecting common errors.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.

; =================================================================================================================================
; Name:           CENSOR BOX 
; Description:    Hide parts of the screen with a colored box
; credits:        Speedmaster, Lexikos, flyingDman, rommmcek
; Topic:          https://www.autohotkey.com/boards/viewtopic.php?f=6&t=78160
; Sript version:  1.1
; AHK Version:    1.1.24.03 (A32/U32/U64)
; Tested on:      Win 7 
; shortcuts::     f1 add a new box
;                 f2 color the box with the top left pixel
;                 f3 freeze/unfreeze all boxes
;                 +LButton show x-cross
;                 Shift Up (while LButton down) start drawing rectangle
;                 all boxes are resizable with the mouse

#SingleInstance force

OnMessage(0x201, "WM_LBUTTONDOWN")
OnMessage(0x84, "WM_NCHITTEST")
OnMessage(0x83, "WM_NCCALCSIZE")
OnMessage(0x86, "WM_NCACTIVATE")
CoordMode, pixel, screen
CoordMode, mouse, screen


BW := 2           				; Border width (and height) in pixels
BC := "FF8800"       			; Border color
FirstCall := True
Gui, -Caption +ToolWindow +LastFound +AlwaysOnTop
Gui, Color, %BC%

Gui, Cross1: -Caption +AlwaysOnTop +ToolWindow
Gui, Cross1: Color, % bc
Gui, Cross2: -Caption +AlwaysOnTop +ToolWindow
Gui, Cross2: Color, % bc
count:= 0
Return

+LButton::
While GetKeyState("Shift", "P") {
    MouseGetPos, OriginX, OriginY
    ;OriginX-=8, OriginY-=5
    Gui, Cross1: show, % "NA y0 w1 x" OriginX " h" A_ScreenHeight
    Gui, Cross2: show, % "NA x0 h1 y" OriginY " w" A_ScreenWidth
    Sleep, 10
}
Gui, Cross1: Hide
Gui, Cross2: Hide
if !GetKeyState("LButton", "P")
    Return
WinGetActiveStats, Title, WindowWidth, WindowHeight, WindowX, WindowY
SetTimer, DrawRectangle, 20
Return

DrawRectangle: ; thx flyingDman
MouseGetPos, X2, Y2
Gui, +LastFound

X2<OriginX? (X1:=X2, X2:=OriginX): X1:=OriginX, Y2<OriginY? (Y1:=Y2, Y2:=OriginY): Y1:=OriginY
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
}
else WinMove, , , X1, Y1, W1, H1
Return

+LButton Up::
SetTimer, DrawRectangle, Off
Gui, Cancel
FirstCall := True
if GetKeyState("Shift", "P")
    Return
newbox:
count++
Gui box_%count%: -caption -DPIScale +AlwaysOnTop +resize ;+LabelGui +LastFound
Gui box_%count%: color, black
Gui box_%count%: show, % "NA x" X1 " y" Y1 " w" W1-16 " h" H1-16, box_%count% ; offset (-16) might be PC/OS specific
return

f2::
WinGetPos,px,py,,, %currentgui%
PixelGetColor, outcolor, % px-1, % py-1, RGB
gui %currentgui%: color, % outcolor
return

f3::
freeze:=!freeze
if freeze
    loop, % count
        gui box_%a_index%: -resize
else
    loop, % count
        gui box_%a_index%: +resize
return

WM_LBUTTONDOWN() {
    global currentgui, freeze
    MouseGetPos, xpos, ypos 
¬:= (A_Gui) && currentgui:=a_gui
¬:= (A_Gui) && (!freeze)  && move()
}

move() { 
	PostMessage, 0xA1, 2 ; WM_NCLBUTTONDOWN
}

; thx Lexikos
; https://autohotkey.com/board/topic/23969-resizable-window-border/
; Sizes the client area to fill the entire window.
WM_NCCALCSIZE()
{
    if A_Gui
        return 0
}

; Prevents a border from being drawn when the window is activated.
WM_NCACTIVATE()
{
    if A_Gui
        return 1
}

; Redefine where the sizing borders are.  This is necessary since
; returning 0 for WM_NCCALCSIZE effectively gives borders zero size.
WM_NCHITTEST(wParam, lParam)
{
    static border_size = 6
    
    if !A_Gui
        return
    
    WinGetPos, gX, gY, gW, gH
    
    x := lParam<<48>>48, y := lParam<<32>>48
    
    hit_left    := x <  gX+border_size
    hit_right   := x >= gX+gW-border_size
    hit_top     := y <  gY+border_size
    hit_bottom  := y >= gY+gH-border_size
    
    if hit_top
    {
        if hit_left
            return 0xD
        else if hit_right
            return 0xE
        else
            return 0xC
    }
    else if hit_bottom
    {
        if hit_left
            return 0x10
        else if hit_right
            return 0x11
        else
            return 0xF
    }
    else if hit_left
        return 0xA
    else if hit_right
        return 0xB
    
    ; else let default hit-testing be done
}


return
guiclose:
~esc::
exitapp

P.s.: Press and hold Shift, press LButton, move mouse to positon x-cross, release Shift, move mouse to draw rectangle, release LButton. Reposition, resize, get background color and (un)freeze remain unchanged.
MancioDellaVega
Posts: 83
Joined: 16 May 2020, 12:27
Location: Italy

Re: CENSOR BOX

08 Jul 2020, 12:53

Great idea and awesome script guys !!!!...Thanks so much...
Courses on AutoHotkey
daywalker
Posts: 32
Joined: 18 Jun 2019, 01:37

Re: CENSOR BOX

09 Jul 2020, 00:34

Great work! How can I remove one box?
MancioDellaVega
Posts: 83
Joined: 16 May 2020, 12:27
Location: Italy

Re: CENSOR BOX

09 Jul 2020, 04:22

daywalker wrote:
09 Jul 2020, 00:34
Great work! How can I remove one box?
I think that it is enough to add something like this...

Code: Select all

F4::
    gui %currentgui%: Destroy
return
Courses on AutoHotkey
User avatar
SpeedMaster
Posts: 494
Joined: 12 Nov 2016, 16:09

Re: CENSOR BOX

10 Jul 2020, 04:21

rommmcek wrote:
08 Jul 2020, 09:56
P.s.: Press and hold Shift, press LButton, move mouse to positon x-cross, release Shift, move mouse to draw rectangle, release LButton. Reposition, resize, get background color and (un)freeze remain unchanged.
Thanks rommmcek for the mouse version. It takes a little bit of practice at the beginning to draw a box but once you get used to it, it's very handy. :thumbup:
MancioDellaVega wrote:
09 Jul 2020, 04:22
I think that it is enough to add something like this...
Thank you, it's working very well. 8-)

I've added these improvements to the new version.
MancioDellaVega
Posts: 83
Joined: 16 May 2020, 12:27
Location: Italy

Re: CENSOR BOX

11 Jul 2020, 01:07

What do you think about to add the possibility of censor or highlight part of the screen?... i was thinking something like this....

Code: Select all

F2::
    transparency:=100  ; 0-255 level of transparency (0 =Transparent , 255=full visibile)

    KeyWait, F2, U T0.6    ; Long press (> 0.6 sec) on Esc closes window  Wait no more than 0.6 sec for key release (also suppress auto-repeat)
    If ErrorLevel   
    {
        ; timeout, so long press
        
        ;~ ToolTip,long press
        ;~ Sleep,1000
        ;~ ToolTip
        
        Gui,%currentgui%: +LastFound
        ; Set Box to transparency
        WinSet,Transparent,%transparency%
        Gui, %currentgui%:Color, FF0000
        

        
    }else{
        ;~ ToolTip,Short press
        ;~ Sleep,1000
        ;~ ToolTip
        
        WinGetPos,px,py,,, %currentgui%
        PixelGetColor, outcolor, % px-1, % py-1, RGB
        Gui,%currentgui%: +LastFound
        ; Set Box to No transparency
        WinSet,Transparent,255

        Gui %currentgui%: color, % outcolor
        
        
    }
    
    KeyWait, F2, U T0.6   ;Wait no more than 0.6 sec for key release (also suppress auto-repeat)


return

Courses on AutoHotkey
User avatar
SpeedMaster
Posts: 494
Joined: 12 Nov 2016, 16:09

Re: CENSOR BOX

14 Jul 2020, 17:37

MancioDellaVega wrote:
11 Jul 2020, 01:07
What do you think about to add the possibility of censor or highlight part of the screen?... i was thinking something like this....
Nice addition thanks :)
updated the script to v1.3

Cheers
User avatar
SpeedMaster
Posts: 494
Joined: 12 Nov 2016, 16:09

Re: CENSOR BOX

10 Oct 2020, 13:43

New version (v1.4)

I've added some additional shortcuts that might be useful. if you don't need them, feel free to disable them... ;)
  • F5:: hide/show all boxes
  • F6:: make current box semi-transparent or opaque
Cheers
burque505
Posts: 1736
Joined: 22 Jan 2017, 19:37

Re: CENSOR BOX

10 Oct 2020, 13:53

@SpeedMaster, nice additions, thank you. :thumbup:
Regards,
burque505

Return to “Scripts and Functions (v1)”

Who is online

Users browsing this forum: gwarble and 156 guests