【问题标题】:Activate Window and Send Input using SetForegroundWindow and SendKeys in C#在 C# 中使用 SetForegroundWindow 和 SendKeys 激活窗口并发送输入
【发布时间】:2015-08-28 21:58:34
【问题描述】:

我想要做的是激活另一个应用程序并向其发送按键输入以触发按钮。但是,此代码似乎不起作用。看起来它可以找到应用程序,但它没有激活它,也没有发送密钥。

[DllImport("user32.dll", CharSet = CharSet.Unicode)]
    public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

[DllImport("user32.dll")]
    public static extern bool SetForegroundWindow(IntPtr hWnd);

private void debugButton_Click(object sender, EventArgs e)
    {
        //GetProcess by Class
        IntPtr rightNowHandle = FindWindow("WindowsForms10.Window.8.app.0.24dc298_r17_ad2", null);
        //Get Handle by Process
        Process proc = Process.GetProcessesByName("RightNow.CX")[0];
        IntPtr ptrFF = proc.MainWindowHandle;
        //Get a handle for the Calculator Application main window
        if (rightNowHandle == IntPtr.Zero)
        {
           MessageBox.Show("Could Not Find Right Now");
            return;
        }
        SetForegroundWindow(rightNowHandle); //Activate Handle By Class
        //SetForegroundWindow(ptrFF); //Activate Handle By Process
        SendKeys.SendWait("{F1}");
    }

这就是我用 Window Spy 拉的东西

任何帮助将不胜感激。谢谢。

【问题讨论】:

  • 而不是 rightNowHandle 尝试使用您根本没有使用的 ptrFF。
  • @Cistoran 您的进程是 32 位进程而 RighNow.CX.exe 进程是 64 位进程吗?
  • @JonathanCarroll 他们都是 64 位机器上的 32 位进程。
  • @Gusman 他们都不工作。我都试过了。
  • 也许你没有正确的类/名称,很久以前我通常玩那些 api,有时找到正确的窗口是一场噩梦(也许它是一个窗口内的窗口,或者它有一个代理窗口等)。尝试一件事,将你得到的句柄与 Spy++ 给出的句柄进行比较,如果相同,那么你确定这不是正确的类/名称。

标签: c# input sendkeys setforegroundwindow


【解决方案1】:

找出我的问题。

SetForegroundWindow 不显示应用程序,如果它被最小化,这是我所期待的。

接下来我使用Windows Input Simulator,而不是 SendKeys 来发送输入。

这是我最终使用的代码。

[DllImport("user32.dll", CharSet = CharSet.Unicode)]
    public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll")]
    private static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
[DllImport("user32.dll")]
    public static extern bool SetForegroundWindow(IntPtr hWnd);
private const int SW_RESTORE = 9;

private void debugButton_Click(object sender, EventArgs e)
    {
        //GetProcess by Class
        IntPtr rightNowHandle = FindWindow("WindowsForms10.Window.8.app.0.24dc298_r17_ad2", null);
        //Get Handle by Process
        Process proc = Process.GetProcessesByName("RightNow.CX")[0];
        IntPtr ptrFF = proc.MainWindowHandle;
        //Get a handle for the Calculator Application main window
        if (rightNowHandle == IntPtr.Zero)
        {
           MessageBox.Show("Could Not Find Right Now");
            return;
        }
        SetForegroundWindow(ptrFF); //Activate Handle By Process
        ShowWindow(ptrFF, SW_RESTORE); //Maximizes Window in case it was minimized.
        //SetForegroundWindow(rightNowHandle); //Activate Handle By Class
        InputSimulator.SimulateModifiedKeyStroke(VirtualKeyCode.LMENU, VirtualKeyCode.VK_A); //Send Left Alt + A
    }

【讨论】:

    猜你喜欢
    • 2013-04-11
    • 1970-01-01
    • 1970-01-01
    • 2018-04-19
    • 1970-01-01
    • 2021-08-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多