Changing mouse cursor

Get help with using AutoHotkey and its commands and hotkeys
User avatar
Scr1pter
Posts: 1069
Joined: 06 Aug 2017, 08:21
Location: Germany

Changing mouse cursor

20 Jan 2019, 18:34

Hello,

I found this old thread:
https://autohotkey.com/board/topic/32608-changing-the-system-cursor/

I copied the wrapper function and tested it.

Code: Select all

+lbutton::
SetSystemCursor("C:\Windows\Cursors\up_il.cur", 30, 30) ; Path to cursor, xDimension, yDimension
return

SetSystemCursor(Cursor = "", cx = 0, cy = 0)
{
	BlankCursor := 0, SystemCursor := 0, FileCursor := 0 ; init	
	SystemCursors = 32512IDC_ARROW, 32513IDC_IBEAM, 32514IDC_WAIT, 32515IDC_CROSS, 32516IDC_UPARROW, 32640IDC_SIZE, 32641IDC_ICON, 32642IDC_SIZENWSE
                 ,32643IDC_SIZENESW, 32644IDC_SIZEWE, 32645IDC_SIZENS, 32646IDC_SIZEALL, 32648IDC_NO, 32649IDC_HAND, 32650IDC_APPSTARTING, 32651IDC_HELP	
	if Cursor = ; empty, so create blank cursor 
	{
		VarSetCapacity(AndMask, 32 * 4, 0xFF), VarSetCapacity(XorMask, 32 * 4, 0)
		BlankCursor = 1 ; flag for later
	}
	else if SubStr(Cursor,1,4) = "IDC_" ; load system cursor
	{
		Loop, Parse, SystemCursors, `,
		{
			CursorName := SubStr(A_Loopfield, 6, 15) ; get the cursor name, no trailing space with substr
			CursorID := SubStr(A_Loopfield, 1, 5) ; get the cursor id
			SystemCursor = 1
			if (CursorName = Cursor)
			{
				CursorHandle := DllCall("LoadCursor", Uint,0, Int,CursorID)	
				Break					
			}
		}	
		if CursorHandle = ; invalid cursor name given
		{
			Msgbox,, SetCursor, Error: Invalid cursor name
			CursorHandle = Error
		}
	}	
	else if FileExist(Cursor)
	{
		SplitPath, Cursor,,, Ext ; auto-detect type
		if Ext = ico
    {
      uType := 0x1
    }
		else if Ext in cur,ani
    {
      uType := 0x2		
    }			
		else ; invalid file ext
		{
			Msgbox,, SetCursor, Error: Invalid file type
			CursorHandle = Error
		}		
		FileCursor = 1
	}
	else
	{	
		Msgbox,, SetCursor, Error: Invalid file path or cursor name
		CursorHandle = Error ; raise for later
	}
	if CursorHandle != Error 
	{
		Loop, Parse, SystemCursors, `,
		{
			if BlankCursor = 1 
			{
				Type = BlankCursor
				%Type%%A_Index% := DllCall("CreateCursor", Uint, 0, Int, 0, Int, 0, Int, 32, Int, 32, Uint, &AndMask, Uint, &XorMask)
				CursorHandle := DllCall("CopyImage", Uint,%Type%%A_Index%, Uint, 0x2, Int, 0, Int, 0, Int, 0)
				DllCall("SetSystemCursor", Uint,CursorHandle, Int,SubStr(A_Loopfield, 1, 5))
			}			
			else if SystemCursor = 1
			{
				Type = SystemCursor
				CursorHandle := DllCall("LoadCursor", Uint, 0, Int, CursorID)	
				%Type%%A_Index% := DllCall("CopyImage", Uint, CursorHandle, Uint, 0x2, Int, cx, Int, cy, Uint, 0)		
				CursorHandle := DllCall("CopyImage", Uint,%Type%%A_Index%, Uint, 0x2, Int, 0, Int, 0, Int, 0)
				DllCall("SetSystemCursor", Uint,CursorHandle, Int,SubStr(A_Loopfield, 1, 5))
			}
			else if FileCursor = 1
			{
				Type = FileCursor
				%Type%%A_Index% := DllCall("LoadImageA", UInt,0, Str, Cursor, UInt, uType, Int, cx, Int, cy, UInt, 0x10) 
				DllCall("SetSystemCursor", Uint,%Type%%A_Index%, Int,SubStr(A_Loopfield, 1, 5))			
			}          
		}
	}	
}
The first test was positive, the cursor changed.
However, how can I change it so that it always has the new look?
(Independent of the background, no matter if text, links etc.)

I created a function to draw on my screen, and therefore it would be useful to have the crosshair all the time.
(Same way as when drawing in Microsoft Paint, where the cursor turns into a crosshair.)

Perhaps I missed something inside of this script.

Thanks for any help.

Cheers!
Please use code tags when posting code!
Keyboard: Logitech G15 FE - Mice: Logitech G9 FE, G502 FE, G602 - OS: Windows 10 Pro 64 Bit - AHK version: 1.1.29.00

Return to “Ask For Help”

Who is online

Users browsing this forum: dd900, flyingDman, Google [Bot] and 93 guests