This script will put a colored border around any window.
Code:
;This script will put a colored border around any window.
;IF you find it useful you can donate to imamdi on PokerStars
#SingleInstance,Force
SetWinDelay,-1
List_OwnerId:="" ;
HookProcAdr := RegisterCallback( "WinEventProc", "F" )
OnExit, ExitSub
; run the script in admin mode
; remove ; before RunAsAdmin()
;---------------------------------
; RunAsAdmin()
if !(token:=Gdip_Startup())
{
MsgBox,ERROR CustomWindowScript will shutdown
ExitApp
}
/*
* Declare your hotkeys here
* Function : SetBorder(windowId,BGR_COLOR)
* color is in BGR format Red,Green,Blue
*
* Function : DestroyBorder(WindowId)
* function will remove the border
*
* Function : getMouseOver()
* get window id under the mouse
*/
;========================================================================================
F1::setBorder(getMouseOver(),0x0000FF) ;RED
F2::setBorder(getMouseOver(),0x00FF00) ;GREEN
F3::setBorder(getMouseOver(),0xFF0000) ;BLUE
F4::setBorder(getMouseOver(),0x000000) ;BLACK
F6::DestroyBorder(getMouseOver()) ;remove border under the mouse
F7::ExitApp ; close script
return
;========================================================================================
;========================================================================================
ExitSub:
if(token)
{
Gdip_Shutdown(token)
}
ExitApp
/*
;****************************************************************************************
; Declare functions
;****************************************************************************************
*/
SetBorder(Id,Color)
{
global list_OwnerId
if (id = DLLCALL("GetDesktopWindow") OR id = DLLCALL("GetShellWindow") )
{
return
}
color:=BgrToArgb(color)
if id not in %list_OwnerId%
{
if(hwnd:=createCustomWindow(Id))
{
addList(list_OwnerId,Id)
setBorderColor(id,color)
setBorderId(id,hwnd)
setHook(id)
DrawBorder(id)
return 1
}
}else if (getBorderColor(id) != color)
{
setBorderColor(id,color)
DrawBorder(id)
return 1
}
return 0
}
setBorderColor(hwnd,color)
{
global
cw%hwnd%_color:=color
}
setBorderId(hwnd,Id)
{
global
cw%hwnd%_borderId:=id
}
BgrToArgb(color)
{
red:= ((Color & 0xff0000) >> 16)
green:= ((Color & 0x00ff00) >> 8)
blue:= (Color & 0xff)
color2:= (blue << 16) + (green << 8) + red
return color2
}
RunAsAdmin()
{
if not A_IsAdmin
{
Run *RunAs "%A_ScriptFullPath%" ; Requires v1.0.92.01+
ExitApp
}
} ;#ENDFUNCTION
getWindowPrev(hwnd)
{
return DecToHex(DLLCALL("GetWindow",UINT,hwnd,INT,3))
} ;#ENDFUNCTION
GetMouseOver()
{
MouseGetPos,,,Id
return id
}
getBorderColor(hwnd)
{
global
return cw%hwnd%_color
}
getBorderId(hwnd)
{
global
return cw%hwnd%_borderId
}
getWindowBorderSize(hwnd,byref hBorder,byref vBorder)
{
VarSetCapacity(struct,60)
numput(60,struct,0,"int")
DllCall("GetWindowInfo","UINT",hwnd,ptr,&struct)
hBorder:=numget(struct,48,int)
vBorder:=numget(struct,52,int)
}
HookProcAdr := RegisterCallback( "WinEventProc", "F" )
decToHex(var)
{
SetFormat, IntegerFast, hex
Var += 0 ; Sets Var (which previously contained 11) to be 0xb.
Var .= "" ; Necessary due to the "fast" mode.
SetFormat, IntegerFast, d
return var
}
DrawProc(hwnd,msg="",lparm="",wparam="")
{
global
local x,y,w,h,id
wingetPos,x,y,w,h,ahk_id%hwnd% ;
if( cw%hwnd%_w !=w or cw%hwnd%_h != h)
{
cw%hwnd%_w := w
cw%hwnd%_h := h
DrawBorder(hwnd)
}else if(cw%hwnd%_x !=x or cw%hwnd%_y != y)
{
cw%hwnd%_x := x
cw%hwnd%_y := y
id:= cw%hwnd%_borderId
winmove, ahk_id%id%,,x,y,
}
} ;#ENDFUNTION
WinEventProc( hWinEventHook, event, hwnd,idObject,idChild,dwEventThread,dwmsEventTime)
{
global list_ownerId
static EVENT_OBJECT_LOCATIONCHANGE:=0x800B, EVENT_OBJECT_DESTROY:=0x8001
if(idObject==0)
{
hwnd:=decToHex(hwnd)
if hwnd in %list_ownerId%
{
if(event =EVENT_OBJECT_LOCATIONCHANGE)
{
DrawProc(hwnd)
}else if(event =EVENT_OBJECT_DESTROY)
{
DestroyBorder(hwnd)
}
}
}
} ;#ENDFUNTION
DrawBorder(id)
{
wingetPos,x,y,Width, Height,ahk_id%id%
GetWindowBorderSize(id,hBorder,vBorder) ;
hwnd:=GetBorderId(id)
hbm := CreateDIBSection(Width, Height)
hdc := CreateCompatibleDC()
obm := SelectObject(hdc, hbm)
G := Gdip_GraphicsFromHDC(hdc)
Gdip_SetSmoothingMode(G, 4)
pPen := Gdip_CreatePen(0xFF000000 | GetBorderColor(Id), hBorder)
Gdip_DrawRectangle(G, pPen,0, 0, width, height)
UpdateLayeredWindow(hwnd, hdc, x, y, Width, Height)
SelectObject(hdc, obm)
DeleteObject(hbm)
DeleteDC(hdc)
Gdip_DeleteGraphics(G)
} ;#ENDFUNTION
DestroyBorder(hwnd)
{
global
if hwnd in %list_OwnerId%
{
gui id%hwnd%:destroy
delList(list_ownerId,hwnd)
}
} ;#ENDFUNTION
createCustomWindow(Id)
{
name:= "id" . id
gui %name%:new, +HwndWindowId +ToolWindow +E0x08000000 +E0x80000 +E0x00000020 +Owner%id% -border -caption, ;
DLLCALL("SetWindowPos",UINT,windowId,INT,GetWindowPrev(id),INT,0,INT,0,INT,0,INT,0,UINT,0x0010|0x0002|0x0001|0x0040)
return WindowId ;
} ;#ENDFUNCTION
SetWinEventHook(eventMin, eventMax, hmodWinEventProc, lpfnWinEventProc, idProcess, idThread, dwFlags)
{
DllCall("CoInitialize", Uint, 0)
return DllCall("SetWinEventHook"
, Uint,eventMin
, Uint,eventMax
, Uint,hmodWinEventProc
, Uint,lpfnWinEventProc
, Uint,idProcess
, Uint,idThread
, Uint,dwFlags)
} ;#ENDFUNCTION
addList(byref list, input)
{
list:=list ? list . "," . input : input
} ;#ENDFUNCTION
delList(byref list,input)
{
OutputVar:= "," . list . ","
needle:= "," . input . ","
StringReplace,OutputVar,OutputVar,%needle%,`,,
StringTrimLeft, OutputVar, OutputVar, 1
StringTrimRight, OutputVar, OutputVar, 1
list:=OutputVar
} ;#ENDFUNCTION
setHook(Hwnd)
{
global HookProcAdr
static list_Pid:="" ,EVENT_OBJECT_LOCATIONCHANGE:=0x800B, WINEVENT_OUTOFCONTEXT:=0,EVENT_OBJECT_DESTROY:=0x8001
winget,pid,Pid,ahk_id%hwnd%
if pid not in %List_pid%
{
addlist(list_pid,pid)
setwinEventHook(EVENT_OBJECT_LOCATIONCHANGE,EVENT_OBJECT_LOCATIONCHANGE,0,HookProcAdr,pid,0,WINEVENT_OUTOFCONTEXT)
setwinEventHook(EVENT_OBJECT_DESTROY,EVENT_OBJECT_DESTROY,0,HookProcAdr,pid,0,WINEVENT_OUTOFCONTEXT)
}
} ;#ENDFUNCTION
;###################################################################################################################
;###################################################################################################################
CreateDIBSection(w, h, hdc="", bpp=32, ByRef ppvBits=0)
{
Ptr := A_PtrSize ? "UPtr" : "UInt"
hdc2 := hdc ? hdc : GetDC()
VarSetCapacity(bi, 40, 0)
NumPut(w, bi, 4, "uint")
, NumPut(h, bi, 8, "uint")
, NumPut(40, bi, 0, "uint")
, NumPut(1, bi, 12, "ushort")
, NumPut(0, bi, 16, "uInt")
, NumPut(bpp, bi, 14, "ushort")
hbm := DllCall("CreateDIBSection"
, Ptr, hdc2
, Ptr, &bi
, "uint", 0
, A_PtrSize ? "UPtr*" : "uint*", ppvBits
, Ptr, 0
, "uint", 0, Ptr)
if !hdc
ReleaseDC(hdc2)
return hbm
}
CreateCompatibleDC(hdc=0)
{
return DllCall("CreateCompatibleDC", A_PtrSize ? "UPtr" : "UInt", hdc)
}
SelectObject(hdc, hgdiobj)
{
Ptr := A_PtrSize ? "UPtr" : "UInt"
return DllCall("SelectObject", Ptr, hdc, Ptr, hgdiobj)
}
DeleteObject(hObject)
{
return DllCall("DeleteObject", A_PtrSize ? "UPtr" : "UInt", hObject)
}
GetDC(hwnd=0)
{
return DllCall("GetDC", A_PtrSize ? "UPtr" : "UInt", hwnd)
}
Gdip_DeletePen(pPen)
{
return DllCall("gdiplus\GdipDeletePen", A_PtrSize ? "UPtr" : "UInt", pPen)
}
Gdip_CreatePen(ARGB, w)
{
DllCall("gdiplus\GdipCreatePen1", "UInt", ARGB, "float", w, "int", 2, A_PtrSize ? "UPtr*" : "UInt*", pPen)
DllCall("gdiplus\GdipSetPenMode", "ptr", pPen,UINT,1)
return pPen
}
Gdip_GraphicsFromHDC(hdc)
{
DllCall("gdiplus\GdipCreateFromHDC", A_PtrSize ? "UPtr" : "UInt", hdc, A_PtrSize ? "UPtr*" : "UInt*", pGraphics)
return pGraphics
}
Gdip_DrawRectangle(pGraphics, pPen, x, y, w, h)
{
Ptr := A_PtrSize ? "UPtr" : "UInt"
return DllCall("gdiplus\GdipDrawRectangle", Ptr, pGraphics, Ptr, pPen, "float", x, "float", y, "float", w, "float", h)
}
UpdateLayeredWindow(hwnd, hdc, x="", y="", w="", h="", Alpha=255)
{
Ptr := A_PtrSize ? "UPtr" : "UInt"
if ((x != "") && (y != ""))
VarSetCapacity(pt, 8), NumPut(x, pt, 0, "UInt"), NumPut(y, pt, 4, "UInt")
if (w = "") ||(h = "")
WinGetPos,,, w, h, ahk_id %hwnd%
return DllCall("UpdateLayeredWindow"
, Ptr, hwnd
, Ptr, 0
, Ptr, ((x = "") && (y = "")) ? 0 : &pt
, "int64*", w|h<<32
, Ptr, hdc
, "int64*", 0
, "uint", 0
, "UInt*", Alpha<<16|1<<24
, "uint", 2)
}
DeleteDC(hdc)
{
return DllCall("DeleteDC", A_PtrSize ? "UPtr" : "UInt", hdc)
}
Gdip_DeleteGraphics(pGraphics)
{
return DllCall("gdiplus\GdipDeleteGraphics", A_PtrSize ? "UPtr" : "UInt", pGraphics)
}
Gdip_Startup()
{
Ptr := A_PtrSize ? "UPtr" : "UInt"
if !DllCall("GetModuleHandle", "str", "gdiplus", Ptr)
DllCall("LoadLibrary", "str", "gdiplus")
VarSetCapacity(si, A_PtrSize = 8 ? 24 : 16, 0), si := Chr(1)
DllCall("gdiplus\GdiplusStartup", A_PtrSize ? "UPtr*" : "uint*", pToken, Ptr, &si, Ptr, 0)
return pToken
}
Gdip_Shutdown(pToken)
{
Ptr := A_PtrSize ? "UPtr" : "UInt"
DllCall("gdiplus\GdiplusShutdown", Ptr, pToken)
if hModule := DllCall("GetModuleHandle", "str", "gdiplus", Ptr)
DllCall("FreeLibrary", Ptr, hModule)
return 0
}
Gdip_SetSmoothingMode(pGraphics, SmoothingMode)
{
return DllCall("gdiplus\GdipSetSmoothingMode", A_PtrSize ? "UPtr" : "UInt", pGraphics, "int", SmoothingMode)
}
ReleaseDC(hdc, hwnd=0)
{
Ptr := A_PtrSize ? "UPtr" : "UInt"
return DllCall("ReleaseDC", Ptr, hwnd, Ptr, hdc)
}
;#ENDSCRIPT