文章編號: 185730 - 上次校閱: 2005年5月3日 - 版次: 4.0

如何防止 32 位元 VB 應用程式的多個執行個體

系統提示本文適用於您使用的作業系統之外的作業系統。與您不相關的文章內容已停用。

在此頁中

全部展開 | 全部摺疊

結論

本文將告訴您,如何避免使用者已經有一個執行個體執行時,載入應用程式的第二個執行個體。它也會將焦點設成 Visual Basic 應用程式的第一個執行個體當您試圖啟動相同的應用程式的第二個執行個體。

其他相關資訊

雖說是逐步範例

  1. 建立新的 Visual Basic 專案。
  2. 進入 [專案屬性,並設定啟始物件 (或啟動表單中 VB4) 是 Sub Main。
  3. 將標準模組新增到 [專案]。
  4. 下列程式碼貼到模組中:
          Option Explicit
    
          Public Const GW_HWNDPREV = 3
    
          Declare Function OpenIcon Lib "user32" (ByVal hwnd As Long) As Long
          Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
            (ByVal lpClassName As String, ByVal lpWindowName As String) _
             As Long
          Declare Function GetWindow Lib "user32" _
            (ByVal hwnd As Long, ByVal wCmd As Long) As Long
          Declare Function SetForegroundWindow Lib "user32" _
            (ByVal hwnd As Long) As Long
    
          Private Sub Form_Load()
                If App.PrevInstance Then
                   ActivatePrevInstance
                End If
             End Sub
    
          Sub ActivatePrevInstance()
             Dim OldTitle As String
             Dim PrevHndl As Long
             Dim result As Long
    
             'Save the title of the application.
             OldTitle = App.Title
    
             'Rename the title of this application so FindWindow
             'will not find this application instance.
             App.Title = "unwanted instance"
    
             'Attempt to get window handle using VB4 class name.
             PrevHndl = FindWindow("ThunderRTMain", OldTitle)
    
             'Check for no success.
             If PrevHndl = 0 Then
                'Attempt to get window handle using VB5 class name.
                PrevHndl = FindWindow("ThunderRT5Main", OldTitle)
             End If
    
             'Check if found
             If PrevHndl = 0 Then
             'Attempt to get window handle using VB6 class name
             PrevHndl = FindWindow("ThunderRT6Main", OldTitle)
             End If
    
             'Check if found
             If PrevHndl = 0 Then
                'No previous instance found.
                Exit Sub
             End If
    
             'Get handle to previous window.
             PrevHndl = GetWindow(PrevHndl, GW_HWNDPREV)
    
             'Restore the program.
             result = OpenIcon(PrevHndl)
    
             'Activate the application.
             result = SetForegroundWindow(PrevHndl)
    
             'End the application.
             Unload Me
          End Sub
    
    						
  5. 專案編譯成 EXE。
  6. 結束 Visual Basic。
  7. 執行您所建立的可執行檔。
  8. 重複步驟 7。

    結果: 程式的第一個執行個體設定焦點,並關閉第二個執行個體。如果應用程式的第一個執行個體已最小化,它會自動恢復至正常的視窗。

?考

如更多有關防止 Visual Basic 4.0 16 位元應用程式的多個執行個體的資訊,按一下下面的文件編號,檢視 「 Microsoft 知識庫 」 中的發行項:
142937? (http://support.microsoft.com/kb/142937/ ) 如何防止 VB 應用程式的多個執行個體

這篇文章中的資訊適用於:
  • Microsoft Visual Basic 6.0 Learning Edition
  • Microsoft Visual Basic 6.0 Professional Edition
  • Microsoft Visual Basic Enterprise Edition for Windows 6.0
  • Microsoft Visual Basic Control Creation Edition
  • Microsoft Visual Basic 5.0 Learning Edition
  • Microsoft Visual Basic 5.0 Professional Edition
  • Microsoft Visual Basic 5.0 Enterprise Edition
  • Microsoft Visual Basic 4.0 Standard Edition
  • Microsoft Visual Basic 4.0 Professional Edition
  • Microsoft Visual Basic 4.0 32-Bit Enterprise Edition
關鍵字:?
kbmt kbhowto KB185730 KbMtzh
機器翻譯機器翻譯
重要:本文是以 Microsoft 機器翻譯軟體翻譯而成,而非使用人工翻譯而成。Microsoft 同時提供使用者人工翻譯及機器翻譯兩個版本的文章,讓使用者可以依其使用語言使用知識庫中的所有文章。但是,機器翻譯的文章可能不盡完美。這些文章中也可能出現拼字、語意或文法上的錯誤,就像外國人在使用本國語言時可能發生的錯誤。Microsoft 不為內容的翻譯錯誤或客戶對該內容的使用所產生的任何錯誤或損害負責。Microsoft也同時將不斷地就機器翻譯軟體進行更新。
按一下這裡查看此文章的英文版本:185730? (http://support.microsoft.com/kb/185730/en-us/ )
Microsoft及(或)其供應商不就任何在本伺服器上發表的文字資料及其相關圖表資訊的恰當性作任何承諾。所有文字資料及其相關圖表均以「現狀」供應,不負任何擔保責任。Microsoft及(或)其供應商謹此聲明,不負任何對與此資訊有關之擔保責任,包括關於適售性、適用於某一特定用途、權利或不侵權的明示或默示擔保責任。Microsoft及(或)其供應商無論如何不對因或與使用本伺服器上資訊或與資訊的實行有關而引起的契約、過失或其他侵權行為之訴訟中的特別的、間接的、衍生性的損害或任何因使用而喪失所導致的之損害、資料或利潤負任何責任。