简体   繁体   English

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

[英]Simulating button click using Win32 API in .NET

i want to invoke a button in another app but i can't to do it .. after searched i found only one API can do this .. it is SendMessage API but i can't to ues it ... i develop an windows app that interact with Viber app ( social application like whatsapp ) . 我想调用另一个应用程序中的按钮,但我无法执行..搜索后,我发现只有一个API可以执行此操作..它是SendMessage API,但我无法使用它...我开发了Windows与Viber应用程序交互的应用程序(社交应用程序,如whatsapp)。 i want to pass number in a text box and click on chat logo to write message then sending it ... any help please 我想在文本框中传递数字,然后单击聊天徽标以写消息,然后发送...任何帮助,请

here is my sample code 这是我的示例代码

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

    }


}

I ran into this problem when trying to hook up an automated GUI tool we have in house. 在尝试连接内部的自动GUI工具时遇到了这个问题。 The problem is due to the way QT emulates windowing. 问题是由于QT仿真窗口的方式引起的。 In order to see window handles in Spy++ (and various Windows API functions) you will need to add an environment variable: 为了查看Spy ++(和各种Windows API函数)中的窗口句柄,您将需要添加一个环境变量:

(From Windows 8.1): (从Windows 8.1):

  1. Go to Control Panel\\System and Security\\System\\Advanced System Settings... 转到控制面板\\系统和安全性\\系统\\高级系统设置...

  2. On the Advanced tab hit Environment Variables 在高级选项卡上,单击环境变量

  3. Add a new variable with a name of QT_USE_NATIVE_WINDOWS and value of 1 添加一个名称为QT_USE_NATIVE_WINDOWS且值为1的新变量

For user variables you need to restart visual studio 对于用户变量,您需要重新启动Visual Studio

For environment variables you may need to restart the machine 对于环境变量,您可能需要重新启动机器

Once all of the above is done you should be able to use Spy++ to get the button handles. 完成上述所有操作后,您应该可以使用Spy ++来获取按钮句柄。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM