Announcement

Collapse
No announcement yet.

fading

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

  • fading

    Lack of Windows expertise is dogging me (not to mention lack of talent, application, etc). I'm attempting to make a control which fades a graphic to nothing then disappears, all in a specified time. In fact it will (in my imagination) also accept a user message which "kills" it. All straightforward stuff you might think, but I don't know how to do the fading bit. Can anyone help, for example by telling me what progression of colour values I should use?

  • #2
    Chris,
    something like this?
    Code:
    To fade in n seconds with s seconds/step
    
    TotalSteps = n/s
    
    for count = 1 to TotSteps
      for pixel = first to last
        NewRed = originalRed * (TotalSteps - count)/TotalSteps
        NewGreen = originalGreen * (TotalSteps - count)/TotalSteps
        NewBlue = originalBlue * (TotalSteps - count)/TotalSteps
      next
    
    
    ..update and display the graphic here
    ..delay for however long you want each step to take here
    
    next
    
    ..all done, graphic has faded to nothing.
    Paul.

    Comment


    • #3
      Paul, yes, thanks!

      Code:
      #PBFORMS CREATED V1.51
      #COMPILE EXE
      #DIM ALL
      
      #PBFORMS BEGIN INCLUDES
      #IF NOT %DEF(%WINAPI)
          #INCLUDE "WIN32API.INC"
      #ENDIF
      #PBFORMS END INCLUDES
      
      #PBFORMS BEGIN CONSTANTS
      %IDD_DIALOG1 =  101
      %IDC_LABEL1  = 1001
      #PBFORMS END CONSTANTS
      
      DECLARE CALLBACK FUNCTION ShowDIALOG1Proc()
      DECLARE FUNCTION ShowDIALOG1(BYVAL hParent AS DWORD) AS LONG
      #PBFORMS DECLARATIONS
      '
      ' Paul Dixon's method
      ' To fade in n seconds with s seconds/step
      '
      'TotalSteps = n/s
      '
      'for count = 1 to TotSteps
      '  for pixel = first to last
      '    NewRed = originalRed * (TotalSteps - count)/TotalSteps
      '    NewGreen = originalGreen * (TotalSteps - count)/TotalSteps
      '    NewBlue = originalBlue * (TotalSteps - count)/TotalSteps
      '  next
      '
      '
      '..update and display the graphic here
      '..delay for however long you want each step to take here
      '
      'next
      '
      '..all done, graphic has faded to nothing.
      
      FUNCTION PBMAIN()
          ShowDIALOG1 %HWND_DESKTOP
      END FUNCTION
      %totalsteps = 10
      CALLBACK FUNCTION ShowDIALOG1Proc()
          STATIC lcount, lred, lgreen, lblue AS LONG
      
          SELECT CASE AS LONG CBMSG
              CASE %WM_INITDIALOG
                  CONTROL SET COLOR CBHNDL, %idc_label1, %WHITE, 0
                  CONTROL SET TEXT CBHNDL, %idc_label1, "FADING!"
              CASE %WM_NCACTIVATE
                  STATIC hWndSaveFocus AS DWORD
                  IF ISFALSE CBWPARAM THEN
                      hWndSaveFocus = GetFocus()
                  ELSEIF hWndSaveFocus THEN
                      SetFocus(hWndSaveFocus)
                      hWndSaveFocus = 0
                  END IF
      
              CASE %WM_COMMAND
                  SELECT CASE AS LONG CBCTL
                      CASE %IDC_LABEL1
                          lRed   = &Hff * lcount/%TotalSteps
                          lGreen = &Hff * lcount/%TotalSteps
                          lBlue  = &Hff * lcount/%TotalSteps
                          INCR lcount
                          IF lcount = %totalsteps THEN lcount = 0
                          CONTROL SET COLOR CBHNDL, %idc_label1, %WHITE, RGB(lRed, lgreen, lblue)
                          CONTROL SET TEXT CBHNDL, %idc_label1, HEX$(lred) + HEX$(lgreen)+HEX$(lblue)
                  END SELECT
          END SELECT
      END FUNCTION
      
      FUNCTION ShowDIALOG1(BYVAL hParent AS DWORD) AS LONG
          LOCAL lRslt AS LONG
      
      #PBFORMS BEGIN DIALOG %IDD_DIALOG1->->
          LOCAL hDlg  AS DWORD
      
          DIALOG NEW hParent, "Click 2 Fade", 177, 118, 201, 121, %WS_POPUP OR %WS_BORDER OR %WS_DLGFRAME OR %WS_SYSMENU OR %WS_CLIPSIBLINGS _
              OR %WS_VISIBLE OR %DS_MODALFRAME OR %DS_3DLOOK OR %DS_NOFAILCREATE OR %DS_SETFONT, %WS_EX_CONTROLPARENT OR %WS_EX_LEFT _
              OR %WS_EX_LTRREADING OR %WS_EX_RIGHTSCROLLBAR, TO hDlg
          CONTROL ADD LABEL, hDlg, %IDC_LABEL1, "Label1", 50, 15, 115, 75, %WS_CHILD OR %WS_VISIBLE OR %WS_TABSTOP OR %SS_CENTER _
                           OR %SS_CENTERIMAGE OR %SS_NOTIFY
      #PBFORMS END DIALOG
      
          DIALOG SHOW MODAL hDlg, CALL ShowDIALOG1Proc TO lRslt
      
      #PBFORMS BEGIN CLEANUP %IDD_DIALOG1
      #PBFORMS END CLEANUP
      
          FUNCTION = lRslt
      END FUNCTION

      Comment


      • #4
        Doing this I noticed that if the colour is faded to white, the white seems more persistent to the eye than the nearly faded colour, plus I suppose it hangs around until the control is killed. The effect - at least on my eyes - is that the white is held for too long.

        Anyway, this version takes a start colour as a parameter to the Fader function and uses a colour gradient array rather than attempting to calculate the colour on the fly.

        ** updated to include a colour picker, controls for specifying interval & number of steps, "stop" button

        Code:
        #PBFORMS CREATED V1.51
        #COMPILE EXE
        #DIM ALL
        
        #PBFORMS BEGIN INCLUDES
        #IF NOT %DEF(%WINAPI)
            #INCLUDE "WIN32API.INC"
        #ENDIF
        #PBFORMS END INCLUDES
        
        #INCLUDE "comdlg32.inc"
        
        #PBFORMS BEGIN CONSTANTS
        %IDD_DIALOG1  = 103
        %IDC_LISTBOX1 = 104
        %IDC_LABEL1   = 105
        %IDC_LABEL2   = 108
        %IDC_LABEL3   = 109
        %IDC_steps_tb = 106
        %IDC_int_TB   = 107
        %IDC_STOP_BN  = 110
        %IDC_GO_BN    = 102
        #PBFORMS END CONSTANTS
        
        DECLARE CALLBACK FUNCTION ShowDIALOG1Proc()
        DECLARE FUNCTION ShowDIALOG1(BYVAL hParent AS DWORD) AS LONG
        #PBFORMS DECLARATIONS
        '--------------------------------------------------------------------
        ' common functions
        '--------------------------------------------------------------------
        ' return selected colour
        FUNCTION SelColour( hD AS DWORD) AS LONG
        
                STATIC CCA      AS CHOOSECOLORAPI
                LOCAL lResult   AS LONG
                STATIC clrRef() AS DWORD ' colorref
        
                DIM clrRef(0 TO 16) AS STATIC DWORD 'colorref
        
                CCA.lStructSize  = LEN(CCA)
                CCA.hwndOwner    = hD
                CCA.lpCustColors = VARPTR(ClrRef())
                CCA.rgbResult    = getsyscolor(%color_3dface)
                CCA.Flags        = CCA.Flags OR %CC_RGBINIT 'or %CC_FULLOPEN
        
                lResult = ChooseColor(CCA)
        
                IF lResult <> 0 THEN 'check if user cancelled dialog ?
                    FUNCTION = CCA.rgbResult 'HEX$(CCA.rgbResult,6)
                    'to get RGB sequence, use RIGHT$(ClrStr,2) + MID$(ClrStr,3,2) + LEFT$ (ClrStr,2)
                END IF
        
        END FUNCTION
        '-------------------------------------------------------------------
        ' FADER functions
        '----------------------------------------------------------------------
        
        CALLBACK FUNCTION FaderCallBackProc()
            STATIC lcount, lredstart, lgreenstart, lbluestart, interval, steps AS LONG
            LOCAL i, l, lcol, lred, lgreen, lblue AS LONG
            LOCAL fR, fG, fB AS SINGLE
            DIM lcolgrad(0 TO 255) AS STATIC LONG
        
            SELECT CASE AS LONG CBMSG
                CASE %WM_INITDIALOG
                    DIALOG GET USER CBHNDL, 1 TO steps
                    DIALOG GET USER CBHNDL, 2 TO lcol
        
                    lcount = 0 ' it's static, so clear down each time through
                    ' make a colour gradient
                    ' start the gradient with the colour supplied
                    l = lcol
                    fR = l AND &HFF
                    SHIFT RIGHT l, 8
                    fG = l AND &HFF
                    SHIFT RIGHT l, 8
                    fB = l AND &HFF
                    FOR i = 0 TO 255
                        lColGrad(i) = RGB((i*(255-fR)/255) + fR, (i*(255-fg)/255) + fg ,(i*(255-fb)/255) + fb)
                    NEXT
                    CONTROL SET COLOR CBHNDL, 101, %WHITE, lColGrad(0)
                    CONTROL SHOW STATE CBHNDL, 101, %SW_SHOW
        
                CASE %wm_timer
                    INCR lcount
                    IF lcount = steps -1 THEN EXIT FUNCTION  ' avoid showing white
                    IF lcount >= Steps THEN
                        DIALOG END CBHNDL, 0
                    END IF
                    LISTBOX ADD getparent(CBHNDL), %idc_listbox1, HEX$( lcolgrad(255*lcount/steps), 6)' & format$( int(255*lcount/steps), " ###.#")
                    CONTROL SET COLOR CBHNDL, 101, %WHITE, lcolgrad(255*lcount/steps)
                    CONTROL REDRAW CBHNDL, 101
                CASE %WM_USER + 2
                    lcount = steps ' stop fading
        
                CASE %WM_NCACTIVATE
                    STATIC hWndSaveFocus AS DWORD
                    IF ISFALSE CBWPARAM THEN
                        hWndSaveFocus = GetFocus()
                    ELSEIF hWndSaveFocus THEN
                        SetFocus(hWndSaveFocus)
                        hWndSaveFocus = 0
                    END IF
        
                CASE %WM_DESTROY
                    CONTROL KILL CBHNDL, 101
                    IF killtimer(CBHNDL, 1) = 0 THEN
                        ? "can't kill timer!"
                    END IF
        
                CASE %WM_COMMAND
                    SELECT CASE AS LONG CBCTL
                        CASE 101
                            DIALOG END CBHNDL, 0
                    END SELECT
            END SELECT
        END FUNCTION
        
        '----------------------------------------------------------------------------------------------
        ' parameters are parent window handle, # of steps, interval between steps in mSec,
        '                start colour, text for label.
        FUNCTION Fader(BYVAL hParent AS DWORD, steps AS LONG, BYVAL interval AS LONG, _
                       BYVAL lColour AS LONG, BYVAL stext AS STRING) AS LONG
        
            LOCAL lRslt, x, y, h, w AS LONG
            LOCAL hDlg  AS DWORD
            LOCAL rc AS rect
        '    static labeltext as string
        
            GetWindowRect hParent, rc
            DIALOG NEW PIXELS, hparent, "", 0,0, rc.nright - rc.nleft, rc.nbottom-rc.ntop, _
                %WS_POPUP OR %WS_CHILD OR _
                %WS_CLIPSIBLINGS OR %WS_VISIBLE OR %DS_NOFAILCREATE, _
                %WS_EX_CONTROLPARENT OR %WS_EX_TOOLWINDOW OR _
                %WS_EX_TOPMOST OR %WS_EX_LEFT TO hDlg
        
            DIALOG SET COLOR hdlg, -1, -2
            DIALOG SET USER hdlg, 1, steps
            DIALOG SET USER hdlg, 2, lcolour
            DIALOG SEND hParent, %WM_USER + 1, hdlg, 0 ' let parent know who we are
            
            CONTROL ADD LABEL, hDlg, 101, stext,10, 10, 80, 30, %SS_CENTER OR %SS_CENTERIMAGE
            CONTROL SET LOC    hdlg, 101, (rc.nright - rc.nleft)/3, (rc.nbottom-rc.ntop)/3
        
            SetTimer hdlg, 1, interval, %NULL
        
            DIALOG SHOW MODELESS hDlg, CALL FaderCallBackProc TO lRslt
        
            FUNCTION = lRslt
        END FUNCTION
        '--------------------------------------------------------------------------------
        FUNCTION PBMAIN()
            ShowDIALOG1 %HWND_DESKTOP
        END FUNCTION
        
        CALLBACK FUNCTION ShowDIALOG1Proc()
            LOCAL interval, steps, TryCOl AS LONG
            LOCAL s AS STRING
            STATIC hDfader AS DWORD
            
            SELECT CASE AS LONG CBMSG
                CASE %WM_INITDIALOG
        
                CASE %WM_USER + 1
                    hDFader = CBWPARAM
                    
                CASE %WM_NCACTIVATE
                    STATIC hWndSaveFocus AS DWORD
                    IF ISFALSE CBWPARAM THEN
                        hWndSaveFocus = GetFocus()
                    ELSEIF hWndSaveFocus THEN
                        SetFocus(hWndSaveFocus)
                        hWndSaveFocus = 0
                    END IF
        
                CASE %WM_COMMAND
                    SELECT CASE AS LONG CBCTL
                        CASE %IDC_GO_BN
                            CONTROL GET TEXT CBHNDL, %idc_int_tb TO s
                            interval = VAL(s)
                            IF interval = 0 THEN BEEP: EXIT FUNCTION
                            CONTROL GET TEXT CBHNDL, %idc_steps_tb TO s
                            steps = VAL(s)
                            IF steps = 0 THEN BEEP: EXIT FUNCTION
                            TryCol = selcolour ( CBHNDL)
                            fader(CBHNDL, steps, interval, TryCol, "GOODBYE")  ' change the colour or text
                        CASE %IDC_STOP_BN
                            DIALOG SEND hdfader, %WM_USER + 2, 0, 0 ' send message to fader dialog to stop fading
                    END SELECT
            END SELECT
        END FUNCTION
        
        FUNCTION ShowDIALOG1(BYVAL hParent AS DWORD) AS LONG
            LOCAL lRslt AS LONG
        
        #PBFORMS BEGIN DIALOG %IDD_DIALOG1->->
            LOCAL hDlg  AS DWORD
        
            DIALOG NEW hParent, "Fader", 70, 70, 265, 134, %WS_POPUP OR %WS_BORDER OR %WS_DLGFRAME OR %WS_SYSMENU OR %WS_CLIPSIBLINGS OR _
                %WS_VISIBLE OR %DS_MODALFRAME OR %DS_3DLOOK OR %DS_NOFAILCREATE OR %DS_SETFONT, %WS_EX_CONTROLPARENT OR %WS_EX_LEFT OR _
                %WS_EX_LTRREADING OR %WS_EX_RIGHTSCROLLBAR, TO hDlg
            CONTROL ADD BUTTON,  hDlg, %IDC_GO_BN, "GO", 190, 110, 30, 20
            CONTROL ADD LISTBOX, hDlg, %IDC_LISTBOX1, , 5, 60, 80, 70
            CONTROL ADD LABEL,   hDlg, %IDC_LABEL1, "RGB picked from color gradient", 5, 45, 80, 15
            CONTROL ADD TEXTBOX, hDlg, %IDC_steps_tb, "", 220, 85, 25, 15, %WS_CHILD OR %WS_VISIBLE OR %WS_TABSTOP OR %ES_LEFT OR _
                %ES_AUTOHSCROLL OR %ES_NUMBER, %WS_EX_CLIENTEDGE OR %WS_EX_LEFT OR %WS_EX_LTRREADING OR %WS_EX_RIGHTSCROLLBAR
            CONTROL ADD TEXTBOX, hDlg, %IDC_int_TB, "", 220, 65, 25, 15, %WS_CHILD OR %WS_VISIBLE OR %WS_TABSTOP OR %ES_LEFT OR _
                %ES_AUTOHSCROLL OR %ES_NUMBER, %WS_EX_CLIENTEDGE OR %WS_EX_LEFT OR %WS_EX_LTRREADING OR %WS_EX_RIGHTSCROLLBAR
            CONTROL ADD LABEL,   hDlg, %IDC_LABEL2, "Interval", 170, 65, 45, 15, %WS_CHILD OR %WS_VISIBLE OR %SS_RIGHT, %WS_EX_RIGHT OR _
                %WS_EX_LTRREADING
            CONTROL ADD LABEL,   hDlg, %IDC_LABEL3, "Steps", 170, 85, 45, 15, %WS_CHILD OR %WS_VISIBLE OR %SS_RIGHT, %WS_EX_RIGHT OR _
                %WS_EX_LTRREADING
            CONTROL ADD BUTTON,  hDlg, %IDC_STOP_BN, "STOP", 225, 110, 30, 20
        #PBFORMS END DIALOG
        
            DIALOG SHOW MODAL hDlg, CALL ShowDIALOG1Proc TO lRslt
        
        #PBFORMS BEGIN CLEANUP %IDD_DIALOG1
        #PBFORMS END CLEANUP
        
            FUNCTION = lRslt
        END FUNCTION
        Last edited by Chris Holbrook; 25 Jan 2008, 05:11 AM. Reason: added colour picker, etc

        Comment


        • #5
          If 'fading' is just one option you are considering to remove the control from view in a less-than-abrupt fashion, you could also try the effects you can get from the AnimateWindow() function.

          AnimateWindow() has a 'fade' option, too, but the doc says that only works on top-level windows, so it would not be supported for use on a control (a child window).

          It's a thought.

          MCM
          Michael Mattias
          Tal Systems Inc. (retired)
          Racine WI USA
          [email protected]
          http://www.talsystems.com

          Comment


          • #6
            Originally posted by Michael Mattias View Post
            ...AnimateWindow() ...
            yes, there's an example here. I almost wish I'd found it before coding the fade! The "blend" effect looks particularly good and I suspect that it would need quite a bit of coding - or would it? either there is some cunning mixing of bitmaps going on, or the window is split into many RECTs which are progressively dropped from a region?

            Comment

            Working...
            X