使用中的Win32 API模拟按钮单击.NET



我想调用另一个应用程序中的按钮,但我做不到。。经过搜索,我发现只有一个API可以做到这一点。。它是SendMessage API,但我不能使用它。。。我开发了一个与Viber应用程序(类似whatsapp的社交应用程序)交互的windows应用程序。我想把号码放在一个文本框里,点击聊天标志写下信息,然后发送。。。请帮忙

这是我的样本代码

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):

  1. 转到"控制面板\系统和安全\系统\高级系统设置…"。。。

  2. 在高级选项卡上点击环境变量

  3. 添加一个名为QT_USE_NAMETIVE_WINDOWS、值为1 的新变量

对于用户变量,您需要重新启动visual studio

对于环境变量,您可能需要重新启动机器

完成以上所有操作后,您应该能够使用Spy++获得按钮手柄。

最新更新