Untitled Diff

Created Diff never expires
9 removals
70 lines
9 additions
70 lines
; Source: Serenity - https://autohotkey.com/board/topic/32608-changing-the-system-cursor/
; Source: Serenity - https://autohotkey.com/board/topic/32608-changing-the-system-cursor/
; Modified: iseahound
; Modified: iseahound


SetSystemCursor(Cursor := "", cx := 0, cy := 0) {
SetSystemCursor(Cursor := "", cx := 0, cy := 0) {


SystemCursors := "32512IDC_ARROW,32513IDC_IBEAM,32514IDC_WAIT,32515IDC_CROSS,32516IDC_UPARROW"
SystemCursors := "32512IDC_ARROW,32513IDC_IBEAM,32514IDC_WAIT,32515IDC_CROSS,32516IDC_UPARROW"
. ",32640IDC_SIZE,32641IDC_ICON,32642IDC_SIZENWSE,32643IDC_SIZENESW,32644IDC_SIZEWE"
. ",32640IDC_SIZE,32641IDC_ICON,32642IDC_SIZENWSE,32643IDC_SIZENESW,32644IDC_SIZEWE"
. ",32645IDC_SIZENS,32646IDC_SIZEALL,32648IDC_NO,32649IDC_HAND,32650IDC_APPSTARTING,32651IDC_HELP"
. ",32645IDC_SIZENS,32646IDC_SIZEALL,32648IDC_NO,32649IDC_HAND,32650IDC_APPSTARTING,32651IDC_HELP"


if (Cursor = "") {
if (Cursor = "") {
VarSetCapacity(AndMask, 128, 0xFF), VarSetCapacity(XorMask, 128, 0)
VarSetCapacity(AndMask, 128, 0xFF), VarSetCapacity(XorMask, 128, 0)


Loop Parse, SystemCursors, % ","
Loop Parse, SystemCursors, % ","
{
{
CursorHandle := DllCall("CreateCursor", "ptr", 0, "int", 0, "int", 0, "int", 32, "int", 32, "ptr", &AndMask, "ptr", &XorMask)
CursorHandle := DllCall("CreateCursor", "ptr", 0, "int", 0, "int", 0, "int", 32, "int", 32, "ptr", &AndMask, "ptr", &XorMask)
DllCall("SetSystemCursor", "ptr", CursorHandle, "int", SubStr(A_LoopField, 1, 5)) ; calls DestroyCursor
DllCall("SetSystemCursor", "ptr", CursorHandle, "uint", SubStr(A_LoopField, 1, 5)) ; calls DestroyCursor
}
}
return
return
}
}


if SubStr(Cursor, 1, 4) = "IDC_" {
if SubStr(Cursor, 1, 4) = "IDC_" {
Loop Parse, SystemCursors, % ","
Loop Parse, SystemCursors, % ","
{
{
CursorName := SubStr(A_LoopField, 6, 15) ; get the cursor name, no trailing space with substr
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
CursorID := SubStr(A_LoopField, 1, 5) ; get the cursor id
} until (CursorName = Cursor)
} until (CursorName = Cursor)


if !(CursorShared:= DllCall("LoadCursor", "uint", 0, "int", CursorID))
if !(CursorShared:= DllCall("LoadCursor", "uint", 0, "uint", CursorID))
throw Exception("Error: Invalid cursor name")
throw Exception("Error: Invalid cursor name")


Loop Parse, SystemCursors, % ","
Loop Parse, SystemCursors, % ","
{
{
CursorHandle := DllCall("CopyImage", "uint", CursorShared, "uint", 2, "int", cx, "int", cy, "uint", 0)
CursorHandle := DllCall("CopyImage", "ptr", CursorShared, "uint", 2, "int", cx, "int", cy, "uint", 0)
DllCall("SetSystemCursor", "ptr", CursorHandle, "int", SubStr(A_LoopField, 1, 5)) ; calls DestroyCursor
DllCall("SetSystemCursor", "ptr", CursorHandle, "uint", SubStr(A_LoopField, 1, 5)) ; calls DestroyCursor
}
}
return
return
}
}


if FileExist(Cursor) {
if FileExist(Cursor) {
SplitPath, Cursor,,, Ext ; auto-detect type
SplitPath, Cursor,,, Ext ; auto-detect type
if ((uType := (Ext = "ani" || Ext = "cur") ? 2 : (Ext = "ico") ? 1 : (Ext = "bmp") ? 0 : "") == "")
if ((uType := (Ext = "ani" || Ext = "cur") ? 2 : (Ext = "ico") ? 1 : (Ext = "bmp") ? 0 : "") == "")
throw Exception("Error: Invalid file type")
throw Exception("Error: Invalid file type")


if (Ext = "ani") {
if (Ext = "ani") {
Loop Parse, SystemCursors, % ","
Loop Parse, SystemCursors, % ","
{
{
CursorHandle := DllCall("LoadImage", "uint", 0, "str", Cursor, "uint", uType, "int", cx, "int", cy, "uint", 0x10)
CursorHandle := DllCall("LoadImage", "ptr", 0, "str", Cursor, "uint", uType, "int", cx, "int", cy, "uint", 0x10)
DllCall("SetSystemCursor", "ptr", CursorHandle, "int", SubStr(A_LoopField, 1, 5)) ; calls DestroyCursor
DllCall("SetSystemCursor", "ptr", CursorHandle, "uint", SubStr(A_LoopField, 1, 5)) ; calls DestroyCursor
}
}
} else {
} else {
if !(CursorShared := DllCall("LoadImage", "uint", 0, "str", Cursor, "uint", uType, "int", cx, "int", cy, "uint", 0x00008010))
if !(CursorShared := DllCall("LoadImage", "ptr", 0, "str", Cursor, "uint", uType, "int", cx, "int", cy, "uint", 0x00008010))
throw Exception("Error: Corrupted file")
throw Exception("Error: Corrupted file")


Loop Parse, SystemCursors, % ","
Loop Parse, SystemCursors, % ","
{
{
CursorHandle := DllCall("CopyImage", "uint", CursorShared, "uint", 2, "int", 0, "int", 0, "uint", 0)
CursorHandle := DllCall("CopyImage", "ptr", CursorShared, "uint", 2, "int", 0, "int", 0, "uint", 0)
DllCall("SetSystemCursor", "ptr", CursorHandle, "int", SubStr(A_LoopField, 1, 5)) ; calls DestroyCursor
DllCall("SetSystemCursor", "ptr", CursorHandle, "uint", SubStr(A_LoopField, 1, 5)) ; calls DestroyCursor
}
}
}
}
return
return
}
}


throw Exception("Error: Invalid file path or cursor name")
throw Exception("Error: Invalid file path or cursor name")
}
}


RestoreCursor() {
RestoreCursor() {
static SPI_SETCURSORS := 0x57
static SPI_SETCURSORS := 0x57
return DllCall("SystemParametersInfo", "uint", SPI_SETCURSORS, "uint", 0, "uint", 0, "uint", 0)
return DllCall("SystemParametersInfo", "uint", SPI_SETCURSORS, "uint", 0, "uint", 0, "uint", 0)
}
}