Windows 10下按住鼠标左键时更改光标颜色的方法求助
Got it, let's tackle this problem. Your goal is to change the cursor's color system-wide when holding the left mouse button to visually confirm if ClickLock is active, right? The issue with your current AutoHotkey script is twofold: it swaps the cursor to a predefined system cursor (not a colored one) and doesn't account for Windows automatically resetting the cursor when you move between windows or interact with different UI elements.
Here are two solid, working solutions:
Solution 1: Use a Custom Colored Cursor File
This is the simpler approach—you'll need a pre-made colored cursor (e.g., red for ClickLock active) saved as a .cur or .ani file. You can create one with tools like CursorFX or free online cursor editors.
Working AutoHotkey Script
#SingleInstance Force ; Replace this path with your custom colored cursor file location CustomCursorPath := "C:\Your\Cursor\Path\RedClickLock.cur" ; Preload the custom cursor to avoid repeated file loading hCustomCursor := DllCall("LoadImage", "UInt", 0, "Str", CustomCursorPath, "UInt", 2, "Int", 0, "Int", 0, "UInt", 0x10) ; Trigger when left mouse button is held ~LButton:: while GetKeyState("LButton", "P") { ; Only change cursor if ClickLock is enabled (optional but useful) if (IsClickLockEnabled()) { DllCall("SetCursor", "UInt", hCustomCursor) } Sleep 10 ; Reduce CPU usage with a small delay } ; Restore default cursor when button is released DllCall("SetCursor", "UInt", 0) return ; Handle cursor resets when moving between windows/mouse movement OnMessage(0x200, "WM_MOUSEMOVE") WM_MOUSEMOVE(wParam, lParam) { global hCustomCursor if (GetKeyState("LButton", "P") && IsClickLockEnabled()) { DllCall("SetCursor", "UInt", hCustomCursor) } } ; Helper function to check if ClickLock is enabled system-wide IsClickLockEnabled() { VarSetCapacity(enabled, 4, 0) DllCall("SystemParametersInfo", "UInt", 0x100E, "UInt", 0, "Ptr", &enabled, "UInt", 0) return NumGet(enabled, 0, "UInt") }
Key Notes:
- The
IsClickLockEnabled()function checks the system's ClickLock status directly, so the cursor only changes when ClickLock is actually active (remove this check if you want the color change regardless of ClickLock). - The
WM_MOUSEMOVEmessage handler ensures the custom cursor stays visible even when you switch windows—Windows often resets the cursor during window transitions, so this fixes that.
Solution 2: Dynamically Draw a Colored Cursor (No External Files)
If you don't want to use an external cursor file, you can generate a colored cursor on-the-fly using GDI+ in AutoHotkey. You'll need the Gdip_All.ahk library (include it in your script's directory).
Working AutoHotkey Script
#SingleInstance Force #Include <Gdip_All> ; Initialize GDI+ for cursor drawing Gdip_Startup() ; Create a 32x32 red cursor (adjust size/color as needed) hBitmap := Gdip_CreateBitmap(32, 32) hGraphics := Gdip_GraphicsFromImage(hBitmap) Gdip_SetSmoothingMode(hGraphics, 4) ; Draw a red filled circle as the cursor Gdip_FillEllipse(hGraphics, Gdip_CreateSolidBrush(0xFF0000), 0, 0, 32, 32) ; Convert bitmap to a cursor hCustomCursor := DllCall("CreateCursor", "UInt", 0, "Int", 16, "Int", 16, "Int", 32, "Int", 32, "Ptr", Gdip_LockBits(hBitmap, 0, 0, 32, 32, 0x2, 0x2000), "Ptr", Gdip_LockBits(hBitmap, 0, 0, 32, 32, 0x2, 0x2000)) ; Clean up temporary GDI+ resources Gdip_DeleteGraphics(hGraphics) Gdip_DisposeImage(hBitmap) ; Same button hold logic as Solution 1 ~LButton:: while GetKeyState("LButton", "P") { if (IsClickLockEnabled()) { DllCall("SetCursor", "UInt", hCustomCursor) } Sleep 10 } DllCall("SetCursor", "UInt", 0) return OnMessage(0x200, "WM_MOUSEMOVE") WM_MOUSEMOVE(wParam, lParam) { global hCustomCursor if (GetKeyState("LButton", "P") && IsClickLockEnabled()) { DllCall("SetCursor", "UInt", hCustomCursor) } } IsClickLockEnabled() { VarSetCapacity(enabled, 4, 0) DllCall("SystemParametersInfo", "UInt", 0x100E, "UInt", 0, "Ptr", &enabled, "UInt", 0) return NumGet(enabled, 0, "UInt") } ; Clean up resources on exit ExitApp() { global hCustomCursor DllCall("DestroyCursor", "UInt", hCustomCursor) Gdip_Shutdown() ExitApp }
Key Notes:
- Adjust the color code (
0xFF0000for red) or drawing shape (replaceFillEllipsewithDrawRectangleetc.) to match your preference. - Make sure the
Gdip_All.ahklibrary is in the same folder as your script or referenced correctly with#Include.
内容的提问来源于stack exchange,提问作者Herman Toothrot

