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

时间:2015-03-18 14:34:54

标签: c#

我想在另一个应用程序中调用一个按钮,但是我无法做到这一点..在搜索之后我发现只有一个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

    }


}

1 个答案:

答案 0 :(得分:2)

我试图连接我们内部的自动GUI工具时遇到了这个问题。问题是由于QT模拟窗口的方式。为了查看Spy ++中的窗口句柄(以及各种Windows API函数),您需要添加一个环境变量:

(来自Windows 8.1):

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

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

  3. 添加名称为QT_USE_NATIVE_WINDOWS且值为1的新变量

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

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

    完成上述所有操作后,您应该可以使用Spy ++来获取按钮句柄。

相关问题