Hide cursor when inside window (api)

Share your advanced PureBasic knowledge/code with the community.
User avatar
Joakim Christiansen
Addict
Addict
Posts: 2452
Joined: Wed Dec 22, 2004 4:12 pm
Location: Norway
Contact:

Hide cursor when inside window (api)

Post by Joakim Christiansen »

Code updated For 5.20+

Just a simple example that hides the mouse cursor when it's inside a window :D

Code: Select all

    OpenWindow(0,0,0,320,240,"Test",#PB_Window_SystemMenu|#PB_Window_ScreenCentered)

    Repeat
      If WindowMouseX(0)=-1 Or WindowMouseY(0)=-1
        If ShowMouse = -1
          ShowMouse = ShowCursor_(#True)
        EndIf
      Else
        If ShowMouse = 0
          ShowMouse = ShowCursor_(#False)
        EndIf
      EndIf
     
      StartDrawing(WindowOutput(0))
        Circle(WindowMouseX(0),WindowMouseY(0),5,#Red)
      StopDrawing()
    Until WindowEvent() = #PB_Event_CloseWindow

ABBKlaus
Addict
Addict
Posts: 1143
Joined: Sat Apr 10, 2004 1:20 pm
Location: Germany

Post by ABBKlaus »

and even better with a little delay :twisted:

Code: Select all

OpenWindow(0,0,0,320,240,#PB_Window_SystemMenu|#PB_Window_ScreenCentered,"Test") 

Repeat 
  If WindowMouseX(0)=-1 Or WindowMouseY(0)=-1 
    If ShowMouse = -1 
      ShowMouse = ShowCursor_(#True) 
    EndIf 
  Else 
    If ShowMouse = 0 
      ShowMouse = ShowCursor_(#False) 
    EndIf 
  EndIf 
  
  StartDrawing(WindowOutput(0)) 
    Circle(WindowMouseX(0),WindowMouseY(0),5,#Red) 
  StopDrawing() 
Until WaitWindowEvent(1) = #PB_Event_CloseWindow 
Dräc
Enthusiast
Enthusiast
Posts: 150
Joined: Sat Oct 09, 2004 12:10 am
Location: Toulouse (France)
Contact:

Post by Dräc »

Because previous codes assume that the state of the internal cursor count is 0 when starting the loop, here a little enhancement:

ShowCursor(Flag): Flag=#False --> Hide cursor, Flag=#True --> Show cursor

Works what ever the number of time that ShowCursor_ was called before.

But I haven’t find another way then calling several time ShowCursor_ to adapt the internal cursor count.

PLEASE, before trying, suppress the ‘_’ in the third line

Code: Select all

If Flag <_> CursorState
Made it unless the code does appared correctly (someone knows how to disable the '<' character under BBcode?)

Code: Select all

Procedure ShowCursor(Flag); Flag=#True/#False, show/hide cursor
  Static CursorState
  If Flag <_> CursorState
	  CursorState = ShowCursor_(Flag)
	  If (CursorState >=0 And Flag = 0) Or (CursorState <0 And Flag)
	    Repeat
	    Until ShowCursor_(Flag) =Flag-1    
	  EndIf
	  CursorState =Flag
  EndIf
EndProcedure

OpenWindow(0,0,0,320,240,"Test",#PB_Window_SystemMenu|#PB_Window_ScreenCentered)

For i=0 To 10000
  ShowCursor_(#True)
Next

Repeat
  If WindowMouseX(0)=-1 Or WindowMouseY(0)=-1
    ShowCursor(#True)
  Else
    ShowCursor(#False)
  EndIf
 
  StartDrawing(WindowOutput(0))
    Circle(WindowMouseX(0),WindowMouseY(0),5,#Red)
  StopDrawing()
Until WaitWindowEvent(1) = #PB_Event_CloseWindow
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post by Trond »

Dräc, disable html in your profile.

Here's yet another way (using a transparent 1x1 pixel cursor):

Code: Select all

OpenWindow(0,0,0,320,240,"Test")

Mask.w = %0000000010000000
hCur = CreateCursor_(GetModuleHandle_(0), 0, 0, 1, 1, @Mask, @Mask+1)
SetClassLong_(WindowID(0), #GCL_HCURSOR, hCur)

Repeat
  StartDrawing(WindowOutput(0))
    Circle(WindowMouseX(0),WindowMouseY(0),5,#Red)
  StopDrawing() 
Until WaitWindowEvent(1) = #PB_Event_CloseWindow

DestroyCursor_(hCur)
Dräc
Enthusiast
Enthusiast
Posts: 150
Joined: Sat Oct 09, 2004 12:10 am
Location: Toulouse (France)
Contact:

Post by Dräc »

Trond wrote:Dräc, disable html in your profile.
Right, thanks
Post Reply