Announcement

Collapse

Forum Guidelines

This forum is for finished source code that is working properly. If you have questions about this or any other source code, please post it in one of the Discussion Forums, not here.
See more
See less

ProgressBar Pseudo-Class

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • ProgressBar Pseudo-Class

    Just for the intertainment i'm trying to implement a class-like setup in PowerBasic. The nice thing about such a construction is that you can avoid global variables. The member-functions share the same data. In the following example the handles and the ID's of the progressbars are saved and protected inside the class. The user never see the handles (allthough he can get them by the ProgressBarGetHandle member-function).

    First i show the example. In the next post the implementation of the class is given (progress.inc ).

    Regards
    Peter

    Code:
    #compile exe
    #dim all
    #register none
    #include "win32api.inc"
    #include "progress.inc"
     
    declare callback function DlgProc
     
    %ID_BUT_TEST     = 1000
    %ID_PROGRESS     = 1001
    %NUM_BARS        = 75
    %MAX_RANGE       = 150
     
    function pbmain
      
        local hDlg as long
     
        dialog new 0, "A lot of ProgressBars!",,,400, 450, %WS_SYSMENU or %WS_MINIMIZEBOX or %DS_CENTER to hDlg
    	control add button, hDlg, %ID_BUT_TEST, "Test", 350, 5, 40, 14
        dialog show modal hDlg call DlgProc
     
    end function
     
    callback function DlgProc
     
        local id as long, j as long, r as long, h as long
        local s as string
     
        select case cbMsg
        case %WM_INITDIALOG
    	for id = 1 to %NUM_BARS
                ProgressBarCreate cbhndl, id, 5, 5 + (id-1)*9, 500, 7, %WS_VISIBLE
                ProgressBarSetRange id, 0, %MAX_RANGE
    	    ProgressBarSetStep id, sqr(id)
       	    ProgressBarSetBarColor id, %Red
    	    ProgressBarSetBkColor id, %LtGray'%White
    	next id	
     
    	ProgressBarCreate cbhndl, %ID_PROGRESS, 5, 682, 500, 20, %WS_VISIBLE
    	ProgressBarSetRange %ID_PROGRESS, 0, %MAX_RANGE
    	ProgressBarSetStep %ID_PROGRESS, 1
    	ProgressBarSetBarColor %ID_PROGRESS, %Yellow
    	ProgressBarSetBkColor %ID_PROGRESS, %Black
     
        case %WM_CTLCOLORDLG
             function = GetStockObject(%WHITE_BRUSH)
     
        case %WM_COMMAND
            select case lowrd(cbwParam)
     	    case %ID_BUT_TEST
    		control disable cbhndl, %ID_BUT_TEST
    		for j = 1 to %MAX_RANGE
    		    ProgressBarStepIt %ID_PROGRESS
    		    for id = 1 to %NUM_BARS
     			ProgressBarStepIt id
      		    next id
    		next j
    		for id = 1 to %NUM_BARS
    		    ProgressBarSetPos id, 0
    		next id
    		ProgressBarSetPos %ID_PROGRESS, 0
     
    		control enable cbhndl, %ID_BUT_TEST
            end select
        end select
    end function

  • #2
    Code:
    ' PROGRESS.INC
    ' ProgressBar Pseudo-Class
      
    #if not %def(%ODT_HEADER)
    #include "commctrl.inc"
    #endif
      
    %PBM_SETBARCOLOR = %WM_USER + 9
    %PBM_SETBKCOLOR  = %CCM_SETBKCOLOR
      
    %MAX_BARS = 100   ' Maximal number of progressbars
      
    ' Member-function ID's
    %CREATE      = 0
    %SETRANGE    = 1
    %STEPIT      = 2 
    %SETSTEP     = 3 
    %SETPOS      = 4 
    %HIDE        = 5 
    %SHOW        = 6 
    %MOVE        = 7 
    %SETBARCOLOR = 8
    %SETBKCOLOR  = 9
    %GETHANDLE   = 10
      
    ' "Class"
    function CProgressBar(byval id_func as long, byval id_bar as long, _
                                                 byval param1 as long, _
                                                 byval param2 as long, _
                                                 byval param3 as long, _
                                                 byval param4 as long, _
                                                 byval param5 as long, _
                                                  byval param6 as long) as long
        ' Protected member variables
        static m_n as long             ' Number of progressbars
        static m_hParent as long
        dim m_hProgress(%MAX_BARS-1) as static long
        dim m_ID(%MAX_BARS-1) as static long
      
        local m_hInst as long
        local m_x as long, m_y as long
        local m_xx as long, m_yy as long
        local m_style as long
        local m_RangeMin as integer, m_RangeMax as integer
        local m_stepInc as long 
        local i as long, j as long
      
        if m_n > 0 then
            for j = 0 to m_n-1  
            if m_ID(j) = id_bar then
                i = j+1         
                exit for
            end if
            next j
        end if
      
        if id_func <> %CREATE then
            if i = 0 then exit function
            decr i  ' 0-based
        else
            if i > 0 then exit function 
        end if
      
        ' Member functions
        select case id_func
        case %CREATE
            m_ID(m_n)        = id_bar
            m_hParent        = param1   
            m_x              = param2
            m_y              = param3
            m_xx             = param4       
            m_yy             = param5
            m_style          = %WS_CHILD or param6
            m_hInst          = GetModuleHandle(byval %NULL)           
            m_hProgress(m_n) = CreateWindow("MSCTLS_PROGRESS32",byval %NULL, _
                                 m_style, _
                                 m_x, m_y, m_xx, m_yy, m_hParent, _
                                 byval %NULL, m_hInst, byval %NULL)
     
            if m_hProgress(m_n) = 0 then exit function  
            incr m_n        
      
        case %SETRANGE              
            if m_hProgress(i) = 0 then exit function        
            m_RangeMin = Param1
            m_RangeMax = Param2
            SendMessage m_hProgress(i), %PBM_SETRANGE, 0, maklng(m_RangeMin,m_RangeMax)
      
        case %STEPIT
            if m_hProgress(i) = 0 then exit function        
            SendMessage m_hProgress(i), %PBM_STEPIT, 0, 0
      
        case %SETSTEP
            if m_hProgress(i) = 0 then exit function        
            m_stepInc = Param1
                SendMessage m_hProgress(i), %PBM_SETSTEP, m_stepInc, 0
      
        case %SETPOS
            if m_hProgress(i) = 0 then exit function        
            SendMessage m_hProgress(i), %PBM_SETPOS, Param1, 0
      
        case %HIDE
            if m_hProgress(i) = 0 then exit function        
            ShowWindow m_hProgress(i), %SW_HIDE
      
        case %SHOW
            if m_hProgress(i) = 0 then exit function        
            ShowWindow m_hProgress(i), %SW_SHOW
      
        case %MOVE
            if m_hProgress(i) = 0 then exit function        
            m_x  = Param1
            m_y  = Param2
            m_xx = Param3
            m_yy = Param4
     
            MoveWindow m_hProgress(i), m_x, m_y, m_xx, m_yy, %TRUE
      
        case %SETBARCOLOR
            if m_hProgress(i) = 0 then exit function    
                SendMessage m_hProgress(i), %PBM_SETBARCOLOR, 0, Param1
      
        case %SETBKCOLOR
            if m_hProgress(i) = 0 then exit function    
                SendMessage m_hProgress(i), %PBM_SETBKCOLOR, 0, Param1
      
        case %GETHANDLE
            function = m_hProgress(i) : exit function
                      
        end select
      
        function = 1
      
    end function
      
    ' Wrapper functions
    function ProgressBarCreate(byval hParent as long, _
                               byval BarId as long, _   
                               byval x as long, _     ' horizontal position
                               byval y as long, _     ' vertical position
                               byval xx as long, _    ' width
                               byval yy as long, _    ' height
                               byval style as long _
                               ) as long
      
        function = CProgressBar(%CREATE, BarId, hParent,x,y,xx, yy, style)
      
    end function
      
    function ProgressBarSetRange(byval BarId as long, _
                                 byval RangeMin as long, _ ' Minimum range value. By default, the minimum value is zero.
                                 byval RangeMax as long _  ' Maximum range value. By default, the maximum value is 100.  
                                 ) as long
      
        function = CProgressBar(%SETRANGE, BarID, RangeMin, RangeMax, 0,0,0,0)
     
    end function
      
    function ProgressBarStepIt(byval BarId as long) as long
        function = CProgressBar(%STEPIT,BarID,0,0,0,0,0,0)
    end function
      
    function ProgressBarSetStep(byval BarId as long, byval StepInc as long) as long
        function = CProgressBar(%SETSTEP, BarID, StepInc,0,0,0,0,0)
    end function
      
    function ProgressBarSetPos(byval BarId as long, byval NewPos as long) as long
        function = CProgressBar(%SETPOS, BarID, NewPos,0,0,0,0,0)
    end function
      
    function ProgressBarHide(byval BarId as long) as long
        function = CProgressBar(%HIDE,BarID,0,0,0,0,0,0)
    end function
     
    function ProgressBarShow(byval BarId as long) as long
        function = CProgressBar(%SHOW,BarID,0,0,0,0,0,0)
    end function
     
    function ProgressBarSetBarColor(byval BarId as long, byval clrBar as long) as long
        function = CProgressBar(%SETBARCOLOR,BarID,clrBar,0,0,0,0,0)
    end function
      
    function ProgressBarSetBkColor(byval BarId as long, byval clrBk as long) as long
        function = CProgressBar(%SETBKCOLOR,BarID,clrBk,0,0,0,0,0)
    end function
      
    function ProgressBarMove(byval BarId as long, _
                             byval x as long, _
                             byval y as long, _
                             byval xx as long, _
                             byval yy as long _                      
                             ) as long
     
        function = CProgressBar(%MOVE,BarID,x,y,xx,yy,0,0)
    end function
     
    function ProgressBarGetHandle(byval BarId as long) as long
        function = CProgressBar(%GETHANDLE,BarID,0,0,0,0,0,0)
    end function
    [This message has been edited by Peter Stephensen (edited June 23, 2000).]

    Comment

    Working...
    X