2009-12-17 134 views
1

在VB.NET中,您可以设置对焦点采用设置外部应用程序集中

AppActivate("Windows Name") 

AppActivate(processID As Integer) 

现在,如果你比如做这个工作正常,外部应用程序:

Dim intNotePad As Integer = Shell("C:\WINNT\Notepad.exe", 
AppWinStyle.MinimizedNoFocus) 
AppActivate(intNotePad) 

但是当我这样做:

For Each theprocess As Process In processlist 
    If InStr(theprocess.ProcessName, "DWG") Then 
     strProcessList += String.Format("Process: {0} ID: {1}", theprocess.ProcessName, theprocess.Id) + vbCrLf 
     AppActivate(theprocess.ID) 
    End If 
Next 

那么即使它是开放的,即使它使用窗口标题找到窗口,它也不会找到该窗口。

但我需要它的进程ID。

我该怎么做?

我需要它在Windows安装程序安装项目中的第三方安装程序的重点。

+0

为什么这个标签为“asp.net”? – AUSteve 2010-01-08 00:10:23

+0

请不要使用'InStr'。 'process.ProcessName.Contains(“DWG”)'是“正确的”.NET方法。 – Ryan 2012-06-06 03:23:28

回答

3

我不知道你为什么没有达到正确的结果。一般来说,在将焦点设置到其他应用程序时,我从来没有太多运气与AppActivate(不同程度的成功,至少)。试试这个:

这个类添加到同一个模块/对象/徘徊无论你的代码是:

Public NotInheritable Class Win32Helper 
    <System.Runtime.InteropServices.DllImport("user32.dll", _ 
    EntryPoint:="SetForegroundWindow", _ 
    CallingConvention:=Runtime.InteropServices.CallingConvention.StdCall, _ 
    CharSet:=Runtime.InteropServices.CharSet.Unicode, SetLastError:=True)> _ 
    Public Shared Function _ 
    SetForegroundWindow(ByVal handle As IntPtr) As Boolean 
     ' Leave function empty 
    End Function 

    <System.Runtime.InteropServices.DllImport("user32.dll", _ 
    EntryPoint:="ShowWindow", _ 
    CallingConvention:=Runtime.InteropServices.CallingConvention.StdCall, _ 
    CharSet:=Runtime.InteropServices.CharSet.Unicode, SetLastError:=True)> _ 
    Public Shared Function ShowWindow(ByVal handle As IntPtr, _ 
    ByVal nCmd As Int32) As Boolean 
     ' Leave function empty 
    End Function 
End Class 

然后在你的代码,而不是AppActivate,做到以下几点:

Dim appHandle As intPtr 
appHandle = theprocess.MainWindowHandle 'theprocess is naturally your process object 

Dim Win32Help As New Win32Helper 
Win32Helper.SetForegroundWindow(appHandle) 
2

试试这些Win32函数:

Declare Sub SwitchToThisWindow Lib "user32.dll" (ByVal hWnd As IntPtr, ByVal fAltTab As Boolean) 
Declare Function SetActiveWindow Lib "user32.dll" (ByVal hwnd As IntPtr) As IntPtr 
Private Enum ShowWindowEnum 
    Hide = 0 
    ShowNormal = 1 
    ShowMinimized = 2 
    ShowMaximized = 3 
    Maximize = 3 
    ShowNormalNoActivate = 4 
    Show = 5 
    Minimize = 6 
    ShowMinNoActivate = 7 
    ShowNoActivate = 8 
    Restore = 9 
    ShowDefault = 10 
    ForceMinimized = 11 
End Enum 

使用Process.MainWindowHandle得到处理。这适用于大多数应用程序,但不是全部。

相关问题