How to set the mouse cursor icon?

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
mikhail22
Posts: 19
Joined: 14 Jan 2018, 11:50

How to set the mouse cursor icon?

16 Mar 2022, 19:33

How to set the mouse cursor correctly?
I just want to set it to crosshair but I can't.
Below is the test script, it works but only if the mouse is not moving,
if I move it even a bit it resets back to arrow.
I want it to stay as crosshair if I'm inside my GUI.

Code: Select all

#SingleInstance force

; Gui, +LastFound +AlwaysOnTop +ToolWindow -Caption  
Gui, +AlwaysOnTop +ToolWindow -Caption  

Gui, Show, w200 h200 
cursorCross := DllCall( "LoadCursor", Uint, 0, Int, 32515 )

z::
	DllCall( "SetCursor", Uint, cursorCross )
return

esc::
GuiClose:
ExitApp
teadrinker
Posts: 4412
Joined: 29 Mar 2015, 09:41
Contact:

Re: How to set the mouse cursor icon?

16 Mar 2022, 21:53

Code: Select all

Gui, New, +hwndhGui +AlwaysOnTop +ToolWindow -Caption
OnMessage(0x20, Func("WM_SETCURSOR").Bind(hGui))
Gui, Show, w200 h200

LoadCursor(cursorId) {
   static IMAGE_CURSOR := 2, flags := (LR_DEFAULTSIZE := 0x40) | (LR_SHARED := 0x8000)
   Return DllCall("LoadImage", "Ptr", 0, "UInt", cursorId, "UInt", IMAGE_CURSOR
                             , "Int", 0, "Int", 0, "UInt", flags, "Ptr")
}

WM_SETCURSOR(hGui) {
   static hCursor := LoadCursor(IDC_CROSS := 32515)
   if (A_Gui = hGui)
      Return DllCall("SetCursor", "Ptr", hCursor, "Ptr")
}
mikhail22
Posts: 19
Joined: 14 Jan 2018, 11:50

Re: How to set the mouse cursor icon?

18 Mar 2022, 11:03

Thanks, it works @teadrinker .
There is however seldom flickering back to default arrow, very seldom but still.

BTW I have found this tutorial

https://www.autohotkey.com/boards/viewtopic.php?t=61496

It works and does not flicker at all and does not require message tracking.
Here is minimal example:

Code: Select all

Gui, +HwndMyGui
Gui, +AlwaysOnTop +ToolWindow -Caption  
Gui, Show, w200 h200 NA 

cursorCross := DllCall( "LoadCursor", Uint, 0, Int, 32515 )
DllCall("SetClassLongPtrW", Uint, MyGui, int, -12, Ptr, cursorCross)
teadrinker
Posts: 4412
Joined: 29 Mar 2015, 09:41
Contact:

Re: How to set the mouse cursor icon?

18 Mar 2022, 11:21

But this code changes cursors for all GUIs:

Code: Select all

Gui, New, +HwndMyGui
Gui, +AlwaysOnTop +ToolWindow -Caption  
Gui, Show, x0 w200 h200 NA 

Gui, New, +HwndMyGui2
Gui, +AlwaysOnTop +ToolWindow -Caption  
Gui, Show, x350 w200 h200 NA 

cursorCross := DllCall( "LoadCursor", Uint, 0, Int, 32515 )
DllCall("SetClassLongPtrW", Uint, MyGui, int, -12, Ptr, cursorCross)
mikhail22
Posts: 19
Joined: 14 Jan 2018, 11:50

Re: How to set the mouse cursor icon?

18 Mar 2022, 11:53

@teadrinker
that is interesting, but in my case I have never had single AHK script with several guis.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Google [Bot], Mateusz53, MrHue, peter_ahk, Pianist and 267 guests