Announcement

Collapse
No announcement yet.

Detecting TAB to a Label as a Button

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

  • Detecting TAB to a Label as a Button

    Hi:

    I have some nicely color labels that I use as buttons. (Too me , it's so much easier to color a button looking label)

    I can detect clicks and use the label/Button.

    Yes, I have WS_TabStops in the label style.

    How do I detect the z-order TAB is on this label and has focus?

    I need to get the SetFocus/KillFocus command if I can.

    Thank You

  • #2
    Maybe GotFocus()?

    GetFocus

    The GetFocus function retrieves the handle to the window that has the keyboard focus, if the window is associated with the calling thread's message queue.

    HWND GetFocus(VOID)

    Parameters
    This function has no parameters.

    Return Values
    The return value is the handle to the window with the keyboard focus. If the calling thread's message queue does not have an associated window with the keyboard focus, the return value is NULL.

    Remarks
    Even if GetFocus returns NULL, another thread's queue may be associated with a window that has the keyboard focus.

    Use the GetForegroundWindow function to retrieve the handle to the window with which the user is currently working. You can associate your thread's message queue with the windows owned by another thread by using the AttachThreadInput function.
    Fred
    "fharris"+Chr$(64)+"evenlink"+Chr$(46)+"com"

    Comment


    • #3
      Fred:

      Thanks for the reply.

      Scenario:

      Label displayed as a button.

      %SS_Notify and %WS_TabStop are part of the style.

      If the user clicks on the label (acting as a button) I get a msg and I process the click. No problem there.

      Now.

      If the User tabs though the CheckBox's or Edit boxes I get a %BN_SETFOCUS and a %BN_KILLFocus ( the edit box msgs are different of course) for each control perfect!

      But as the user continues though the defined TAB z-order (with the Label/Buttons in the order), I have no idea how to detect that the TAB (Focus) is now on each of my label/buttons.

      What message is being sent that I can process like the above %BN_SetFocus and %BN_KillFocus for the checkboxs but for these label/Buttons?

      Thank you

      Bruce

      P.S. Wow this forums clock is 4 hrs different from mine...hummm I'm central time so where is the time zone?
      Last edited by Bruce Wood; 26 Mar 2008, 09:56 PM. Reason: Better explaination

      Comment


      • #4
        Here's one way to do it Bruce,
        Code:
        #COMPILE EXE
        #DIM ALL
        #INCLUDE "WIN32API.INC"
         
        %LBL_LABEL1   = 1001
        %LBL_LABEL2   = 1002
        %TXT_TEXTBOX1 = 1011
        %TXT_TEXTBOX2 = 1012
         
        'Subclass Macros. tt Colin Schmidt
        MACRO mSC_Set(hDlg, IdCtl, SCProc)
          SETWINDOWLONG GETDLGITEM(hDlg, IdCtl), %GWL_USERDATA, _
            SETWINDOWLONG(GETDLGITEM(hDlg, IdCtl), %GWL_WNDPROC, CODEPTR(SCProc))
        END MACRO
        'eg mSC_Set(hDlg, %ID_Ctrl, SubClassProc) '<<Identify SubClass CallBack Function
         
        MACRO mSC_OrgProc
          FUNCTION = CALLWINDOWPROC(GETWINDOWLONG(CBHNDL, %GWL_USERDATA), CBHNDL, CBMSG, CBWPARAM, CBLPARAM)
        END MACRO
        'eg mSC_OrgProc    '<<Pass on unhandled messages tp original CallBack Proc.
        '------------------/Macros
         
        CALLBACK FUNCTION LabelProc
         LOCAL hWnd AS DWORD
          hWnd = GetParent(CBHNDL)
          SELECT CASE AS LONG CBMSG
            CASE %WM_SETFOCUS
              SELECT CASE CBHNDL
                CASE GetDlgItem(hWnd, %LBL_LABEL1) :DIALOG SET TEXT hWnd, "1. Got Focus": _
                  Control Set Color hWnd, %LBL_LABEL1, %BLUE, -1&
                  CONTROL REDRAW hWnd, %LBL_LABEL1
                  WinBeep 400, 200
                CASE GetDlgItem(hWnd, %LBL_LABEL2) :DIALOG SET TEXT hWnd, "2. Got Focus": _
                  Control Set Color hWnd, %LBL_LABEL2, %BLUE, -1&
                  CONTROL REDRAW hWnd, %LBL_LABEL2
                  WinBeep 1000, 200
              END SELECT
              FUNCTION = 0 : EXIT FUNCTION
         
            CASE %WM_KILLFOCUS
              SELECT CASE CBHNDL
                CASE GetDlgItem(hWnd, %LBL_LABEL1) :DIALOG SET TEXT hWnd, "1. Lost Focus": _
                  Control Set Color hWnd, %LBL_LABEL1, -1&, -1&
                  CONTROL REDRAW hWnd, %LBL_LABEL1
                  WinBeep 400, 20
                CASE GetDlgItem(hWnd, %LBL_LABEL2) :DIALOG SET TEXT hWnd, "2. Lost Focus": _
                  Control Set Color hWnd, %LBL_LABEL2, -1&, -1&
                  CONTROL REDRAW hWnd, %LBL_LABEL2
                  WinBeep 1000, 20
              END SELECT
              FUNCTION = 0 : EXIT FUNCTION
          END SELECT
         
         mSC_OrgProc       'pass unhandled messages to original proc
        END FUNCTION
        '------------------/LabelProc
         
        CALLBACK FUNCTION DialogProc()
          SELECT CASE AS LONG CBMSG
         
            CASE %WM_COMMAND
              SELECT CASE AS LONG CBCTL
                CASE %LBL_LABEL1
                  IF CBCTLMSG = %BN_CLICKED THEN
                    CONTROL SET FOCUS CBHNDL, %LBL_LABEL1
                    Dialog SET TEXT CBHNDL, "Label1 clicked"
                  END IF
         
                CASE %LBL_LABEL2
                  IF CBCTLMSG = %BN_CLICKED THEN
                    CONTROL SET FOCUS CBHNDL, %LBL_LABEL2
                    Dialog SET TEXT CBHNDL, "Label2 clicked"
                  END IF
         
              END SELECT
         
          END SELECT
        END FUNCTION
        '------------------/DialogProc
         
        FUNCTION PBMAIN()
         LOCAL hDlg  AS DWORD
         
         DIALOG NEW 0, "Press Tab, Click Label", 158, 124, 201, 121, %WS_CAPTION OR %WS_SYSMENU, TO hDlg
          CONTROL ADD TEXTBOX, hDlg, %TXT_TEXTBOX1, "TextBox1", 25, 15, 100, 13
          CONTROL ADD LABEL,   hDlg, %LBL_LABEL1, "Label1", 25, 40, 100, 10, %WS_CHILD OR _
            %WS_VISIBLE OR %WS_TABSTOP OR %SS_NOTIFY
          CONTROL ADD TEXTBOX, hDlg, %TXT_TEXTBOX2, "TextBox2", 25, 60, 100, 13
          CONTROL ADD LABEL,   hDlg, %LBL_LABEL2, "Label2", 25, 90, 100, 10, %WS_CHILD OR _
            %WS_VISIBLE OR %WS_TABSTOP OR %SS_NOTIFY
         
          mSC_Set(hDlg, %LBL_LABEL1, LabelProc)     'direct Label1 msgs to SubClass Proc
          mSC_Set(hDlg, %LBL_LABEL2, LabelProc)     'direct Label2 msgs to (same) SubClass Proc
         
         DIALOG SHOW MODAL hDlg, CALL DialogProc
         
        END FUNCTION
        '------------------/PBMAIN
        BTW all forum message times are relative to GMT -4 until you log in - then they should be relative to your local time.
        Rgds, Dave

        Comment


        • #5
          Thank You Dave!

          It does indeed work as I need it too

          Had to subclass it. I would not have gone down that road, but it's a lesson.

          Again, thank you for your time in giving me the example.

          Bruce

          Comment


          • #6
            ok, and I thought it was in the bag.

            The previous problem is solved, no problem now on the focus issue.

            But...there is always a but isn't there...

            Normally after an item receives focus a tap on the space bar will set or un-set the checkbox, this works as well for true buttons.

            Now, I know these are labels but after receiving focus I need the label to respond as a button and report the space bar push.

            Not visually, but as if it was clicked on.

            ok, I'm sure I will need to do the "button push" part , but I now need to know when the users has pushed the space bar.

            How do I detect that?

            Thank's again

            Bruce

            Comment


            • #7
              You can't detect key presses on a label. (Label control cannot get focus).

              To ask the dumb question, why don't you just use buttons? You can always owner-draw them.

              I use the RRButton stuff written by Borje Hagsten. I don't have a link here but a search on 'RRButton' should find it for you.
              Michael Mattias
              Tal Systems (retired)
              Port Washington WI USA
              [email protected]
              http://www.talsystems.com

              Comment


              • #8
                You can't detect key presses on a label. (Label control cannot get focus).
                That is not correct.

                1. Create the labels with the SS_NOTIFY and WS_TABSTOP styles.
                2. Subclass them.
                3. Add this code in the subclass proc for the labels
                Code:
                  CASE %WM_LBUTTONDOWN
                    SetFocus hWnd
                The labels will now receive the focus when user tabs to or clicks on them.
                They will also receive keyboard messages(WM_KEYDOWN etc) and focus messages(WM_SETFOCUS, WM_KILLFOCUS etc.)

                You can also get a label to mimic more closely the behaviour of a button, but you will not be able to use
                DDT to do this.
                Last edited by Dominic Mitchell; 27 Mar 2008, 07:31 PM. Reason: forgot the SS_NOTIFY style
                Dominic Mitchell
                Phoenix Visual Designer
                http://www.phnxthunder.com

                Comment


                • #9
                  Bruce,
                  Can be done. Make this change to the subclass procedure..
                  Code:
                  CALLBACK FUNCTION LabelProc
                   LOCAL hWnd, hLABEL1, hLABEL2 AS DWORD
                    hWnd = GetParent(CBHNDL)
                    CONTROL HANDLE hWnd, %LBL_LABEL1 TO hLABEL1
                    CONTROL HANDLE hWnd, %LBL_LABEL2 TO hLABEL2
                    SELECT CASE AS LONG CBMSG
                      CASE %WM_KEYUP
                        SELECT CASE CBWPARAM
                          CASE %VK_SPACE
                            SELECT CASE GetFocus
                              CASE hLabel1
                                DIALOG SEND hWnd, %WM_COMMAND, MAKLNG(%LBL_LABEL1, %STN_CLICKED), %LBL_LABEL1
                              CASE hLabel2
                                DIALOG SEND hWnd, %WM_COMMAND, MAKLNG(%LBL_LABEL2, %STN_CLICKED), %LBL_LABEL2
                              FUNCTION = 0 : EXIT FUNCTION
                            END SELECT
                        END SELECT
                        ' Static / Label notification %STN_CLICKED = %BTN_CLICKED = 0 (Win32API.inc)
                      CASE %WM_SETFOCUS
                  ...
                  That will send a click to the label when the space bar is tapped.

                  Prefered route might be as MCM suggested - RRButton is available on Don Ewalls site: http://www.reonis.com/POFFS/index.htm
                  Rgds, Dave

                  Comment


                  • #10
                    Right on target!

                    Thank you Dave

                    Don't tell Michael it can be done.

                    Michael:

                    Show me a simple way to color and change the color on a button (Foreground and background). I don't like being forced to do owner drawn buttons just to color them.

                    Wow, MS though who would ever want to colorize a button.

                    If you do then jump though hoops.

                    Normally I don't need the tabstop info, so that was new to me, but I use the Label as a button all the time. Change colors, no problem

                    Bruce
                    Last edited by Bruce Wood; 27 Mar 2008, 10:04 PM.

                    Comment


                    • #11
                      Originally posted by Bruce Wood View Post
                      Right on target!

                      Michael:

                      Show me a simple way to color and change the color on a button (Foreground and background). I don't like being forced to do owner drawn buttons just to color them.


                      Bruce
                      You can always use a %BS_BITMAP button or a DDT IMGBUTTONX and this little utility for creating the bitmaps.

                      www.jcfuller.com/txt2bmpbut.zip

                      James

                      Comment


                      • #12
                        If there were a 'simple' way you'd have a more lucrative career asking me if I want fries with that. For that matter, owner-drawing a label is not all that hard. Off the top of my head I think it's probably only a couple of statements (CreateBrush, SelectObject, FillRect, Selectobject, DeleteObject, TextOut)

                        BTW, source for assertion re labels and focus is SDK:
                        Applications often use static controls to label other controls or to separate a group of controls. Although static controls are child windows, they cannot be selected. Therefore, they cannot receive the keyboard focus and cannot have a keyboard interface.
                        Michael Mattias
                        Tal Systems (retired)
                        Port Washington WI USA
                        [email protected]
                        http://www.talsystems.com

                        Comment


                        • #13
                          Unfortunately, that description in the SDK docs is not accurate.
                          When any child window has the WS_TABSTOP style, the OS calls SetFocus on the handle
                          of the child window when the user tabs to it. As a result, it will receive keyboard
                          messages.
                          Selection with the mouse is a different matter. The control, not the OS, is responsible
                          for calling SetFocus when it is clicked.
                          Dominic Mitchell
                          Phoenix Visual Designer
                          http://www.phnxthunder.com

                          Comment


                          • #14
                            Well, It can be done and Dave kindly provided the code to do it and it works great.

                            I certainly did not except this to start a war.

                            I respect Michael's input and all of his posts.

                            Thank you DAVE, Dominic, Michael, and Fred.

                            jcfuller: I'll checkout the bitmap utility when I pick up my fries

                            Bruce

                            Comment


                            • #15
                              Wouldn't the users of your program be more comfortable clicking on buttons than labels Bruce? Its my understanding that one can change the color of a button's background by processing the WM_CTLCOLORSTATIC message. At least that has always worked for me in the few occasions I have done it. Actually, I've only ever done it once, but that once is in a relatively important app my organization uses. At...

                              http://www.powerbasic.com/support/pb...ad.php?t=36717

                              is a demo (unfortunately, only SDK style) of using bit fields tied to check boxes (21 of them) where I changed the color of 6 of the 21 check boxes (check boxes are really modified buttons). What seems to happen there is that the label contained within the button gets its color changed. Anyway, I'm just thinking that buttons are more natural objects to click on.
                              Fred
                              "fharris"+Chr$(64)+"evenlink"+Chr$(46)+"com"

                              Comment


                              • #16
                                I stand corrected, embarrased, and humiliated! Please disregard my last post, for I now see that changing the background color of a button isn't so easy afterall. I was assumming that because a checkbox was of the same class "button" as an ordinary push button, that its color could be changed as easily. Not so. I tried and failed, then checked in Rector and Newcommer. No deal!
                                Fred
                                "fharris"+Chr$(64)+"evenlink"+Chr$(46)+"com"

                                Comment


                                • #17
                                  Fred:

                                  Well, my labels look a little bit like buttons.

                                  I looked over your very nice demo, but noticed that the button ( Display Bits ) which is where I'm at, is not colored.

                                  I need to be able to change both background and foreground colors.

                                  Thank you for your reply

                                  Bruce

                                  OOPS: And your second reply was on page two so I didn't see the disregard message.

                                  Your demo is still very informative!
                                  Last edited by Bruce Wood; 30 Mar 2008, 03:21 PM.

                                  Comment

                                  Working...
                                  X