[SOLVED] Switch mouse cursor from arrow to hand

Just starting out? Need help? Post your questions and find answers here.
Joubarbe
Enthusiast
Enthusiast
Posts: 744
Joined: Wed Sep 18, 2013 11:54 am
Location: France

[SOLVED] Switch mouse cursor from arrow to hand

Post by Joubarbe »

Is there any way to change (temporarily) the mouse cursor while using hardware mouse and a windowed screen? (on Windows)

Code: Select all

SetClassLong_(hWnd, #GCL_HCURSOR, LoadCursor_(0, #IDC_HAND))
This doesn't work.

SOLVED:

Code: Select all

SetClassLongPtr_(WindowID(0), #GCL_HCURSOR, 0)
SetCursor_(LoadCursor_(0, #IDC_HAND))
Last edited by Joubarbe on Tue Dec 09, 2025 6:23 pm, edited 1 time in total.
Axolotl
Addict
Addict
Posts: 899
Joined: Wed Dec 31, 2008 3:36 pm

Re: Switch mouse cursor from arrow to hand

Post by Axolotl »

Please try

Code: Select all

setcursor_(hCursor)
BTW: there are a lot of examples around. Just search for setcursor_
Just because it worked doesn't mean it works.
PureBasic 6.04 (x86) and <latest stable version and current alpha/beta> (x64) on Windows 11 Home. Now started with Linux (VM: Ubuntu 22.04).
Joubarbe
Enthusiast
Enthusiast
Posts: 744
Joined: Wed Sep 18, 2013 11:54 am
Location: France

Re: Switch mouse cursor from arrow to hand

Post by Joubarbe »

The problem is that it's very glitchy when used with a screen:

Code: Select all

If InitSprite() = #Null
  MessageRequester("Error", "InitSprite failed.", #PB_MessageRequester_Error)
  End
EndIf

#_WINDOW_WIDTH = 720
#_WINDOW_HEIGHT = 640
OpenWindow(0, 0, 0, #_WINDOW_WIDTH, #_WINDOW_HEIGHT, "", #PB_Window_ScreenCentered | #PB_Window_SystemMenu)
OpenWindowedScreen(WindowID(0), 0, 0, #_WINDOW_WIDTH, #_WINDOW_HEIGHT)

CreateSprite(0, 100, 100, #PB_Sprite_AlphaBlending)

Repeat
  
  Repeat
    Define event = WindowEvent()
    
    Select event
      Case #PB_Event_CloseWindow
        End
    EndSelect
  Until event = #Null
  
  FlipBuffers() 
  ClearScreen(#Black)
  
  If WindowMouseX(0) >= 0 And WindowMouseX(0) < 100 And WindowMouseY(0) >= 0 And WindowMouseY(0) < 100
    DisplayTransparentSprite(0, 0, 0, 255, #Green)
    SetCursor_(LoadCursor_(0, #IDC_HAND))
  Else
    DisplayTransparentSprite(0, 0, 0, 255, #Gray)
    SetCursor_(LoadCursor_(0, #IDC_ARROW))
  EndIf
  
  Delay(1)
  
ForEver
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 5013
Joined: Sun Apr 12, 2009 6:27 am

Re: Switch mouse cursor from arrow to hand

Post by RASHAD »

Code: Select all

If InitSprite() = #Null
  MessageRequester("Error", "InitSprite failed.", #PB_MessageRequester_Error)
  End
EndIf

#_WINDOW_WIDTH = 720
#_WINDOW_HEIGHT = 640
OpenWindow(0, 0, 0, #_WINDOW_WIDTH, #_WINDOW_HEIGHT, "", #PB_Window_ScreenCentered | #PB_Window_SystemMenu)
OpenWindowedScreen(WindowID(0), 0, 0, #_WINDOW_WIDTH, #_WINDOW_HEIGHT)

CreateSprite(0, 100, 100, #PB_Sprite_AlphaBlending)

Repeat
  
  Repeat
    Define event = WindowEvent()
    
    Select event
      Case #PB_Event_CloseWindow
        End
    EndSelect
  Until event = #Null
  
  FlipBuffers() 
  ClearScreen(#Black)
  
  If WindowMouseX(0) >= 0 And WindowMouseX(0) < 100 And WindowMouseY(0) >= 0 And WindowMouseY(0) < 100
    DisplayTransparentSprite(0, 0, 0, 255, #Green)
    SetClassLongPtr_(WindowID(0), #GCL_HCURSOR, 0)
    SetCursor_(LoadCursor_(0, #IDC_HAND))
  Else
    DisplayTransparentSprite(0, 0, 0, 255, #Gray)
    SetClassLongPtr_(WindowID(0), #GCL_HCURSOR, 0)
    SetCursor_(LoadCursor_(0, #IDC_ARROW))
  EndIf
  
  Delay(1)
  
ForEver

Egypt my love
Joubarbe
Enthusiast
Enthusiast
Posts: 744
Joined: Wed Sep 18, 2013 11:54 am
Location: France

Re: Switch mouse cursor from arrow to hand

Post by Joubarbe »

Thank you RASHAD! :)
Post Reply