Hiding the mouse cursor/pointer

Share your advanced PureBasic knowledge/code with the community.
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Hiding the mouse cursor/pointer

Post by PB »

Original source: http://www.purebasic.fr/english/viewtopic.php?t=20564

I compacted netmaestro's source to make a standalone working procedure
that seems to work everywhere (not just your own app). For your own app,
you can just hide or show it with the ShowCursor API.

This code also uses a snippet from Trond (the "mask" bits) to replace using
CreateImage in netmaestro's original source. I also removed the need to
use an array of blank cursors. The result below probably looks ugly and
confusing to read, but that's the way I like to code: small and compact! :P

Note: It might seem like a waste to always load the cursors in the "yn=1"
block, but the fact is the user might switch cursor schemes on the fly, so
you shouldn't pre-load them outside of the procedure.

Code: Select all

Procedure MouseHide(yn)
  #OCR_HAND=32649 : #OCR_APPSTARTING=32650 : #OCR_HELP=32651
  Static mousehidden,tmpIDC_APPSTARTING,tmpIDC_ARROW,tmpIDC_CROSS,tmpIDC_HAND,tmpIDC_HELP,tmpIDC_IBEAM,tmpIDC_NO
  Static tmpIDC_SIZEALL,tmpIDC_SIZENESW,tmpIDC_SIZENS,tmpIDC_SIZENWSE,tmpIDC_SIZEWE,tmpIDC_UPARROW,tmpIDC_WAIT
  If (yn=0 And mousehidden=1) Or (yn=1 And mousehidden=0)
    mousehidden=yn
    If yn=1
      tmpIDC_APPSTARTING=CopyIcon_(LoadCursor_(0,#IDC_APPSTARTING)) : tmpIDC_ARROW=CopyIcon_(LoadCursor_(0,#IDC_ARROW))
      tmpIDC_CROSS=CopyIcon_(LoadCursor_(0,#IDC_CROSS)) : tmpIDC_HAND=CopyIcon_(LoadCursor_(0,#IDC_HAND))
      tmpIDC_HELP=CopyIcon_(LoadCursor_(0,#IDC_HELP)) : tmpIDC_IBEAM=CopyIcon_(LoadCursor_(0,#IDC_IBEAM))
      tmpIDC_NO=CopyIcon_(LoadCursor_(0,#IDC_NO)) : tmpIDC_SIZEALL=CopyIcon_(LoadCursor_(0,#IDC_SIZEALL))
      tmpIDC_SIZENESW=CopyIcon_(LoadCursor_(0,#IDC_SIZENESW)) : tmpIDC_SIZENS=CopyIcon_(LoadCursor_(0,#IDC_SIZENS))
      tmpIDC_SIZENWSE=CopyIcon_(LoadCursor_(0,#IDC_SIZENWSE)) : tmpIDC_SIZEWE=CopyIcon_(LoadCursor_(0,#IDC_SIZEWE))
      tmpIDC_UPARROW=CopyIcon_(LoadCursor_(0,#IDC_UPARROW)) : tmpIDC_WAIT=CopyIcon_(LoadCursor_(0,#IDC_WAIT))
      mask.w=%0000000010000000 : c=CreateCursor_(GetModuleHandle_(0),0,0,1,1,@mask,@mask+1)
      SetSystemCursor_(CopyIcon_(c),#OCR_APPSTARTING) : SetSystemCursor_(CopyIcon_(c),#OCR_NORMAL) : SetSystemCursor_(CopyIcon_(c),#OCR_CROSS)
      SetSystemCursor_(CopyIcon_(c),#OCR_HAND) : SetSystemCursor_(CopyIcon_(c),#OCR_HELP) : SetSystemCursor_(CopyIcon_(c),#OCR_IBEAM)
      SetSystemCursor_(CopyIcon_(c),#OCR_NO) : SetSystemCursor_(CopyIcon_(c),#OCR_SIZEALL) : SetSystemCursor_(CopyIcon_(c),#OCR_SIZENESW)
      SetSystemCursor_(CopyIcon_(c),#OCR_SIZENS) : SetSystemCursor_(CopyIcon_(c),#OCR_SIZENWSE) : SetSystemCursor_(CopyIcon_(c),#OCR_SIZEWE)
      SetSystemCursor_(CopyIcon_(c),#OCR_UP) : SetSystemCursor_(CopyIcon_(c),#OCR_WAIT) : DestroyCursor_(c)
    Else
      SetSystemCursor_(tmpIDC_APPSTARTING,#OCR_APPSTARTING) : SetSystemCursor_(tmpIDC_ARROW,#OCR_NORMAL)
      SetSystemCursor_(tmpIDC_CROSS,#OCR_CROSS) : SetSystemCursor_(tmpIDC_HAND,#OCR_HAND)
      SetSystemCursor_(tmpIDC_HELP,#OCR_HELP) : SetSystemCursor_(tmpIDC_IBEAM,#OCR_IBEAM)
      SetSystemCursor_(tmpIDC_NO,#OCR_NO) : SetSystemCursor_(tmpIDC_SIZEALL,#OCR_SIZEALL)
      SetSystemCursor_(tmpIDC_SIZENESW,#OCR_SIZENESW) : SetSystemCursor_(tmpIDC_SIZENS,#OCR_SIZENS)
      SetSystemCursor_(tmpIDC_SIZENWSE,#OCR_SIZENWSE) : SetSystemCursor_(tmpIDC_SIZEWE,#OCR_SIZEWE)
      SetSystemCursor_(tmpIDC_UPARROW,#OCR_UP) : SetSystemCursor_(tmpIDC_WAIT,#OCR_WAIT)
      DestroyIcon_(tmpIDC_APPSTARTING) : DestroyIcon_(tmpIDC_ARROW) : DestroyIcon_(tmpIDC_CROSS)
      DestroyIcon_(tmpIDC_HAND) : DestroyIcon_(tmpIDC_HELP) : DestroyIcon_(tmpIDC_IBEAM)
      DestroyIcon_(tmpIDC_NO) : DestroyIcon_(tmpIDC_SIZEALL) : DestroyIcon_(tmpIDC_SIZENESW)
      DestroyIcon_(tmpIDC_SIZENS) : DestroyIcon_(tmpIDC_SIZENWSE) : DestroyIcon_(tmpIDC_SIZEWE)
      DestroyIcon_(tmpIDC_UPARROW) : DestroyIcon_(tmpIDC_WAIT)
    EndIf
  EndIf
EndProcedure

MouseHide(1)
MessageRequester("Mouse hidden","Press Space to view it again")
MouseHide(0)