How to hide mouse cursor?

Just starting out? Need help? Post your questions and find answers here.
xenopsyx
User
User
Posts: 10
Joined: Wed Mar 01, 2006 3:10 pm

How to hide mouse cursor?

Post by xenopsyx »

I want to know to hide mouse cursor.
I've found it. but I din't find.
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post 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
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Post 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. :(
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
thefool
Always Here
Always Here
Posts: 5875
Joined: Sat Aug 30, 2003 5:58 pm
Location: Denmark

Post by thefool »

PB, do you still want mouse events? If not then just move the cursor to an out-of-screen position.
xenopsyx
User
User
Posts: 10
Joined: Wed Mar 01, 2006 3:10 pm

Thank you Trond

Post by xenopsyx »

It is good to me.
Thanks :lol:
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Post 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.
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
thefool
Always Here
Always Here
Posts: 5875
Joined: Sat Aug 30, 2003 5:58 pm
Location: Denmark

Post 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?
User avatar
Joakim Christiansen
Addict
Addict
Posts: 2452
Joined: Wed Dec 22, 2004 4:12 pm
Location: Norway
Contact:

Post 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
SFSxOI
Addict
Addict
Posts: 2970
Joined: Sat Dec 31, 2005 5:24 pm
Location: Where ya would never look.....

Post 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.
Sparkie
PureBatMan Forever
PureBatMan Forever
Posts: 2307
Joined: Tue Feb 10, 2004 3:07 am
Location: Ohio, USA

Post 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

What goes around comes around.

PB 5.21 LTS (x86) - Windows 8.1
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Post 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.
naw
Enthusiast
Enthusiast
Posts: 573
Joined: Fri Apr 25, 2003 4:57 pm

Post 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.
Ta - N
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Post 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)           
BERESHEIT
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Post 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.
User avatar
Fluid Byte
Addict
Addict
Posts: 2336
Joined: Fri Jul 21, 2006 4:41 am
Location: Berlin, Germany

Post by Fluid Byte »

But a logoff and logon restores it; no need to fully reboot.
Don't you guys have keyboards? :roll:
Windows 10 Pro, 64-Bit / Whose Hoff is it anyway?
Post Reply