Flicker in example code from help file

Just starting out? Need help? Post your questions and find answers here.
firace
Addict
Addict
Posts: 946
Joined: Wed Nov 09, 2011 8:58 am

Flicker in example code from help file

Post by firace »

This example in the help file is flickering heavily for me.... (Windows 8.1, x86)

Code: Select all

 If OpenWindow(0, 0, 0, 300, 30, "Window mouse monitor", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
    TextGadget(0, 10, 6, 200, 20, "")
    
    Repeat
      Event = WaitWindowEvent(20) ; return at least every 20ms for an update
      
      SetGadgetText(0, "Window mouse position: " + Str(WindowMouseX(0)) + "," + Str(WindowMouseY(0)))       
    Until Event = #PB_Event_CloseWindow
  EndIf
Are there good / simple ways to fix that?
GPI
PureBasic Expert
PureBasic Expert
Posts: 1394
Joined: Fri Apr 25, 2003 6:41 pm

Re: Flicker in example code from help file

Post by GPI »

I would sepearte this in a fixed text and a variable one:

Code: Select all

If OpenWindow(0, 0, 0, 300, 30, "Window mouse monitor", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
    TextGadget(0, 10, 6, 150, 20, "Window mouse position: ")
    TextGadget(1,160,6,100,20,"")
    Repeat
      Event = WaitWindowEvent(20) ; return at least every 20ms for an update
     
      SetGadgetText(1,  Str(WindowMouseX(0)) + "," + Str(WindowMouseY(0)))       
    Until Event = #PB_Event_CloseWindow
  EndIf
or you use a read-ony borderless string-gadget

Code: Select all

If OpenWindow(0, 0, 0, 300, 30, "Window mouse monitor", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
    StringGadget(0, 10, 6, 200, 20, "",#PB_String_BorderLess|#PB_String_ReadOnly)
   
    Repeat
      Event = WaitWindowEvent(20) ; return at least every 20ms for an update
     
      SetGadgetText(0, "Window mouse position: " + Str(WindowMouseX(0)) + "," + Str(WindowMouseY(0)))       
    Until Event = #PB_Event_CloseWindow
  EndIf
edit: or the best methode:

Code: Select all

 If OpenWindow(0, 0, 0, 300, 30, "Window mouse monitor", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
    TextGadget(0, 10, 6, 200, 20, "")
    
    Define oldx,oldy,x,y
    Repeat
      Event = WaitWindowEvent(20) ; return at least every 20ms for an update
      x=WindowMouseX(0)
      y=WindowMouseY(0)
      If x<>oldx Or y<>oldy
        SetGadgetText(0, "Window mouse position: " + Str(x) + "," + Str(y))       
        oldx=x
        oldy=y
      EndIf
      
    Until Event = #PB_Event_CloseWindow
  EndIf
only change the text, when the coordinates have changed.
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Re: Flicker in example code from help file

Post by ts-soft »

Code: Select all

EnableExplicit

Define X, Y, oldX, oldY

If OpenWindow(0, 0, 0, 300, 30, "Window mouse monitor", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  TextGadget(0, 10, 6, 200, 20, "")
  
  AddWindowTimer(0, 1, 100)
  
  Repeat
    Select WaitWindowEvent()
      Case #PB_Event_CloseWindow
        Break
      Case #PB_Event_Timer
        If EventTimer() = 1
          X = WindowMouseX(0)
          Y = WindowMouseY(0)
          If X <> oldX Or Y <> oldY
            SetGadgetText(0, "Window mouse position: " + Str(X) + "," + Str(Y))
            oldX = X
            oldY = Y
          EndIf
        EndIf
    EndSelect
  ForEver
EndIf
No code outside a event in eventloop and no flickering at all :wink:
PureBasic 5.73 | SpiderBasic 2.30 | Windows 10 Pro (x64) | Linux Mint 20.1 (x64)
Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.
Image
firace
Addict
Addict
Posts: 946
Joined: Wed Nov 09, 2011 8:58 am

Re: Flicker in example code from help file

Post by firace »

Many thanks for the various approaches provided!
Mesa
Enthusiast
Enthusiast
Posts: 433
Joined: Fri Feb 24, 2012 10:19 am

Re: Flicker in example code from help file

Post by Mesa »

@firace: This is not the help example ! ! !
The help example is

Code: Select all

  If OpenWindow(0, 0, 0, 300, 30, "Position of the mouse on the desktop", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
    TextGadget(0, 10, 6, 200, 20, "")
    
    Repeat
      Event = WaitWindowEvent(20)
      
      If Event = 0 ; No more events in the queue, so let's display the mouse coordinates
        SetGadgetText(0, "Coordinates: " + Str(DesktopMouseX()) + "," + Str(DesktopMouseY()))  
      EndIf
       
    Until Event = #PB_Event_CloseWindow
  EndIf
The line

Code: Select all

 If Event = 0  ; No more events in the queue, so let's display the mouse coordinates
is very very important.

The example has no sense without that line (i'm the creator of this example in the french help team)

You should not have flickering with this line (i hope :wink: ).

M.
firace
Addict
Addict
Posts: 946
Joined: Wed Nov 09, 2011 8:58 am

Re: Flicker in example code from help file

Post by firace »

@Mesa: yes it is taken straight from the help file (from WindowMouseX, not DesktopMouseX)

And unfortunately even your example flickers for me :(

GPI's second snippet does eliminate the flicker, though.
User avatar
Vera
Addict
Addict
Posts: 858
Joined: Tue Aug 11, 2009 1:56 pm
Location: Essen (Germany)

Re: Flicker in example code from help file

Post by Vera »

Mesa wrote:(i'm the creator of this example in the french help team)
Both examples exist since 2004 and there is no specific french help team.
Post Reply