471,319 Members | 2,201 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,319 software developers and data experts.

clicking button in external app

Hello everyone, my program is supposed to click a button on another
application when I click a button on my application. If I use a spy
program to get its identity, how can I do this? I know I should use
winapi and the interop services, but I cant quite set the code up to
work. Ive managed to get the window focus so far with winapi, so all I
need to do is execute a specific button. any know how to do this when I
click the button on my program?

Feb 3 '06 #1
7 7643
Hello, blah!

b> Hello everyone, my program is supposed to click a button on another
b> application when I click a button on my application. If I use a spy
b> program to get its identity, how can I do this? I know I should use
b> winapi and the interop services, but I cant quite set the code up to
b> work. Ive managed to get the window focus so far with winapi, so all I
b> need to do is execute a specific button. any know how to do this when I
b> click the button on my program?

You have to get the window handle of the button, and then call SendMessage on this handle with
BM_CLICK message.

You can use Spy++ took to get window name of the button.

--
Regards, Vadym Stetsyak
www: http://vadmyst.blogspot.com
Feb 3 '06 #2
blah,

What you want to do is to define the SendMessage API function and send
the BM_CLICK message to the button (assuming you have the handle to it).

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"blah" <so********@yahoo.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
Hello everyone, my program is supposed to click a button on another
application when I click a button on my application. If I use a spy
program to get its identity, how can I do this? I know I should use
winapi and the interop services, but I cant quite set the code up to
work. Ive managed to get the window focus so far with winapi, so all I
need to do is execute a specific button. any know how to do this when I
click the button on my program?

Feb 3 '06 #3
Thank you for the replies, Im not at my computer right now but would
the code by any chance look something like this (upon button click):
{
HWND w = FindWindow("Window Class", NULL);
{
SendMessage(w, WM_LBUTTONDOWN, 0, 0);
}
}
unfortunately I cant try it right now, but since it looks so simple Im
guessing its wrong anyway?

Feb 3 '06 #4
blah,

Yes, it will look like that, but you have to convert it to .NET.

Also, passing WM_LBUTTONDOWN is INCORRECT. You need to pass BM_CLICK.
This will correctly simulate a click on the button (the button down event,
the click event, the button up event, etc, etc).
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"blah" <so********@yahoo.com> wrote in message
news:11*********************@o13g2000cwo.googlegro ups.com...
Thank you for the replies, Im not at my computer right now but would
the code by any chance look something like this (upon button click):
{
HWND w = FindWindow("Window Class", NULL);
{
SendMessage(w, WM_LBUTTONDOWN, 0, 0);
}
}
unfortunately I cant try it right now, but since it looks so simple Im
guessing its wrong anyway?

Feb 3 '06 #5
Oh yes, BM_CLICK!, I was thinking too much into it. I will give it a
try when I can. Thanks for the assistance!

Feb 3 '06 #6
How does find window work if the button is in a differnt application? What
if the app is currntly showing mulitple buttons? How do you know you got the
right one?

Also SendMessage, I'm guessing you have to import it using
System.Runtime.InteropServices?

I ask as I have a whole set of routines that I DLL Imported a bunch of APIs
for doing this, EnumWindows, EnumChildern, Send/Post Message, and defined a
bunch of constants for the messages.

Thanks
Wayne Sepega

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:e4**************@TK2MSFTNGP10.phx.gbl...
blah,

Yes, it will look like that, but you have to convert it to .NET.

Also, passing WM_LBUTTONDOWN is INCORRECT. You need to pass BM_CLICK.
This will correctly simulate a click on the button (the button down event,
the click event, the button up event, etc, etc).
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"blah" <so********@yahoo.com> wrote in message
news:11*********************@o13g2000cwo.googlegro ups.com...
Thank you for the replies, Im not at my computer right now but would
the code by any chance look something like this (upon button click):
{
HWND w = FindWindow("Window Class", NULL);
{
SendMessage(w, WM_LBUTTONDOWN, 0, 0);
}
}
unfortunately I cant try it right now, but since it looks so simple Im
guessing its wrong anyway?


Feb 3 '06 #7
Wayne,
How does find window work if the button is in a differnt application? What
if the app is currntly showing mulitple buttons? How do you know you got
the right one?
That's up to you to figure out. You can traverse the handle tree and
try to filter it out yourself, or use FindWindow. However, it's not easy to
do, and you have to be very sure of which window you are trying to
communicate to (which is a reason why as a cross process mechanism, this
isn't the best, but sometimes, it is the only way).

Also SendMessage, I'm guessing you have to import it using
System.Runtime.InteropServices?
Yes, the attributes defined in there will help you define SendMessage
for use in C# code.
I ask as I have a whole set of routines that I DLL Imported a bunch of
APIs for doing this, EnumWindows, EnumChildern, Send/Post Message, and
defined a bunch of constants for the messages.
You might want to check http://www.pinvoke.net, as it will have a good
number of definitions (constants, functions and structs) there as well.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

Thanks
Wayne Sepega

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote
in message news:e4**************@TK2MSFTNGP10.phx.gbl...
blah,

Yes, it will look like that, but you have to convert it to .NET.

Also, passing WM_LBUTTONDOWN is INCORRECT. You need to pass BM_CLICK.
This will correctly simulate a click on the button (the button down
event, the click event, the button up event, etc, etc).
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"blah" <so********@yahoo.com> wrote in message
news:11*********************@o13g2000cwo.googlegro ups.com...
Thank you for the replies, Im not at my computer right now but would
the code by any chance look something like this (upon button click):
{
HWND w = FindWindow("Window Class", NULL);
{
SendMessage(w, WM_LBUTTONDOWN, 0, 0);
}
}
unfortunately I cant try it right now, but since it looks so simple Im
guessing its wrong anyway?



Feb 3 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

13 posts views Thread by Botao | last post: by
10 posts views Thread by Jim Bayers | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.