2012-04-03 135 views
1

我正在尋找一種方法從進程ID獲取窗口標題。從pid獲取窗口標題

我想構建函數獲取特定窗口的pid並返回其窗口標題。

我嘗試使用AutoIt,但沒有奏效。

任何想法?

+0

我tryied它和它沒有工作 – MoShe 2012-04-03 06:54:22

回答

6

這應該是很簡單的:

Process.GetProcessById(processId).MainWindowTitle; 

,如果你喜歡它作爲一個功能,你要求:

public string GetWindowTitle(int processId){ 
    return Process.GetProcessById(processId).MainWindowTitle; 
} 
+3

只是爲了,這需要使用'從System.Diagnostics'不必看它節省未來的Google。 – rojo 2013-04-21 02:45:05

3

中的AutoIt這已經做過很多次之前,與選項返回屬於該進程的所有窗口,而不僅僅是主窗口。

This post提供了標準的解決方案。如果腐爛蔓延:

;0 will return 1 base array; leaving it 1 will return the first visible window it finds 
Func _WinGetByPID($iPID, $nArray = 1) 
    If IsString($iPID) Then $iPID = ProcessExists($iPID) 
    Local $aWList = WinList(), $sHold 

    For $iCC = 1 To $aWList[0][0] 
     If WinGetProcess($aWList[$iCC][1]) = $iPID And _ 
      BitAND(WinGetState($aWList[$iCC][1]), 2) = 0 Then 
      If $nArray Then Return $aWList[$iCC][0] 
      $sHold &= $aWList[$iCC][0] & Chr(1) 
     EndIf 
    Next 

    If $sHold Then Return StringSplit(StringTrimRight($sHold, 1), Chr(1)) 
    Return SetError(1, 0, 0) 
EndFunc 
+0

這顯然不是C#,因爲提問者已經提出要求。 – 2014-05-23 06:03:21

+3

@OndrejSotolar,它被標記爲AutoIt,OP甚至沒有提到C#的問題,所以你完全基於標籤的順序。無論如何,有人在Google上搜索「自動從pid窗口獲取窗口」最終會出現在這裏,那麼問題是什麼? – Matt 2014-05-23 09:36:52

+1

正是我所做的,當我搜索「自動處理窗口」的前幾個結果之一時, – bigp 2014-08-26 12:32:22