2009-12-17 4 views
1
mise au point

Dans VB.NET, vous pouvez définir le focus à une application externe à l'aideMise en application externe

AppActivate("Windows Name") 

ou

AppActivate(processID As Integer) 

Maintenant, cela fonctionne très bien si vous le faites par exemple:

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

Mais quand je le fais:

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 

alors il ne trouve pas la fenêtre, même si elle est ouverte et même si elle trouve la fenêtre en utilisant le titre de la fenêtre.

Mais j'en ai besoin par ID de processus.

Comment puis-je faire cela?

J'ai besoin d'elle pour mettre l'accent sur un programme d'installation tiers dans un projet d'installation Windows Installer.

+0

Pourquoi est-ce marqué "asp.net"? – AUSteve

+0

Veuillez ne jamais utiliser 'InStr'. 'theprocess.ProcessName.Contains (" DWG ")' est le "correct", .NET façon de faire les choses. – Ryan

Répondre

3

Je ne sais pas exactement pourquoi vous n'obtenez pas le bon résultat. Généralement, en mettant l'accent sur d'autres applications, je n'ai jamais eu trop de chance avec AppActivate (différents degrés de succès, au moins). Essayez ceci:

Ajouter cette classe au même module/objet/whereever votre code est:

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 

Ensuite, dans votre code, au lieu de AppActivate, procédez comme suit:

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

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

Essayez ces fonctions 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 

Utilisez Process.MainWindowHandle pour obtenir le handle. Cela fonctionne sur la plupart des applications, mais pas toutes.

Questions connexes