在另一个应用程序点击一个按钮 [英] Click a button in another application
问题描述
我想哟使用 SendMessage函数
或 PostMessage的
在另一个应用程序按一个按钮
I want yo use SendMessage
or PostMessage
to press a button in another app
我有一个示例代码通过获取窗口句柄要做到这一点,但它不能正常工作
i have a sample code to do this by getting Window Handle, but it doesn't work
此外,我使用了WinDowse来获得所需的信息。这里是代码
also i used "WinDowse" to get required info. here is the code
private const uint BM_CLICK = 0x00F5;
private const uint WM_LBUTTONDOWN = 0x0201;
private const uint WM_LBUTTONUP = 0x0202;
private void PushOKButton(IntPtr ptrWindow)
{
WindowHandle = FindWindow(null, "Form1");
if (ptrWindow == IntPtr.Zero)
return;
IntPtr ptrOKButton = FindWindowEx(ptrWindow, IntPtr.Zero, "Button", "&Yes");
if (ptrOKButton == IntPtr.Zero)
return;
SendMessage(ptrOKButton, WM_LBUTTONDOWN, 0, 0);
SendMessage(ptrOKButton, WM_LBUTTONUP, 0, 0);
SendMessage(ptrOKButton, BM_CLICK, 0, 0);
}
有一个Compelete Suloution在C#?
is There a Compelete Suloution in c# ?
推荐答案
您有正确的总体思路。
总有一些技巧,以自动化。
You have the right general idea. There are always tricks to automation.
按钮下/上是preatty接近点击的原始作用,但并不完全一样。
你将要考虑
Button Down/Up is preatty close to the raw action of clicking, but not quite the same. You will want to consider
- 的COORDS在那里你点击 - 某些按钮在0,0不喜欢边点击
- 鼠标的位置 - 鼠标在开始或结束按钮以外会导致其无法正常工作
- 时间点击 - 有时一个快速的点击是忽视,加比如说为10ms
BM_CLICK是做一个按钮鼠标点击时发送一个额外的Win32消息 - 它是简短的表达方式,更容易,如果控制是单独使用此权利类型。
BM_Click is an extra win32 message that is sent when doing a mouse click on a button - it's the shorter method and easier if the controls are the right type to use this alone.
一些努力,以防止一些像从国外的应用程序密码编辑使用WM_GETTEXT,警惕你可能还取决于目的地的应用程序的问题。
Some effort is made to prevent things like using WM_GetText on password edits from foreign apps, be wary you may also have issues depending on the destination app.
不幸的是我对你没有C#示例代码现在。
Unfortunately I have no C# sample code for you right now.
也许你会考虑的AutoIt 以节省一些时间。
Perhaps you might look into AutoIT to save some time.
这篇关于在另一个应用程序点击一个按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!