Temporarily hide all system cursors

Share your advanced PureBasic knowledge/code with the community.
Mistrel
Addict
Addict
Posts: 3415
Joined: Sat Jun 30, 2007 8:04 pm

Temporarily hide all system cursors

Post by Mistrel »

Keep in mind that this function does not hide the cursor per se. Instead it loads all current system cursors into a cache and replaces them blank cursor, to be restored later.

Keep in mind that this is NOT the correct way to hide the user's mouse but it's useful in a pinch where Win32 SetCapture is not available such as when the hWnd not owned by your process. See the SetCapture documentation:
Sets the mouse capture to the specified window belonging to the current thread.
Despite finding a constant for IDC_ALTSELECT, I was not able to find a way to hide the "Alternate Select" cursor (the right-facing cursor typically used in code editor gutters).

Any help finding this constant would be appreciated.

h.HideCursor.pb:

Code: Select all

#IDC_PEN=32631
#IDC_WAITCD=32663
#IDC_ALTSELECT=32501

Structure Glob_HideCursor
  Array CursorIdentifiers.i(19)
  Array CurcorCache.i(19)
  InvisibleCursor.i
EndStructure

Global Glob_HideCursor.Glob_HideCursor

Declare HideCursor(Hidden=#True, UpdateCache=#False)

If Not Glob_HideCursor\InvisibleCursor
  ;/ Create an invisible cursor
  AndMask=AllocateMemory(32*4)
  FillMemory(AndMask,32*4,$FF)
  XorMask=AllocateMemory(32*4)
  
  Glob_HideCursor\InvisibleCursor=CreateCursor_(0,0,0,32,32,AndMask,XorMask)
  
  FreeMemory(AndMask)
  FreeMemory(XorMask)
  
  ;/ Build an array of common system cursor identifiers
  Restore CursorIdentifiers
  For i=1 To 19
    Read.i Glob_HideCursor\CursorIdentifiers(i-1)
  Next i
EndIf

DataSection
   CursorIdentifiers:
   Data.i #IDC_APPSTARTING	
   Data.i #IDC_ARROW
   Data.i #IDC_CROSS
   Data.i #IDC_HAND
   Data.i #IDC_HELP
   Data.i #IDC_IBEAM
   Data.i #IDC_ICON
   Data.i #IDC_NO
   Data.i #IDC_SIZE
   Data.i #IDC_SIZEALL
   Data.i #IDC_SIZENESW
   Data.i #IDC_SIZENS
   Data.i #IDC_SIZENWSE
   Data.i #IDC_SIZEWE
   Data.i #IDC_UPARROW
   Data.i #IDC_WAIT
   Data.i #IDC_PEN
   Data.i #IDC_WAITCD
   Data.i #IDC_ALTSELECT
EndDataSection

EnableExplicit
XIncludeFile #PB_Compiler_File+"\..\"+"lib.HideCursor.pb"
DisableExplicit
lib.HideCursor.pb:

Code: Select all

Procedure HideCursor(Hidden=#True, UpdateCache=#False)
  Protected hCursor
  Protected i
  
  ;/ Inititalize at first call or update cache on request
  If UpdateCache Or Not Glob_HideCursor\CurcorCache(0)
    ;/ Store a copy of cursors currently in use
    For i=1 To 19
      ;/ Destroy any previously cached cursors
      If Glob_HideCursor\CurcorCache(i-1)
        DestroyCursor_(Glob_HideCursor\CurcorCache(i-1))
      EndIf
      
      hCursor=LoadCursor_(0,Glob_HideCursor\CursorIdentifiers(i-1))
      Glob_HideCursor\CurcorCache(i-1)=CopyImage_(hCursor,2,0,0,0)
      
      ;/ Destroy the temporary cursor
      DestroyCursor_(hCursor)
    Next i
  EndIf
  
  For i=1 To 19
    If Hidden
      hCursor=CopyImage_(Glob_HideCursor\InvisibleCursor,2,0,0,0)
    Else
      hCursor=CopyImage_(Glob_HideCursor\CurcorCache(i-1),2,0,0,0)
    EndIf
    
    SetSystemCursor_(hCursor,Glob_HideCursor\CursorIdentifiers(i-1))
  Next i
EndProcedure
Example:

Code: Select all

;/ Hide the cursor for two seconds
HideCursor(#True)
Delay(2000)
HideCursor(#False)
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Re: Temporarily hide all system cursors

Post by PB »

> it loads all current system cursors into a cache and replaces
> them blank cursor, to be restored later

Just be aware that if your app crashes while the mouse is gone,
then the mouse pointer is gone for the entire PC until the user
reboots or logs off/on again. Try it by running your example
with "HideCursor(#False)" removed, or kill the app in Task
Manager during the delay.
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
Mistrel
Addict
Addict
Posts: 3415
Joined: Sat Jun 30, 2007 8:04 pm

Re: Temporarily hide all system cursors

Post by Mistrel »

It you're comfortable with keyboard navigation then you can get it back by opening "Mouse Properties" in the Control Panel to reset your cursor.

Yes, this is indeed dangerous and completely irresponsible. But like I said, it's nice in a pinch. :wink:

Still hoping to find a constant for the "Alternate Select" cursor.
Post Reply