2015-10-27 76 views
2

我有一個代碼在這裏,但按鈕點擊一些實例並不真正去最小化或恢復。我盡我所能,但沒有運氣給我。如何最小化和恢復大量實例的過程?

Private Declare Function ShowWindow Lib "user32.dll" (_ 
ByVal hWnd As IntPtr, _ 
ByVal nCmdShow As Show_Window _ 
) As Boolean 

Private Enum Show_Window As Integer 
    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 

Private Sub MinimizeAllToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles MinimizeAllToolStripMenuItem.Click 
    Dim p() As Process = Process.GetProcessesByName("GameServer") 
    Dim p1() As Process = Process.GetProcessesByName("AccountServer") 
    Dim p2() As Process = Process.GetProcessesByName("GateServer") 
    Dim p3() As Process = Process.GetProcessesByName("GroupServer") 
    Dim p4() As Process = Process.GetProcessesByName("TradeServer") 
    Dim p5() As Process = Process.GetProcessesByName("FilterServer") 
    If MinimizeAllToolStripMenuItem.Text = "Minimize All" Then 
     For Each pr As Process In p 
      ShowWindow(pr.MainWindowHandle, Show_Window.ForceMinimized) 
     Next pr 
     For Each pr As Process In p1 
      ShowWindow(pr.MainWindowHandle, Show_Window.ForceMinimized) 
     Next pr 
     For Each pr As Process In p2 
      ShowWindow(pr.MainWindowHandle, Show_Window.ForceMinimized) 
     Next pr 
     For Each pr As Process In p3 
      ShowWindow(pr.MainWindowHandle, Show_Window.ForceMinimized) 
     Next pr 
     For Each pr As Process In p4 
      ShowWindow(pr.MainWindowHandle, Show_Window.ForceMinimized) 
     Next pr 
     For Each pr As Process In p5 
      ShowWindow(pr.MainWindowHandle, Show_Window.ForceMinimized) 
     Next pr 
     MinimizeAllToolStripMenuItem.Text = "Restore All" 
    Else 
     For Each pr As Process In p 
      ShowWindow(pr.MainWindowHandle, Show_Window.Restore) 
     Next pr 
     For Each pr As Process In p1 
      ShowWindow(pr.MainWindowHandle, Show_Window.Restore) 
     Next pr 
     For Each pr As Process In p2 
      ShowWindow(pr.MainWindowHandle, Show_Window.Restore) 
     Next pr 
     For Each pr As Process In p3 
      ShowWindow(pr.MainWindowHandle, Show_Window.Restore) 
     Next pr 
     For Each pr As Process In p4 
      ShowWindow(pr.MainWindowHandle, Show_Window.Restore) 
     Next pr 
     For Each pr As Process In p5 
      ShowWindow(pr.MainWindowHandle, Show_Window.Restore) 
     Next pr 
     MinimizeAllToolStripMenuItem.Text = "Minimize All" 
    End If 
End Sub 

什麼可能是錯的?或者,也許有人可以分享你的代碼,如何最大限度地減少和恢復大量實例的過程。我希望有一個人可以幫助我。

+0

你可以包含什麼地方出了錯一些更多的細節?你有可能發佈自己編譯的工作代碼嗎?這將有助於我們診斷。 – kdbanman

+0

你好@kdbanman,這段代碼正在工作,它的編譯沒有錯誤。問題是,每當我按下按鈕,有時一些實例不會最小化,有時不會恢復。 – Foxseiz

回答

2

真的不知道是哪裏的問題可能是,但你可以減少代碼顯著使用這樣的事情:

Private Declare Function ShowWindow Lib "user32.dll" (
    ByVal hWnd As IntPtr, 
    ByVal nCmdShow As Show_Window 
    ) As Boolean 

Private Enum Show_Window As Integer 
    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 

Private Sub MinimizeAllToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles MinimizeAllToolStripMenuItem.Click 
    Dim processes() As String = {"GameServer", "AccountServer", "GateServer", "GroupServer", "TradeServer", "FilterServer"} 
    Dim cmd As Show_Window = If(MinimizeAllToolStripMenuItem.Text = "Minimize All", Show_Window.Minimize, Show_Window.Restore) 
    For Each processName As String In processes 
     For Each pr As Process In Process.GetProcessesByName(processName) 
      ShowWindow(pr.MainWindowHandle, cmd) 
     Next 
    Next 
    MinimizeAllToolStripMenuItem.Text = If(MinimizeAllToolStripMenuItem.Text = "Restore All", "Minimize All", "Restore All") 
End Sub 
+0

感謝您的優化,但我不太確定程序可以在哪裏。只是爲了一個想法,我使用你的代碼之前我用這個代碼以及那些服務器。 http://www.experts-exchange.com/questions/28736752/VB-net-Auto-Restart-Application.html – Foxseiz

相關問題