How to hide mouse cursor?
Posted: Sat Mar 18, 2006 12:19 pm
I want to know to hide mouse cursor.
I've found it. but I din't find.
I've found it. but I din't find.
http://www.purebasic.com
https://www.purebasic.fr/english/
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
ohPB 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.
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
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.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.
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)
Don't you guys have keyboards? :roll:But a logoff and logon restores it; no need to fully reboot.