Announcement

Collapse
No announcement yet.

Simulate OK-button clicked [PBDLL60]

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

  • Simulate OK-button clicked [PBDLL60]

    How can I simulate that a dialog's OK-button has been activated already when the program starts?

    Here is the case:
    In a listbox the user can select an item, then click an OK-button in order to display a few lines of text. I want to simulate the most significant item being activated already on the fly when the application has been started. I know how to select an item in the listbox, but how do you simulate the buttonclick? And furthermore, how to implement the same effect when the item has been doubleclicked instead of the OK-button?

    Thanks,
    Egbert




    [This message has been edited by Egbert Zijlema (edited May 04, 2000).]

    Egbert Zijlema, journalist and programmer (zijlema at basicguru dot eu)
    http://zijlema.basicguru.eu
    *** Opinions expressed here are not necessarily untrue ***

  • #2
    Havent tested it, but it should work
    Code:
    If you have the CtrlId-number to the Button and listbox
    SendMessage(hDlg,%WM_COMMAND,MakDwd(%ButtonId,%BN_CLICKED),GetDlgItem(hDlg,%ButtonId))
    SendMessage(hDlg,%WM_COMMAND,MakDwd(%ListId,%LBN_DBLCLK),GetDlgItem(hDlg,%ListId))
    ....
    If you have the handle (hWnd) to Button and listbox 
    SendMessage(hDlg,%WM_COMMAND,MakDwd(GetDlgCtrlId(hWndButton),%BN_CLICKED),hWndButton)
    SendMessage(hDlg,%WM_COMMAND,MakDwd(GetDlgCtrlId(hWndList),%LBN_DBLCLK),hWndList)

    ------------------
    Fred
    mailto:[email protected][email protected]</A>
    http://www.oxenby.se



    [This message has been edited by Fred Oxenby (edited May 04, 2000).]
    Fred
    mailto:[email protected][email protected]</A>
    http://www.oxenby.se

    Comment


    • #3
      To automate a button click, use this:

      SendDlgItemMessage hDlg, %IDOK, %BM_CLICK, 0, 0



      ------------------
      Kev G Peel
      KGP Software
      Bridgwater, UK.
      mailto:[email protected][email protected]</A>

      Comment


      • #4
        The correct way is to send a %BM_CLICK message to the control.

        It is not usual for a %BN_CLICKED message (which is a *notification* message) to be sent programmatically - it is automatically sent by the control's handler in response to a %BM_CLICK to originate the %BN_CLICKED message. The exception for this would be if your code *is* the handler for a custom control. In general, notification messages such as those which are prefixed with %BN_ should not be sent by your code.

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

        Comment


        • #5
          But,
          If you are sending
          SendDlgItemMessage hDlg, %IDOK, %BM_CLICK, 0, 0
          You have to make sure the dialog is in foreground focus...


          ------------------
          Fred
          mailto:[email protected][email protected]</A>
          http://www.oxenby.se

          Fred
          mailto:[email protected][email protected]</A>
          http://www.oxenby.se

          Comment


          • #6
            PostMessage is much better

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

            Comment


            • #7
              Thank you all, folks.

              K. Peel's solution pointed me into the good direction. Instead of his API-call, I prefer the use of "CONTROL SEND CBHNDL....."

              Regards,
              Egbert

              BTW: I don't need any help to perform the doubleclick message. Found it myself (in one of my own applications!!!!!)


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

              Egbert Zijlema, journalist and programmer (zijlema at basicguru dot eu)
              http://zijlema.basicguru.eu
              *** Opinions expressed here are not necessarily untrue ***

              Comment


              • #8
                > You have to make sure the dialog is in foreground focus

                I don't believe that that's true, since you are sending the message directly to the handle of the dialog. Have you experienced failures when the dialog was not in the foreground, or...?

                -- Eric


                ------------------
                Perfect Sync: Perfect Sync Development Tools
                Email: mailto:[email protected][email protected]</A>

                "Not my circus, not my monkeys."

                Comment


                • #9
                  Originally posted by Fred Oxenby:
                  But, If you are sending SendDlgItemMessage hDlg, %IDOK, %BM_CLICK, 0, 0 You have to make sure the dialog is in foreground focus...
                  Really??? I don't think so Fred.

                  In the following code, the control is triggered fine for me even if the dialog is obscured and without focus. Tested on Win2K.

                  Note, Semen made a good point here too - if you are not 100% sure what will happen when the button message is sent, the use of PostMessage() may prevent your application from freezing until the SendMessage() function returns control [to the calling app]. CONTROL SEND encapsulates SendMessage(), so this applies to this too.
                  Code:
                  #COMPILE EXE
                  #INCLUDE "win32api.inc"
                   
                  CALLBACK FUNCTION BTN()
                    IF CBMSG = %WM_COMMAND AND CBCTLMSG = %BN_CLICKED THEN _
                        MSGBOX "TADA! we got a %BN_CLICKED message!"
                  END FUNCTION
                   
                  CALLBACK FUNCTION dlgcb
                    IF CBMSG = %WM_INITDIALOG THEN
                        SetTimer CBHNDL, 1, 5000, BYVAL %NULL
                    ELSEIF CBMSG = %WM_DESTROY THEN
                        KillTImer CBHNDL, 1
                    ELSEIF CBMSG = %WM_TIMER THEN
                      CONTROL SEND CBHNDL, 101, %BM_CLICK, 0, 0
                    END IF
                  END FUNCTION
                   
                  FUNCTION PBMAIN()AS LONG
                    DIALOG NEW 0&, "%BM_CLICK test", 10,60,100,120, %WS_SYSMENU TO hDlg&
                    CONTROL ADD BUTTON, hDlg&,101, "Trigger MSGBOX",10,30,80,15,, CALL BTN
                    DIALOG SHOW MODAL hDlg& CALL dlgcb
                  END FUNCTION


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

                  Comment


                  • #10
                    I make use of postmsg and sending a wm_keydown and up (VK_SPACE)

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

                    Comment


                    • #11
                      I was merly quoting MSDN, perhaps I'm wrong
                      Remarks
                      If the button is in a dialog box and the dialog box is not active, the BM_CLICK message might fail. To ensure success in this situation, call the SetActiveWindow function to activate the dialog box before sending the BM_CLICK message to the button.
                      Using SendDlgItemMessage is identical to retrieving a handle to the specified control and calling the SendMessage function

                      My example-code was a demonstration how to 'put things' together
                      and use send-message.
                      ------------------
                      Fred
                      mailto:[email protected][email protected]</A>
                      http://www.oxenby.se



                      [This message has been edited by Fred Oxenby (edited May 05, 2000).]
                      Fred
                      mailto:[email protected][email protected]</A>
                      http://www.oxenby.se

                      Comment


                      • #12
                        Fred --

                        I can see where that MSDN quote could be misleading. To clarify...

                        The "focus" and the "forground" are two different things. On a dialog that has more than one control, one control at a time has "the focus". You can usually change the focus from control to control with the tab key, and the control with the focus (such as a button) is often drawn with a slightly thicker border. But if there are many different programs running at the same time, with many different dialogs or windows visible, each dialog or window has a control that has the "focus" for that dialog or window.

                        But only one window, system-wide, has the "foreground" at any given time. If you press a key like Enter, the window that currently owns the foreground is the only window that sees it, and the control that has the focus on the dialog that has the foreground is the one that is activated.

                        So your phrase "foreground focus" caused Lance and me to become unfocused and come out of the background...

                        -- Eric


                        ------------------
                        Perfect Sync: Perfect Sync Development Tools
                        Email: mailto:[email protected][email protected]</A>



                        [This message has been edited by Eric Pearson (edited May 05, 2000).]
                        "Not my circus, not my monkeys."

                        Comment


                        • #13
                          Sorry, but this was probably not be the last time my limited
                          knowledge in the english language will let frogs jump out of my mouth......
                          (oops)
                          How do you simulate item_dblclk in a listbox without sending
                          the WM_Command notification-message?


                          ------------------
                          Fred
                          mailto:[email protected][email protected]</A>
                          http://www.oxenby.se



                          [This message has been edited by Fred Oxenby (edited May 05, 2000).]
                          Fred
                          mailto:[email protected][email protected]</A>
                          http://www.oxenby.se

                          Comment


                          • #14
                            To send event is easy and not interesting.
                            Much better to imitate cursor movement and mouse click (to see visual effect). Somebody tried to do this ?

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

                            Comment


                            • #15
                              Maybe try using mouse_event()? That is exactly what the mouse_event() API is designed to do. It is the mouse equivalent of the keydb_event() API.

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

                              Comment

                              Working...
                              X