简体   繁体   中英

Activate Window and Send Input using SetForegroundWindow and SendKeys in C#

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

窗口间谍信息

Any help would be greatly appreciated. Thanks.

Figured out my issue.

SetForegroundWindow does not show the application if it's minimized, which is what I was anticipating.

Next I used Windows Input Simulator , to send the input instead of SendKeys.

Here's the code I ended up using.

[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
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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