Announcement

Collapse
No announcement yet.

Trap Button down event

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

  • Trap Button down event

    Hi all,
    The %BN_CLICKED message of a button only takes place when a button
    is released.
    Code:
        CALLBACK FUNCTION b1
        IF CBCTLMSG=%BN_CLICKED THEN
                MSGBOX "fff"
        END IF
        END FUNCTION
    I would like to trap the event when a button is pressed.
    I tried it like this but it does nothing apparently...:
    I'm sure when one of you guys comes up with the solution, it will, once
    again, seem so obvious...(hopefully)
    Code:
    #COMPILE EXE
    #DIM ALL
    #INCLUDE "WIN32API.INC"
    #INCLUDE "COMMCTRL.INC"
    %ID_UDBUTON1 = 101
    %ID_UDBUTON2 = 102
    GLOBAL hDlg AS LONG
    GLOBAL hButton1 AS LONG
    
    CALLBACK FUNCTION DlgProc
        STATIC a AS LONG, x AS LONG , BSTATE AS LONG
    
        SELECT CASE CBMSG
            CASE %WM_INITDIALOG
                InitCommonControls
            CASE %WM_NOTIFY
            CASE %WM_COMMAND
            CASE %WM_LBUTTONDOWN
                a = SendMessage(GetDlgItem(CBHNDL, %ID_UDBUTON1), %BM_GETSTATE, 0, 0) AND %BST_PUSHED
                INCR x
                SetWindowText hDlg, " Counter: " + STR$(x)
        END SELECT
    END FUNCTION
    
    
    FUNCTION PBMAIN
        DIALOG NEW 0, "Button Down demo??",,, 130, 90, %WS_OVERLAPPEDWIN TO hDlg
        CONTROL ADD BUTTON, hDlg, %ID_UDBUTON1,"+", 10, 10, 20,20,'%BS_NOTIFY ' CALL CB_Button1
        CONTROL ADD BUTTON, hDlg, %ID_UDBUTON2,"-", 10, 31, 20,20,'%BS_NOTIFY 'OR %BS_OWNERDRAW CALL CB_Button2
        DIALOG SHOW MODAL hDlg CALL DlgProc
    END FUNCTION
    kind regards
    Eddy


    ------------------
    [email protected]

    [This message has been edited by Eddy Van Esch (edited June 19, 2001).]
    Eddy

  • #2
    WM_MOUSEACTIVATE **
    WM_SETCURSOR to receive WM_LBUTTONDOWN etc..
    WM_HITTEST

    ------------------
    hellobasic

    Comment


    • #3
      Edwin,
      I tried to find info on the messages you said, but I wasn't able to come
      up with something I could use (or understand..). I'm not a windows guru
      as you might have guessed.
      What I actually want is 2 buttons, an UP and a DOWN button to increment
      or decrement a numerical value. When one of the buttons is clicked,
      the value should be incremented or decremented by a certain value.
      When the buttons are continuously pressed, the value should continue
      to change. Also, when the button is held during a longer time, the
      value should start to increment faster and faster..
      I modified an 'old' routine of Semen. This does almost what I want.
      I just have to add the piece that does the accelerated incrementation.
      What I don't like about this routine is that the single clicking does
      not go smooth because the value is only in- or decremented when
      the timer is fired...
      But like I said, I haven't been able to come up with something better.
      I also tried to use Up-down controls but then I also have to do the
      accelerated incrementing myself and it needs just as much lines of code...

      Code:
      #COMPILE EXE
         #REGISTER NONE
         #DIM ALL
         #INCLUDE "win32api.inc"
         %ID_B = 2
         %ID_L = 3
         %ID_B2 = 4
         %ID_L2 = 5
         GLOBAL x AS LONG,x2 AS LONG
         GLOBAL hdlg AS LONG
      
         CALLBACK FUNCTION maincallback
            %Delay0 = 80
            STATIC t AS LONG
            DIM tt AS LONG
            SELECT CASE CBMSG
               CASE %WM_INITDIALOG: tt = %Delay0 : SetTimer CBHNDL, 1, tt, BYVAL 0
               CASE %WM_DESTROY: KillTimer CBHNDL, 1
               CASE %WM_TIMER
                  IF (SendMessage(GetDlgItem(CBHNDL, %ID_B), %BM_GETSTATE, 0, 0) AND %BST_PUSHED) THEN
                     INCR x: CONTROL SET TEXT CBHNDL, %ID_L, "X = " & STR$(x): _
                  END IF
                  IF (SendMessage(GetDlgItem(CBHNDL, %ID_B2), %BM_GETSTATE, 0, 0) AND %BST_PUSHED) THEN
                     DECR x: CONTROL SET TEXT CBHNDL, %ID_L, "X = " & STR$(x)
                  END IF
            END SELECT
         END FUNCTION
      
         FUNCTION PBMAIN AS LONG
            DIALOG NEW 0, "Hold down the left mouse button", , , 350, 300, %WS_MAXIMIZEBOX OR _
            %WS_MINIMIZEBOX OR %WS_SYSMENU OR %WS_CAPTION TO hdlg
            CONTROL ADD BUTTON, hdlg, %ID_B, "Up Button", 180, 50, 100, 20, %BS_DEFAULT 
            CONTROL ADD LABEL, hdlg, %ID_L, "X = ", 180, 80, 125, 20
            CONTROL ADD BUTTON, hdlg, %ID_B2, "Down Button", 180, 150, 100, 20, %BS_DEFAULT
            CONTROL ADD LABEL, hdlg, %ID_L2, "Click button or hold it down..", 180, 180, 125, 20
            DIALOG SHOW MODAL hdlg, CALL maincallback
         END FUNCTION



      ------------------
      [email protected]
      Eddy

      Comment


      • #4
        It is not clear what you ultimately want such a control to do, but if this is just to adjust some specific "value", it seems to me that you may be approaching the problem from the wrong direction.

        For example, you could use a native Windows slider control and give it a logarithmic scale. Then you could read the value directly from the control and leave the UI interface control to Windows.

        The same kind of control can also be achieved with a scrollbar, but a slider looks a little tidier.

        Maybe if you could explain the purpose of the control, we could offer more specific suggestions to accomplish what you want to achieve.

        ------------------
        Lance
        PowerBASIC Support
        mailto:[email protected][email protected]</A>
        Lance
        mailto:[email protected]

        Comment


        • #5
          Originally posted by Lance Edmonds:
          Maybe if you could explain the purpose of the control, we could offer more specific suggestions to accomplish what you want to achieve.
          [/B]
          I'm working on a program that keeps track of the time that a pc user
          works on a (or multiple) project(s).
          So, the program has to handle a lot of time variables. I use the time
          format "HHHHH:MM" because a certain project one is working on can run
          over a long period of time (a year or so).
          Normally these times are automatically registered by the program, the user
          only has to select which project he is working on at the time.
          But I also want to add the possibility to change a certain time manually.
          Because of the HHHHH:MM format, I didn't want to just use a textbox.
          Therefore, I wanted to add an up-down control (or 2 seperate buttons, one for
          'up' and one for 'down') to increase/decrease the hours, and one control
          to increase/decrease the minutes.
          So, the user had to simply press a button to change the time instead of
          typing it in.
          Because the number of hours can range from let's say 1 to 10000, I wanted
          the 'accelerated increment' so that when the current timevalue is 10 and
          you want it to be 10000, you don't have to press the UP button for half
          an hour or so...
          I hope this makes my 'problem' a bit clearer.
          Could be that I'm going a bit over the top with this, making it all
          a bit more complicated than it should be, but I'm open to suggestions...
          King regards
          Eddy


          ------------------
          [email protected]
          Eddy

          Comment


          • #6
            It's easy to make use of a timer and reset the timer interval after certain counts/or time.
            If no one shows up with a quick solution i might do it but i still think a timer is required.

            1) Is this a problem?
            2) What if i made this using a special windowclass?
            You would receive a simple notify each timer interval wich you respond to.



            ------------------
            hellobasic

            Comment


            • #7
              Btw, the datepicker already has a sep. date field to scroll with
              Isn't this the simpliest solution.

              Example: doublclick your clock icon in the taskbar and see the time control.
              The same is available for dates.


              ------------------
              hellobasic

              Comment


              • #8
                Originally posted by Edwin Knoppert:
                It's easy to make use of a timer and reset the timer interval after certain counts/or time.
                Edwin,
                With the knowledge I have right now, I think it would be the easiest for
                me to use a timer like you said and play with the timer interval. Something
                like my last example above.
                Since in this program I only work with hours and minutes (even if the number of hours go
                to 10000..) I think the datepicker would be less appropriate.
                It amazes me how 'complicated' the solution is for this little problem in a windows environment.
                I used to do something like this in DOS in a flash... (you don't have to reply on this
                last remark... )
                kind regards
                Eddy



                ------------------
                [email protected]
                Eddy

                Comment


                • #9
                  What about my offer?

                  ------------------


                  [This message has been edited by Edwin Knoppert (edited June 20, 2001).]
                  hellobasic

                  Comment


                  • #10
                    Originally posted by Edwin Knoppert:
                    What about my offer?
                    I think I can manage it, using a (few) timer(s) like said before. So I'll have
                    a go first.
                    But thanks a lot for the offer Edwin!!
                    Kind regards
                    Eddy



                    ------------------
                    [email protected]
                    Eddy

                    Comment


                    • #11
                      This piece of code I wrote does pretty much what I want. Didn't think
                      it would take this many lines of code though...

                      Code:
                      ' Accelerated Up-Down buttons
                      ' With an UP and a DOWN button you can increment or decrement a certain numerical variable.
                      ' Clicking the buttons changes the variable with 1.
                      ' Holding one of the buttons down will continuously increment or decrement the variable.
                      ' The longer you hold the button down, the faster the variable will change. This is the
                      ' acceleration effect.
                      
                      #COMPILE EXE
                      #REGISTER NONE
                      #DIM ALL
                      #INCLUDE "win32api.inc"
                      
                      %ID_UpButt   = 2
                      %ID_Number   = 3
                      %ID_DownButt = 4
                      %ID_Label    = 5
                      %Delay_NoButtonPressed    = 20  'Take this one rather short so that you get a quick response when you
                                                      'click a button
                      %Delay_ButtonPressed_Start= 300 'When button is pressed this delay is triggered. After this delay,
                                                      'when button is still pressed, parameter starts de/incrementing
                      %Delay_ButtonPressed_End  = 20  'To get the acceleration effect, the time interval is decreased when
                                                      'the buton is held, this is the smallest time interval
                      
                      $Prefix = "Try to set this value to 10000: "
                      GLOBAL x AS LONG
                      GLOBAL hdlg AS LONG
                      
                      CALLBACK FUNCTION maincallback
                          STATIC UpButtonWasPushed AS LONG, DownButtWasPushed AS LONG, Time AS LONG, Delta AS LONG
                          SELECT CASE CBMSG
                             CASE %WM_INITDIALOG
                                  SetTimer CBHNDL, 1, %Delay_NoButtonPressed, BYVAL 0
                                  UpButtonWasPushed   = %False
                                  DownButtWasPushed   = %False
                                  Delta               = 1
                             CASE %WM_DESTROY
                                  KillTimer CBHNDL, 1
                             CASE %WM_TIMER
                                  'Process UP button
                                  IF (SendMessage(GetDlgItem(CBHNDL, %ID_UpButt), %BM_GETSTATE, 0, 0) AND %BST_PUSHED) THEN
                                      'Up Button is now pushed
                                      IF UpButtonWasPushed = %False THEN
                                          'Button was just pushed. Set timer value to wait for next increment
                                          UpButtonWasPushed = %True 'Remember it for next time out event
                                          Time        = %Delay_ButtonPressed_Start
                                          KillTimer CBHNDL, 1
                                          SetTimer CBHNDL, 1, Time, BYVAL 0
                                      ELSE
                                          'Timer was already set. Increment Delta value to get the acceleration effect
                                          IF Time > %Delay_ButtonPressed_End THEN
                                              'Decrement timer only if lower limit not reached
                                              Time = Time -40
                                              KillTimer CBHNDL, 1
                                              SetTimer CBHNDL, 1, Time, BYVAL 0
                                          END IF
                                          Delta = Delta + 1
                                      END IF
                                      x = x + Delta: CONTROL SET TEXT CBHNDL, %ID_Number, $Prefix & STR$(x)
                                  ELSE
                                      'Button not pushed
                                      IF UpButtonWasPushed THEN
                                          UpButtonWasPushed = %False
                                          'Buttonstate just changed, so reset timer:
                                          KillTimer CBHNDL, 1
                                          SetTimer CBHNDL, 1, %Delay_NoButtonPressed, BYVAL 0
                                          Delta = 1
                                      END IF
                                  END IF
                      
                                  'Process DOWN button
                                  IF (SendMessage(GetDlgItem(CBHNDL, %ID_DownButt), %BM_GETSTATE, 0, 0) AND %BST_PUSHED) THEN
                                      'Down Button is now pushed
                                      IF DownButtWasPushed = %False THEN
                                          'Button was just pushed. Set timer value to wait for next increment
                                          DownButtWasPushed = %True 'Remember it for next time out event
                                          Time        = %Delay_ButtonPressed_Start
                                          KillTimer CBHNDL, 1
                                          SetTimer CBHNDL, 1, Time, BYVAL 0
                                      ELSE
                                          'Timer was already set. Decrement time interval to get the acceleration effect
                                          IF Time > %Delay_ButtonPressed_End THEN
                                              'Decrement timer only if lower limit not reached
                                              Time = Time -40
                                              KillTimer CBHNDL, 1
                                              SetTimer CBHNDL, 1, Time, BYVAL 0
                                          END IF
                                          Delta = Delta + 1
                                      END IF
                                      x = x - Delta: CONTROL SET TEXT CBHNDL, %ID_Number, $Prefix & STR$(x)
                                  ELSE
                                      'Button not pushed
                                      IF DownButtWasPushed THEN
                                          DownButtWasPushed = %False
                                          'Buttonstate just changed, so reset timer:
                                          KillTimer CBHNDL, 1
                                          SetTimer CBHNDL, 1, %Delay_NoButtonPressed, BYVAL 0
                                          Delta = 1
                                      END IF
                                  END IF
                          END SELECT
                      END FUNCTION
                      
                      FUNCTION PBMAIN AS LONG
                          x = 0   'Initial value of the variable you want to change
                      
                          DIALOG NEW 0, "Accelerated Up-down buttons", , , 200, 100, %WS_MAXIMIZEBOX OR _
                          %WS_MINIMIZEBOX OR %WS_SYSMENU OR %WS_CAPTION TO hdlg
                          CONTROL ADD LABEL, hdlg, %ID_Number, $Prefix & STR$(x), 10, 10, 125, 10
                          CONTROL ADD BUTTON, hdlg, %ID_UpButt,     "Up",   70, 20, 25, 15, %BS_DEFAULT
                          CONTROL ADD BUTTON, hdlg, %ID_DownButt,   "Down", 70, 35, 25, 15, %BS_DEFAULT
                          CONTROL ADD LABEL, hdlg, %ID_Label, "Click one of the buttons or click-and-hold...", 10, 60, 125, 20
                          DIALOG SHOW MODAL hdlg, CALL maincallback
                      END FUNCTION
                      ------------------
                      [email protected]

                      [This message has been edited by Eddy Van Esch (edited June 21, 2001).]
                      Eddy

                      Comment


                      • #12
                        Code:
                        '==========================================================
                        ' Using updown control with accellerators
                        '==========================================================
                        #COMPILE EXE
                        #REGISTER NONE
                        #DIM ALL
                        #INCLUDE "win32api.inc"
                        #INCLUDE "Commctrl.inc"
                        
                        %UD = 2 : %TB = 3
                        %UDSTYLE            = %WS_CHILD OR %WS_BORDER OR %WS_VISIBLE OR _
                                              %UDS_WRAP OR %UDS_ARROWKEYS OR %UDS_SETBUDDYINT
                        GLOBAL hdlg AS LONG
                        
                        FUNCTION PBMAIN AS LONG
                          DIM UDA(2) AS GLOBAL UDACCEL
                          Dialog New 0, "Accelerated Up-down", , , 140, 75, %WS_MAXIMIZEBOX OR _
                                                 %WS_MINIMIZEBOX OR %WS_SYSMENU OR %WS_CAPTION TO hdlg
                          Control Add Label, hDlg, -1     ,"Testing:" ,15, 30, 60, 12
                          Control Add TextBox, hDlg, %TB,"0",45,30,50,12,, %WS_EX_CLIENTEDGE
                          Control Add "msctls_updown32", hDlg,%UD,"", 95, 30, 20, 12, %UDSTYLE
                          Control SEND hDlg, %UD, %UDM_SETBUDDY,GetDlgItem(hDlg,%TB),0&
                          Control SEND hDlg, %UD, %UDM_SETRANGE,0&, MAKLNG (10000,0)
                          '========================================================
                          'UDA holds the accellerator values for the updown control
                          '========================================================
                          UDA(0).nSec = 0               'From 0-2 secs, control
                          UDA(0).nInc = 1               'increments by 1
                        
                          UDA(1).nSec = 2               'After 2 secs, control
                          UDA(1).nInc = 25              'increments by 25
                        
                          UDA(2).nSec = 4               'After 4 secs, control
                          UDA(2).nInc = 100             'increments by 100
                        
                          Control SEND hDlg,%UD, %UDM_SETACCEL, 3, VARPTR(UDA(0))
                          '========================================================
                            Dialog Show Modal hdlg
                        END FUNCTION
                        ------------------
                        Peter.
                        mailto[email protected][email protected]</A>
                        Regards,
                        Peter

                        Comment


                        • #13
                          Originally posted by Peter Lameijn:
                          Code:
                          '==========================================================
                          ' Using updown control with accellerators
                          '==========================================================
                          Cool Peter! Very nice, thanks a lot. I can use this code!
                          Eddy



                          ------------------
                          [email protected]
                          Eddy

                          Comment

                          Working...
                          X