0

What I'm trying to do is activate another application and send a key input to it to trigger a button. However this code doesn't seem to work. It looks like it can find the application but it does not activate it, and does not send the key.

[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}");
    }

And here is what I pull with Window Spy

Window Spy Info

Any help would be greatly appreciated. Thanks.

4

1 回答 1

0

想通了我的问题。

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
    }
于 2015-08-28T22:16:01.747 回答