Skip to content

Instantly share code, notes, and snippets.

@kiemrong08
Created February 5, 2025 09:35
Show Gist options
  • Save kiemrong08/f20a14602fd4f000abaeef9873552a05 to your computer and use it in GitHub Desktop.
Save kiemrong08/f20a14602fd4f000abaeef9873552a05 to your computer and use it in GitHub Desktop.
display mouse crosshair autohotkey
#Include, Gdip.ahk
#NoEnv
#SingleInstance, Force
SetBatchLines, -1 ; Tối ưu hiệu năng CPU
SetWinDelay, -1 ; Giảm độ trễ của GUI
SetControlDelay, -1
SendMode Input
SetWorkingDir %A_ScriptDir%
CoordMode, Mouse, Screen
; ------- Configurable Section -------
global CrosshairOpacity = 4D ; Made global for dynamic adjustment
CrosshairColor = FF00FF
CrosshairWidth = 1
Interval = 64 ;in millisecond
; ------------------------------------
; Initialize visibility state
global IsCrosshairVisible := true
; Setup timers
SetTimer, UPDATEDSCRIPT, 1000
SetTimer, Check, %Interval%
If !pToken := Gdip_Startup()
{
MsgBox, 48, gdiplus error!, Gdiplus failed to start. Please ensure you have gdiplus on your system
ExitApp
}
OnExit, Exit
; Get the virtual screen dimensions (all monitors combined)
SysGet, VirtualWidth, 78 ; SM_CXVIRTUALSCREEN
SysGet, VirtualHeight, 79 ; SM_CYVIRTUALSCREEN
SysGet, VirtualLeft, 76 ; SM_XVIRTUALSCREEN
SysGet, VirtualTop, 77 ; SM_YVIRTUALSCREEN
Width := VirtualWidth
Height := VirtualHeight
; Setup GUI
Gui, 1: -Caption +E0x80000 +LastFound +OwnDialogs +Owner +AlwaysOnTop
Gui, 1: Show, NA, Gui98
WinSet, ExStyle, +0x20, Gui98
hwnd1 := WinExist()
; Initialize graphics
hbm := CreateDIBSection(Width, Height)
hdc := CreateCompatibleDC()
obm := SelectObject(hdc, hbm)
G := Gdip_GraphicsFromHDC(hdc)
Gdip_SetSmoothingMode(G, 4)
Gdip_GraphicsClear(G)
; Initial draw of crosshair
gosub Draw ; Add this line to show crosshair immediately
Draw:
if (!IsCrosshairVisible) {
Gdip_GraphicsClear(G)
UpdateLayeredWindow(hwnd1, hdc, VirtualLeft, VirtualTop, Width, Height)
return
}
MyPen = 0x%CrosshairOpacity%%CrosshairColor%
pPen := Gdip_CreatePen(MyPen, CrosshairWidth)
MouseGetPos, xx, yy
; Adjust coordinates relative to virtual screen
xx := xx - VirtualLeft
yy := yy - VirtualTop
currentx := xx
currenty := yy
DrawLine:
Gdip_DrawLine(G, pPen, 0, yy, Width, yy)
UpdateLayeredWindow(hwnd1, hdc, VirtualLeft, VirtualTop, Width, Height)
Gdip_DrawLine(G, pPen, xx, 0, xx, Height)
UpdateLayeredWindow(hwnd1, hdc, VirtualLeft, VirtualTop, Width, Height)
Gdip_DeletePen(pPen)
return
Redraw:
Gdip_GraphicsClear(G)
UpdateLayeredWindow(hwnd1, hdc, VirtualLeft, VirtualTop, Width, Height)
Goto Draw
Check:
MouseGetPos, xx2, yy2
; Adjust coordinates relative to virtual screen
xx2 := xx2 - VirtualLeft
yy2 := yy2 - VirtualTop
if (currentx != xx2) or (currenty != yy2)
{
currentx := xx2
currenty := yy2
gosub Redraw
}
return
; Hotkey to toggle crosshair (Alt+2)
!2::
IsCrosshairVisible := true
gosub Redraw
return
; Hotkey to hide crosshair (Esc)
esc::
IsCrosshairVisible := false
Gdip_GraphicsClear(G)
UpdateLayeredWindow(hwnd1, hdc, VirtualLeft, VirtualTop, Width, Height)
return
; Decrease opacity (Ctrl+8)
^8::
; Convert hex to decimal for calculation
currentOpacity := "0x" . CrosshairOpacity
if (currentOpacity > 0x20) { ; Don't let it become completely transparent
currentOpacity -= 0x10 ; Decrease by 16 (in hex)
CrosshairOpacity := Format("{:02X}", currentOpacity)
gosub Redraw
ShowOpacityTooltip()
}
return
; Increase opacity (Ctrl+9)
^9::
; Convert hex to decimal for calculation
currentOpacity := "0x" . CrosshairOpacity
if (currentOpacity < 0xFF) {
currentOpacity += 0x10 ; Increase by 16 (in hex)
CrosshairOpacity := Format("{:02X}", currentOpacity)
gosub Redraw
ShowOpacityTooltip()
}
return
; Function to show current opacity
ShowOpacityTooltip() {
currentOpacity := "0x" . CrosshairOpacity
opacityPercentage := Round((currentOpacity / 0xFF) * 100)
ToolTip, Opacity: %opacityPercentage%`%
SetTimer, RemoveToolTip, -1000
}
RemoveToolTip:
ToolTip
return
Exit:
SelectObject(hdc, obm)
DeleteObject(hbm)
DeleteDC(hdc)
Gdip_DeleteGraphics(G)
Gdip_Shutdown(pToken)
ExitApp
Return
UPDATEDSCRIPT:
FileGetAttrib,attribs,%A_ScriptFullPath%
IfInString,attribs,A
{
FileSetAttrib,-A,%A_ScriptFullPath%
Sleep,500
Reload
}
Return
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment