Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Clicking a button with SendMessage [modified]

Clicking a button with SendMessage [modified]

Scheduled Pinned Locked Moved C#
csharptestingquestionwinformsbeta-testing
9 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    musefan
    wrote on last edited by
    #1

    Hi All, I am trying to simulate a button click using the SendMessage function of the Win32API. My application is C# WinForms .net 4.0 (the application performing the automation) EDIT: The application I am trying to automate is not of my own work and I have no control over source code in any way. First of all I am using FindWindow to get the handle to the window I want, I know this is working as the value of the handle is correct (as found using Spy++) Next I am getting the handle of the child control (the button I want to click), again I know I am getting the correct value here. Then I use SetActiveWindow() and pass in the window handle (which appears to work fine) Then I use SendMessage() to simulate a mouse left button down and a mouse left button up. For now I just want to simulate the "Cancel" button for testing, which when clicked manually will simply close the form. My code however does nothing, the thing that is annoying me thou is that the "Cancel" button actually gets focus (just like it would if it had been clicked manually) but no action is taken. Does anybody know why this could be happening? Is it possible that the application is question could be detecting I am using the SendMessage function and deciding not to process the relevant code? sort of like a "prevent automation" feature. Thanks for any help, and the relevant code for SendMessage is as follows (also note, I have tried PostMessage too)

    [DllImport("user32.dll")]
    public static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);

    uint WM_LBUTTONDOWN = 0x0201;
    uint WM_LBUTTONUP = 0x0202;
    uint BM_SETSTATE = 0x00F3;
    uint BN_CLICKED = 245;

    void Function()
    {
    //Code to get handles here

     //I have tried all variations of the following (so with some commented etc.)
     SendMessage(hwndButton, WM\_LBUTTONDOWN, IntPtr.Zero, IntPtr.Zero);
     SendMessage(hwndButton, WM\_LBUTTONUP, IntPtr.Zero, IntPtr.Zero);
     SendMessage(hwndButton, BM\_SETSTATE, IntPtr.Zero, IntPtr.Zero);
     SendMessage(hwndButton, BN\_CLICKED, IntPtr.Zero, IntPtr.Zero);
    

    }

    I may or may not be responsible for my own actions

    modified on Tuesday, May 31, 2011 11:59 AM

    L P _ 3 Replies Last reply
    0
    • M musefan

      Hi All, I am trying to simulate a button click using the SendMessage function of the Win32API. My application is C# WinForms .net 4.0 (the application performing the automation) EDIT: The application I am trying to automate is not of my own work and I have no control over source code in any way. First of all I am using FindWindow to get the handle to the window I want, I know this is working as the value of the handle is correct (as found using Spy++) Next I am getting the handle of the child control (the button I want to click), again I know I am getting the correct value here. Then I use SetActiveWindow() and pass in the window handle (which appears to work fine) Then I use SendMessage() to simulate a mouse left button down and a mouse left button up. For now I just want to simulate the "Cancel" button for testing, which when clicked manually will simply close the form. My code however does nothing, the thing that is annoying me thou is that the "Cancel" button actually gets focus (just like it would if it had been clicked manually) but no action is taken. Does anybody know why this could be happening? Is it possible that the application is question could be detecting I am using the SendMessage function and deciding not to process the relevant code? sort of like a "prevent automation" feature. Thanks for any help, and the relevant code for SendMessage is as follows (also note, I have tried PostMessage too)

      [DllImport("user32.dll")]
      public static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);

      uint WM_LBUTTONDOWN = 0x0201;
      uint WM_LBUTTONUP = 0x0202;
      uint BM_SETSTATE = 0x00F3;
      uint BN_CLICKED = 245;

      void Function()
      {
      //Code to get handles here

       //I have tried all variations of the following (so with some commented etc.)
       SendMessage(hwndButton, WM\_LBUTTONDOWN, IntPtr.Zero, IntPtr.Zero);
       SendMessage(hwndButton, WM\_LBUTTONUP, IntPtr.Zero, IntPtr.Zero);
       SendMessage(hwndButton, BM\_SETSTATE, IntPtr.Zero, IntPtr.Zero);
       SendMessage(hwndButton, BN\_CLICKED, IntPtr.Zero, IntPtr.Zero);
      

      }

      I may or may not be responsible for my own actions

      modified on Tuesday, May 31, 2011 11:59 AM

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      Did you try BM_CLICK instead?

      M 1 Reply Last reply
      0
      • L Lost User

        Did you try BM_CLICK instead?

        M Offline
        M Offline
        musefan
        wrote on last edited by
        #3

        I think BM_CLICK is the same as I have BN_CLICKED (seen different name in different places). I have used a value of 245, if you know a different value for BM_CLICK then please let me know and I will give that a try

        I may or may not be responsible for my own actions

        L 2 Replies Last reply
        0
        • M musefan

          I think BM_CLICK is the same as I have BN_CLICKED (seen different name in different places). I have used a value of 245, if you know a different value for BM_CLICK then please let me know and I will give that a try

          I may or may not be responsible for my own actions

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          Not sure if your BN_CLICKED and BM_CLICK are same, but here is what the documentation at http://msdn.microsoft.com/en-us/library/bb775985(VS.85).aspx[^] says: "Simulates the user clicking a button. This message causes the button to receive the WM_LBUTTONDOWN and WM_LBUTTONUP messages, and the button's parent window to receive a BN_CLICKED notification code." I believe that the parent must be notified of the event for it to handle the event and BM_CLICK does this.

          1 Reply Last reply
          0
          • M musefan

            I think BM_CLICK is the same as I have BN_CLICKED (seen different name in different places). I have used a value of 245, if you know a different value for BM_CLICK then please let me know and I will give that a try

            I may or may not be responsible for my own actions

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            And I just verified that BM_CLICK is 245 (0x00F5) in winuser.h

            M 1 Reply Last reply
            0
            • L Lost User

              And I just verified that BM_CLICK is 245 (0x00F5) in winuser.h

              M Offline
              M Offline
              musefan
              wrote on last edited by
              #6

              Hmmm, so this is what I am using for BN_CLICKED so this should be the same as you suggested then :(

              I may or may not be responsible for my own actions

              1 Reply Last reply
              0
              • M musefan

                Hi All, I am trying to simulate a button click using the SendMessage function of the Win32API. My application is C# WinForms .net 4.0 (the application performing the automation) EDIT: The application I am trying to automate is not of my own work and I have no control over source code in any way. First of all I am using FindWindow to get the handle to the window I want, I know this is working as the value of the handle is correct (as found using Spy++) Next I am getting the handle of the child control (the button I want to click), again I know I am getting the correct value here. Then I use SetActiveWindow() and pass in the window handle (which appears to work fine) Then I use SendMessage() to simulate a mouse left button down and a mouse left button up. For now I just want to simulate the "Cancel" button for testing, which when clicked manually will simply close the form. My code however does nothing, the thing that is annoying me thou is that the "Cancel" button actually gets focus (just like it would if it had been clicked manually) but no action is taken. Does anybody know why this could be happening? Is it possible that the application is question could be detecting I am using the SendMessage function and deciding not to process the relevant code? sort of like a "prevent automation" feature. Thanks for any help, and the relevant code for SendMessage is as follows (also note, I have tried PostMessage too)

                [DllImport("user32.dll")]
                public static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);

                uint WM_LBUTTONDOWN = 0x0201;
                uint WM_LBUTTONUP = 0x0202;
                uint BM_SETSTATE = 0x00F3;
                uint BN_CLICKED = 245;

                void Function()
                {
                //Code to get handles here

                 //I have tried all variations of the following (so with some commented etc.)
                 SendMessage(hwndButton, WM\_LBUTTONDOWN, IntPtr.Zero, IntPtr.Zero);
                 SendMessage(hwndButton, WM\_LBUTTONUP, IntPtr.Zero, IntPtr.Zero);
                 SendMessage(hwndButton, BM\_SETSTATE, IntPtr.Zero, IntPtr.Zero);
                 SendMessage(hwndButton, BN\_CLICKED, IntPtr.Zero, IntPtr.Zero);
                

                }

                I may or may not be responsible for my own actions

                modified on Tuesday, May 31, 2011 11:59 AM

                P Offline
                P Offline
                Pete OHanlon
                wrote on last edited by
                #7

                Why are you attempting to go the long way round to automate the application? There's a feature called Microsoft Active Accessibility (MSAA)[^] which provides accessibility for WinForms applications. This allows you to get access to controls, and automate them.

                Forgive your enemies - it messes with their heads

                My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility

                M 1 Reply Last reply
                0
                • P Pete OHanlon

                  Why are you attempting to go the long way round to automate the application? There's a feature called Microsoft Active Accessibility (MSAA)[^] which provides accessibility for WinForms applications. This allows you to get access to controls, and automate them.

                  Forgive your enemies - it messes with their heads

                  My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility

                  M Offline
                  M Offline
                  musefan
                  wrote on last edited by
                  #8

                  Thanks for your reply, but I think you think this application I want to automate is my own application? Just to clear it up, this is a 3rd party application which I have no control over source code in any way. Perhaps I am wrong and there is more to the information you linked, but it appears these are guidelines on how to create an application that allows for programmatic calls to achieve automation, right?

                  I may or may not be responsible for my own actions

                  1 Reply Last reply
                  0
                  • M musefan

                    Hi All, I am trying to simulate a button click using the SendMessage function of the Win32API. My application is C# WinForms .net 4.0 (the application performing the automation) EDIT: The application I am trying to automate is not of my own work and I have no control over source code in any way. First of all I am using FindWindow to get the handle to the window I want, I know this is working as the value of the handle is correct (as found using Spy++) Next I am getting the handle of the child control (the button I want to click), again I know I am getting the correct value here. Then I use SetActiveWindow() and pass in the window handle (which appears to work fine) Then I use SendMessage() to simulate a mouse left button down and a mouse left button up. For now I just want to simulate the "Cancel" button for testing, which when clicked manually will simply close the form. My code however does nothing, the thing that is annoying me thou is that the "Cancel" button actually gets focus (just like it would if it had been clicked manually) but no action is taken. Does anybody know why this could be happening? Is it possible that the application is question could be detecting I am using the SendMessage function and deciding not to process the relevant code? sort of like a "prevent automation" feature. Thanks for any help, and the relevant code for SendMessage is as follows (also note, I have tried PostMessage too)

                    [DllImport("user32.dll")]
                    public static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);

                    uint WM_LBUTTONDOWN = 0x0201;
                    uint WM_LBUTTONUP = 0x0202;
                    uint BM_SETSTATE = 0x00F3;
                    uint BN_CLICKED = 245;

                    void Function()
                    {
                    //Code to get handles here

                     //I have tried all variations of the following (so with some commented etc.)
                     SendMessage(hwndButton, WM\_LBUTTONDOWN, IntPtr.Zero, IntPtr.Zero);
                     SendMessage(hwndButton, WM\_LBUTTONUP, IntPtr.Zero, IntPtr.Zero);
                     SendMessage(hwndButton, BM\_SETSTATE, IntPtr.Zero, IntPtr.Zero);
                     SendMessage(hwndButton, BN\_CLICKED, IntPtr.Zero, IntPtr.Zero);
                    

                    }

                    I may or may not be responsible for my own actions

                    modified on Tuesday, May 31, 2011 11:59 AM

                    _ Offline
                    _ Offline
                    _Erik_
                    wrote on last edited by
                    #9

                    Have you tried SendInput[^] function?

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups