Vista / Win7 progress bar realtime updating fix

Share your advanced PureBasic knowledge/code with the community.
User avatar
luis
Addict
Addict
Posts: 3895
Joined: Wed Aug 31, 2005 11:09 pm
Location: Italy

Vista / Win7 progress bar realtime updating fix

Post by luis »

Sorry if something similar was already posted, I didn't find it.

Well, probably you noticed the fact win 7 / vista are using a buffering technique (kind of interpolation) to smooth the progress bar update especially when there are some big jumps in the update of the position value.

All nice and well, but when you go smootly from 1 to 100 for example, half of the time the bar is still at 50%-75% when you are closing your pretty window. Not nice.

This is a simple fix (sort of). Tested on win 7, should work on vista too I believe.

Code: Select all

EnableExplicit

#VISTA_PROGBAR_RESOLUTION = 100 ; this should be enough, higher multipliers = higher resolution

Macro VISTA_PROGBAR_FIX (topvalue)
topvalue * #VISTA_PROGBAR_RESOLUTION
EndMacro

Procedure SetVistaProgressBarValue  (nGadget, iValue)
; hack to partially fix the silly vista/win 7 behaviour
SetGadgetState(nGadget, iValue * #VISTA_PROGBAR_RESOLUTION)
SetGadgetState(nGadget, iValue * #VISTA_PROGBAR_RESOLUTION - 1)
SetGadgetState(nGadget, iValue * #VISTA_PROGBAR_RESOLUTION )
EndProcedure



; TEST PROGRAM

#DEFINE_FIX_ENABLED = 1 ; TRY THIS

Enumeration 
#WIN_MAIN
#PROGBAR
EndEnumeration


Procedure Main()
Protected iEvent, k
Protected iTopValue = 100 ; change this to try

If OpenWindow(#WIN_MAIN, 10, 10, 640, 480, "Main Window", #PB_Window_SystemMenu)

CompilerIf #DEFINE_FIX_ENABLED = 1

    ; you define min and max as usual, but include the top value in this macro
    ProgressBarGadget(#PROGBAR, 10,10, 600, 20, 1, VISTA_PROGBAR_FIX (iTopValue))
   
    For k = 1 To iTopValue                 
        SetVistaProgressBarValue (#PROGBAR, k)
        Delay(25)  ;  to simulate the processing time of each item       
    Next
    
CompilerElse
 
    ProgressBarGadget(#PROGBAR, 10,10, 600, 20, 1, iTopValue)
   
    For k = 1 To iTopValue                 
        SetGadgetState (#PROGBAR, k)
        Delay(25)  ;  to simulate the processing time of each item       
    Next

CompilerEndIf
   
    Delay(100)  ; at the end of the loop, to be on the safe side
                 
    FreeGadget(#PROGBAR) ; release the statusbar
     
    Repeat
        iEvent = WaitWindowEvent()       
    Until iEvent = #PB_Event_CloseWindow
   
EndIf

EndProcedure

Main()
EDIT: added a sample of the normal behaviour in case you haven't experienced it yet. See #DEFINE_FIX_ENABLED.
"Have you tried turning it off and on again ?"
SFSxOI
Addict
Addict
Posts: 2970
Joined: Sat Dec 31, 2005 5:24 pm
Location: Where ya would never look.....

Re: Vista / Win7 progress bar realtime updating fix

Post by SFSxOI »

Thanks for the post luis. :)

But I don't understand > "All nice and well, but when you go smootly from 1 to 100 for example, half of the time the bar is still at 50%-75% when you are closing your pretty window. Not nice."

I'm not seeing that here on my Windows 7 Ultimate machines.
The advantage of a 64 bit operating system over a 32 bit operating system comes down to only being twice the headache.
User avatar
luis
Addict
Addict
Posts: 3895
Joined: Wed Aug 31, 2005 11:09 pm
Location: Italy

Re: Vista / Win7 progress bar realtime updating fix

Post by luis »

Hi, it does not happen always and everywhere, but happens a lot. I found many posts in various forums lamenting this behavior under vista/7.

I saw the problem on my win7 computer and inside a VM with windows 7. Enough to try to fix it!

Have you tried the snippet above setting #DEFINE_FIX_ENABLED = 0 ?

If you didn't notice yet good for you ! But remember this thread for future reference :)

If you give enough time to the gui, and you don't close the window with the progress bar, eventually it will catch up.

EDIT: found another old thread about this (I believe):
http://www.purebasic.fr/english/viewtopic.php?p=256855

EDIT: another link about this:
http://social.msdn.microsoft.com/Forums ... cae21e0b32
Last edited by luis on Sat Dec 19, 2009 6:27 pm, edited 1 time in total.
"Have you tried turning it off and on again ?"
User avatar
chi
Addict
Addict
Posts: 1087
Joined: Sat May 05, 2007 5:31 pm
Location: Austria

Re: Vista / Win7 progress bar realtime updating fix

Post by chi »

thanks luis! exactly what i was looking for :wink:
Et cetera is my worst enemy
User avatar
luis
Addict
Addict
Posts: 3895
Joined: Wed Aug 31, 2005 11:09 pm
Location: Italy

Re: Vista / Win7 progress bar realtime updating fix

Post by luis »

Hi chi, the method have some shortcomings but it's the simplest way to accomplish this I have found.

According to this:
http://msdn.microsoft.com/en-us/library ... 85%29.aspx

there is not a style we can use to disable this under Aero....

If anyone discover a "clean" way to fix this mess please let us know (disabling themes for the specific gadget doesn't count) :wink:
"Have you tried turning it off and on again ?"
Post Reply