Page 1 of 2

How to hide mouse cursor?

Posted: Sat Mar 18, 2006 12:19 pm
by xenopsyx
I want to know to hide mouse cursor.
I've found it. but I din't find.

Posted: Sat Mar 18, 2006 12:51 pm
by Trond

Code: Select all

OpenWindow(0, 0, 0, 400, 300, #PB_Window_ScreenCentered | #PB_Window_SystemMenu, "")
ShowCursor_(0)


Repeat
  Select WaitWindowEvent()
    Case #PB_Event_CloseWindow
      Break
  EndSelect
ForEver

Posted: Sat Mar 18, 2006 1:15 pm
by PB
@Trond: Interesting... it hides the cursor if it's over the window, but not
anywhere else. Do you know a way to do it no matter where it is? I've
never been able to find a way. :(

Posted: Sat Mar 18, 2006 1:33 pm
by thefool
PB, do you still want mouse events? If not then just move the cursor to an out-of-screen position.

Thank you Trond

Posted: Sat Mar 18, 2006 1:43 pm
by xenopsyx
It is good to me.
Thanks :lol:

Posted: Sat Mar 18, 2006 1:58 pm
by PB
> PB, do you still want mouse events? If not then just move the cursor to an
> out-of-screen position.

That's what I've been doing. :) I always wanted a better way to just hide the
cursor though, than doing that, when my app doesn't have the focus.

Posted: Sat Mar 18, 2006 3:45 pm
by thefool
PB wrote: That's what I've been doing. :) I always wanted a better way to just hide the
cursor though, than doing that, when my app doesn't have the focus.
oh :)
Well i searched google, and it seems like people do use that method.
However im also wondering.. What about smashing a global hook and use ShowCursor_(0) at them while out of your window?

Posted: Sat Mar 18, 2006 5:01 pm
by Joakim Christiansen
Here is my example to hide the cursor when it's inside the window:
http://www.purebasic.fr/english/viewtopic.php?t=19309

Posted: Sat Mar 18, 2006 10:23 pm
by SFSxOI
I've wanted to know also. If your doing anything with custom cursors in DirectX/Direct Input, the system cursor gets in the way because the system cursor has different things applied to it that a custom DirectX cursor doesn't ...like acceleration for example. DirectX/ Direct Input doesn't care about cursors, so the problem comes in when the system cursor is still available because if it is no matter what you do in Direct Input the mouse is still affected by the system cursor settings. I've been told by a buncha VB people that you need to hide the system cursor to keep the interaction from happening - but its kinda strange also that none of them have been able to tell me how to get rid of the system cursor. Even when its hidden in a window the mouse is still affected by its settings. So, how do you just turn the system cursor off and on when you want to is a question 'cause just hiding it doesn't do the trick all the time.

Posted: Sun Mar 19, 2006 12:07 am
by Sparkie
Not sure if this fits your needs but here's some code that locks and hides the cursor to the upper left corner of the screen.

Press [Esc] to quit or wait 10 seconds and it auto-quits. ;)

Code: Select all

