简体   繁体   English

将另一个应用程序置于前台并向其发送输入

[英]Bringing another application into foreground and sending input to it

I am trying to restore( or even maximize ) another application and send input to it from my project. 我正在尝试还原(甚至最大化)另一个应用程序,并从我的项目中向其发送输入。 I can do it for certain applications like Notepad or Skype , but it doesn't work for other applications such as TeamSpeak . 我可以对某些应用程序(例如NotepadSkype ,但不适用于TeamSpeak等其他应用程序。
Any ideas why and how I can solve the problem ? 有什么想法为什么以及如何解决这个问题?

Here's my code : 这是我的代码:

private void winact()
{       
    IntPtr hWnd; //change this to IntPtr
    Process[] processRunning = Process.GetProcesses();

    string title, name;
    foreach (Process pr in processRunning)
    {
        title = pr.MainWindowTitle.ToLower();
        name = pr.ProcessName.ToLower();

        if (title.Contains("teamspeak".ToLower()) || name.Contains("teamspeak".ToLower()))
        {
            hWnd = pr.MainWindowHandle;
            ShowWindow(hWnd, 3);
            SetForegroundWindow(hWnd); //set to topmost
            break;
        }
    }
}  

I use InputSimulator to send the input. 我使用InputSimulator发送输入。

Could you just try this? 您可以尝试一下吗? I think this should work for you. 我认为这应该为您工作。

private void winact()
{
    IntPtr hWnd; //change this to IntPtr
    Process[] processRunning = Process.GetProcesses();

    string title, name;
    foreach (Process pr in processRunning)
    {
        title = pr.MainWindowTitle.ToLower();
        name = pr.ProcessName.ToLower();

        if (title.Contains("teamspeak".ToLower()) || name.Contains("teamspeak".ToLower()))
        {
            hWnd = pr.MainWindowHandle;
            HwndSource hwndSource = HwndSource.FromHwnd(hWnd);
            Window window = hwndSource.RootVisual as Window;
            window.Activate();
            window.TopMost = true;

            ShowWindow(hWnd, 3);
            SetForegroundWindow(hWnd); //set to topmost
            break;
        }
    }
}

Apparently the problem was that my application did not run with administrator rights. 显然问题是我的应用程序没有以管理员权限运行。 In order to be able to send input to other apps efficiently, the role of administrator is required. 为了能够将输入有效地发送到其他应用,需要管理员角色。

To do this, I've added an app.manifest to the application, and changed requestedExecutionLevel level from asInvoker to requireAdministrator . 为此,我向应用程序添加了一个app.manifest ,并将asInvoker requestedExecutionLevel levelasInvoker更改为requireAdministrator
Using InputSimulator worked for other applications, but certain applications, such as games, consider the input to be a simple message and do not process keystrokes as actions. 使用InputSimulator在其他应用程序上使用,但是某些应用程序(例如游戏)将输入视为简单消息,并且不会将击键作为动作进行处理。 They are however passed to the chat. 但是,他们被传递到聊天室。 (this is what I've noticed) (这是我注意到的)

In order to solve this issue, I have written a very script in autoIT which is simply taking the parameter passed to it (only 1) and sends the parameter as a keystroke to the window, currently in foreground. 为了解决此问题,我在autoIT中编写了一个非常脚本,该脚本只是采用传递给它的参数(只有1)并将该参数作为按键发送到当前位于前景的窗口中。 The script is compiled resulting in an executable which I am calling. 该脚本已编译,生成了我正在调用的可执行文件。

I am sure that there is a better way of doing it, but this is the best I've managed to do. 我相信有更好的方法可以做到,但这是我设法做到的最好。

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

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