C# 在.NET中使用Win32 API模拟按钮单击
我想在另一个应用程序中调用一个按钮,但我无法这样做。。经过搜索,我发现只有一个API可以做到这一点。。这是SendMessage API,但我无法使用它。。。 我开发了一个与Viber应用程序(如whatsapp等社交应用程序)交互的windows应用程序。我想通过一个文本框中的数字,并点击聊天室的标志写消息,然后发送它。。。需要帮忙吗 这是我的示例代码C# 在.NET中使用Win32 API模拟按钮单击,c#,C#,我想在另一个应用程序中调用一个按钮,但我无法这样做。。经过搜索,我发现只有一个API可以做到这一点。。这是SendMessage API,但我无法使用它。。。 我开发了一个与Viber应用程序(如whatsapp等社交应用程序)交互的windows应用程序。我想通过一个文本框中的数字,并点击聊天室的标志写消息,然后发送它。。。需要帮忙吗 这是我的示例代码 namespace ViberMess public partial class Form1 : Form { // Get a h
namespace ViberMess
public partial class Form1 : Form
{
// Get a handle to an application window.
[DllImport("USER32.DLL", CharSet = CharSet.Unicode)]
public static extern IntPtr FindWindow(string lpClassName,
string lpWindowName);
// Activate an application window.
[DllImport("USER32.DLL")]
public static extern bool SetForegroundWindow(IntPtr hWnd);
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
// Get a handle to the viber application. The window class
// and window name were obtained using the Spy++ tool.
IntPtr calculatorHandle = FindWindow("Qt5QWindowIcon", "Viber +2xxxxxxxxx");
// Verify that Viber is a running process.
if (calculatorHandle == IntPtr.Zero)
{
MessageBox.Show("Viber is not running.");
return;
}
// Make Viber the foreground application and send it
// phone number
SetForegroundWindow(calculatorHandle);
//Send CTRL+d to open dialled windows
SendKeys.SendWait("^d");
//Pass phone number
SendKeys.SendWait("+2010xxxxxxxx");
//Here is i want to click on message logo then type text and press Enter
//My problem how to click on message logo to type text
}
}
我在尝试连接我们内部的自动化GUI工具时遇到了这个问题。问题在于QT模拟窗口的方式。要在Spy++(以及各种Windows API函数)中查看窗口句柄,您需要添加一个环境变量: (来自Windows 8.1):
完成以上所有操作后,您应该能够使用Spy++来获取按钮手柄。为什么您不能使用
SendMessage
您的意思是您不能或不知道如何使用。。请看这里是的,我不知道如何找到聊天按钮是一个完整的框架,它允许您在比试图模拟windows消息更高的语义级别上自动化其他应用程序。所以我不同意“只有一个API”。它确实有一点学习曲线,但它是为这类工作设计的。通过使用Spy++我无法在Viberth中获取按钮id消息将是WM\u NOTIFY
,参数为BN\u CLICKED
,控件id。这是你应该在Spy++中查找的内容,也是你将使用SendMessage
i soooo的内容你知道很多爱。我花了大约2天的时间研究这个问题,但在网上找不到有意义的提示。环境变量提示刚刚拯救了我的一天。非常感谢。该解决方案到现在(2020年)仍然有效,但问题是对话框全部呈现为黑色