2016-08-17 65 views
0

我正試圖恢復(甚至最大化)另一個應用程序,並從我的項目向它發送輸入。我可以爲NotepadSkype等某些應用程序執行此操作,但不適用於其他應用程序,如TeamSpeak
任何想法爲什麼以及如何解決問題?將另一個應用程序放入前臺並向其發送輸入

這裏是我的代碼:

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

我用InputSimulator發送輸入。

+0

http://stackoverflow.com/questions/257587/bring-a-window-to-the-front-in-wpf MyWindow.TopMost = true –

+0

我想讓另一個進程成爲最頂級的進程,總是通過'winact()'設置,問題出在輸入 –

+0

你試過'hWnd.Activate();'和'hWnd.TopMost = true;'?我認爲這應該足以獲得關鍵投入。 – ViVi

回答

0

顯然問題是我的應用程序沒有以管理員權限運行。爲了能夠有效地將輸入發送到其他應用程序,需要管理員的角色。

爲此,我已將app.manifest添加到應用程序,並將requestedExecutionLevel levelasInvoker更改爲requireAdministrator
使用InputSimulator可用於其他應用程序,但某些應用程序(如遊戲)認爲輸入是簡單消息,不會將按鍵操作處理爲操作。然而他們被傳遞給聊天。 (這是我注意到的)

爲了解決這個問題,我已經在autoIT中編寫了一個腳本,它只是簡單地將參數傳遞給它(只有一個),並將參數作爲擊鍵發送給窗口,目前在前臺。編譯腳本導致我正在調用一個可執行文件。

我相信有更好的方式來做到這一點,但這是我設法做到的最好的方式。

0

你能試試嗎?我認爲這應該適合你。

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; 
     } 
    } 
} 
+0

使用這個,我在第二行得到一個'NullReferenceException'。 'hwndSource'爲null –

+0

您是否檢查了我的編輯@BogdanMolinger – ViVi

+0

仍然出現錯誤。 –

相關問題