您可以查看這篇文章,瞭解如何隱藏外部應用程序窗口。 http://www.vbforums.com/showthread.php?669210-RESOLVED-Hiding-Window-of-external-process
在這個例子中,人們試圖以隱藏模式啓動calc.exe。
首先關閉所有刀片上的progect的beggining
Imports System.Runtime.InteropServices
然後,你必須枚舉標誌爲的ShowWindow(http://msdn.microsoft.com/en-us/library/windows/desktop/ms633548%28v=vs.85%29.aspx
Private Enum ShowWindowCommand As Integer
Hide = 0
Show = 5
Minimize = 6
Restore = 9
End Enum
設置指定窗口的顯示狀態
<DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _
Private Shared Function ShowWindow(ByVal hwnd As IntPtr, ByVal nCmdShow As ShowWindowCommand) As Boolean
End Function
確定指定的窗口句柄是否標識existi ng窗口
<DllImport("user32.dll", CharSet:=CharSet.Auto, SetLastError:=True)>
Private Shared Function IsWindow(ByVal hWnd As IntPtr) As Boolean
End Function
確定指定窗口是否已最小化。
Private Declare Auto Function IsIconic Lib "user32.dll" (ByVal hwnd As IntPtr) As Boolean
變量保存窗口句柄(你可以的choise自己的名字,但在其它代碼的一部分重命名)
Private calc_hWnd As IntPtr
啓動Windows計算器(你的情況saplogon.exe)最小化和隱藏,同時加載表格
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Dim test As New Process
Try
' file to launch
test.StartInfo.FileName = "calc.exe" ' note: full path not needed for windows calc.
' Note: This next line has no effect on WinXP "calc.exe" and some other apps like FireFox.
'test.StartInfo.WindowStyle = ProcessWindowStyle.Minimized
' start the app
test.Start()
' wait until app is in idle state
test.WaitForInputIdle(-1)
' get app main window handle
Dim tmp_hWnd As IntPtr = test.MainWindowHandle
' make sure handle is valid (non zero)
' try up to 10 times within one second
' do a re-try loop that runs for a second or two
For i As Integer = 1 To 10
tmp_hWnd = test.MainWindowHandle
If Not tmp_hWnd.Equals(IntPtr.Zero) Then Exit For ' got handle so exit loop
Threading.Thread.Sleep(100) ' wait 100ms
Next '- try again
If Not tmp_hWnd.Equals(IntPtr.Zero) Then
' use ShowWindow to change app window state (minimize and hide it).
ShowWindow(tmp_hWnd, ShowWindowCommand.Minimize)
ShowWindow(tmp_hWnd, ShowWindowCommand.Hide)
' save handle for later use.
calc_hWnd = tmp_hWnd
Else
' no window handle?
MessageBox.Show("Unable to get a window handle!")
End If
Catch ex As Exception
' error !
MessageBox.Show(ex.Message)
End Try
End Sub
上退出恢復/取消隱藏應用程序,如果發現運行。
Private Sub Form1_FormClosing(sender As Object, e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
' is our variable set to non-zero?
If Not calc_hWnd.Equals(IntPtr.Zero) Then
' is app window found?
If IsWindow(calc_hWnd) = True Then
' if app is minimized then restore it
If IsIconic(calc_hWnd) Then
ShowWindow(calc_hWnd, ShowWindowCommand.Restore)
End If
' make sure window is seen incase it was hidden.
ShowWindow(calc_hWnd, ShowWindowCommand.Show)
End If
End If
End Sub
但是你可以編寫另一個代碼並殺死saplogon.exe進程。
Private Sub Form1_FormClosing(sender As Object, e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
For Each p As Process In System.Diagnostics.Process.GetProcessesByName("saplogon.exe")
Try
p.Kill()
' possibly with a timeout
p.WaitForExit()
' process has already exited - might be able to let this one go
Catch ex As Exception
MessageBox.Show(ex.toString)
End Try
Next
End Sub
雖然此鏈接可能會回答問題,但最好在此處包含答案的重要部分並提供供參考的鏈接。如果鏈接頁面更改,則僅鏈接答案可能會失效。 – djv 2015-03-26 15:38:40
給我一些時間來更新答案 – 2015-03-26 15:41:07