Clicking a button with SendMessage [modified]
-
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
-
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
-
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
-
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
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.
-
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
-
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
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
-
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
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
-
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