2012-04-03 84 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