;...Our tiny, borderless 1 x 1 Window
If OpenWindow(0, 0, 0, 1, 1, "Test", #PB_Window_BorderLess)
  
  ;...Add an [Esc] keyress to quit
  AddKeyboardShortcut(0, #PB_Shortcut_Escape, 1)
  
  ;...I'll use a failsafe timer to quit in 10 seconds
  StartTime = ElapsedMilliseconds()
  
  ;...Get the window rect
  GetWindowRect_(WindowID(0), @cursorRect.RECT)
  
  ;...Confine the cursor to our 1 pixel window
  ClipCursor_(cursorRect)
  
  ;...Hide the cursor
  cursor = ShowCursor_(#False)
  
  quit = #False
  Repeat
    ;...I'll use a delay to enable our failsafe countdown to quit
    event = WaitWindowEvent(100)
     
    ;...Our failsafe to quit
    If ElapsedMilliseconds() - StartTime > 10000
      quit = #True
    EndIf
    
    ;...Any key press will also end app
    If event = #PB_Event_Menu
      quit = #True
    EndIf
    
  Until quit
  ;...Free the mouse
  ClipCursor_(0)
EndIf
End


Posted: Thu Sep 27, 2007 11:35 am
by PB
So, did anyone find out a way to do it without just moving the mouse to a
corner of the screen? :) I've been trying, but no luck so far. Because if I move
it to a corner, it invariably makes a slide-open window appear (such as the
PureBasic IDE's procedure list, or Media Player Classic's seek bar, etc).

Note: My app has no window, so I need to hide the cursor no matter which
window has the focus at the time.

Posted: Thu Sep 27, 2007 12:29 pm
by naw
Is it possible to change the appearance of the System Cursor to perhaps a transparent block, then it doesnt matter where it is 'cos it'll be invisible.

Posted: Fri Sep 28, 2007 7:30 pm
by netmaestro
naw wrote:Is it possible to change the appearance of the System Cursor to perhaps a transparent block, then it doesnt matter where it is 'cos it'll be invisible.
This idea sounds simple enough yet it is not trivial at all, which might be OK except for the fact that it doesn't work very well either. Firstly, there are 14 system cursors and they would all have to be replaced by the invisible one in order for the mouse pointer not to show. Before they can be replaced they have to be copied so that they can be restored when you're done. The SetSystemCursor api destroys the current system cursor when you set a new one, so you must always be working with a set of copies. This is a bit involved but quite doable. However, there are a couple of big downsides: One, if anything happens to your program and it doesn't end normally, the user would have to restart his machine to get his cursors back. Trust me, you won't be popular if this occurs. Two, you can only affect system cursors, not custom ones that other programs are using. For example in the PB IDE there is a backwards arrow used when you hover over the line numbers column. You can't get rid of this using this method. The cursor would only be invisible most of the time, rather than all the time.

With these serious drawbacks I can hardly recommend this method, but for those interested in seeing it in action you can run this code.

** END WITH THE ESCAPE KEY ONLY **

Don't kill this demo with the debugger or you'll have to restart your computer to get your cursors back!

Code: Select all

; Fill in a couple of missing constants

#OCR_HAND        = 32649
#OCR_APPSTARTING = 32650
#OCR_HELP        = 32651

; 1) Make copies of all the system cursors

tmpIDC_APPSTARTING = CopyIcon_(LoadCursor_(#Null, #IDC_APPSTARTING))
tmpIDC_ARROW       = CopyIcon_(LoadCursor_(#Null, #IDC_ARROW))
tmpIDC_CROSS       = CopyIcon_(LoadCursor_(#Null, #IDC_CROSS))  
tmpIDC_HAND        = CopyIcon_(LoadCursor_(#Null, #IDC_HAND))  
tmpIDC_HELP        = CopyIcon_(LoadCursor_(#Null, #IDC_HELP)) 
tmpIDC_IBEAM       = CopyIcon_(LoadCursor_(#Null, #IDC_IBEAM))
tmpIDC_NO          = CopyIcon_(LoadCursor_(#Null, #IDC_NO))
tmpIDC_SIZEALL     = CopyIcon_(LoadCursor_(#Null, #IDC_SIZEALL))
tmpIDC_SIZENESW    = CopyIcon_(LoadCursor_(#Null, #IDC_SIZENESW))
tmpIDC_SIZENS      = CopyIcon_(LoadCursor_(#Null, #IDC_SIZENS))
tmpIDC_SIZENWSE    = CopyIcon_(LoadCursor_(#Null, #IDC_SIZENWSE))
tmpIDC_SIZEWE      = CopyIcon_(LoadCursor_(#Null, #IDC_SIZEWE))
tmpIDC_UPARROW     = CopyIcon_(LoadCursor_(#Null, #IDC_UPARROW))
tmpIDC_WAIT        = CopyIcon_(LoadCursor_(#Null, #IDC_WAIT))       
                               
; 2) Create an invisible cursor

CreateImage(0, 32,32, #PB_Image_DisplayFormat) 
CreateImage(1, 32, 32, #PB_Image_DisplayFormat) 

StartDrawing(ImageOutput(0)) 
 Box(0,0,32,32,#White)
StopDrawing() 

icoInf.ICONINFO 
icoInf\fIcon = #False 
icoInf\hbmMask = ImageID(0) 
icoInf\hbmColor = ImageID(1) 

invisiblecursor = CreateIconIndirect_(icoInf)

Dim blankcursors(14)
For i=1 To 14
  blankcursors(i) = CopyIcon_(invisiblecursor)
Next

; 3) Set all the system cursors to the invisible one

SetSystemCursor_(blankcursors(1),  #OCR_APPSTARTING)
SetSystemCursor_(blankcursors(2),  #OCR_NORMAL)
SetSystemCursor_(blankcursors(3),  #OCR_CROSS)
SetSystemCursor_(blankcursors(4),  #OCR_HAND)
SetSystemCursor_(blankcursors(5),  #OCR_HELP)
SetSystemCursor_(blankcursors(6),  #OCR_IBEAM)
SetSystemCursor_(blankcursors(7),  #OCR_NO)
SetSystemCursor_(blankcursors(8),  #OCR_SIZEALL)
SetSystemCursor_(blankcursors(9),  #OCR_SIZENESW)
SetSystemCursor_(blankcursors(10), #OCR_SIZENS)
SetSystemCursor_(blankcursors(11), #OCR_SIZENWSE)
SetSystemCursor_(blankcursors(12), #OCR_SIZEWE)
SetSystemCursor_(blankcursors(13), #OCR_UP)
SetSystemCursor_(blankcursors(14), #OCR_WAIT)
                 
; 4) Loop until you get the keypress you want

Repeat 
    Delay(1)
Until GetAsyncKeyState_(#VK_ESCAPE) & 32768

; 5) Reset all the system cursors to their original images

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)
                                   
; 6) Destroy temporary cursors
                        
For i=1 To 14
  DestroyIcon_(blankcursors(i))
Next
                                  
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)           

Posted: Sat Sep 29, 2007 12:49 am
by PB
> Don't kill this demo with the debugger or you'll have to restart your
> computer to get your cursors back!

Hehehe, I did a test app which replaced the arrow system cursor and quit my
app and then had this problem, ie. no cursor anymore. But a logoff and logon
restores it; no need to fully reboot.

Posted: Sat Sep 29, 2007 1:13 am
by Fluid Byte
But a logoff and logon restores it; no need to fully reboot.
Don't you guys have keyboards? :roll: