如何使用 Visual Basic 2005 或 Visual Basic.NET 停用的主控台應用程式標題列上的 [關閉] 按鈕

文章編號: 818361 - 檢視此文章適用的產品。
全部展開 | 全部摺疊

在此頁中

結論

本文將逐步告訴您,如何停用的主控台應用程式標題列上的 [關閉] 按鈕。若要執行此動作必須宣告成員的 User32.dll 檔案的外部程序的參考、 取得主控台] 應用程式的 [系統] 功能表的控制代碼,然後再刪除 [主控台應用程式的 [系統] 功能表中的 [關閉 功能表項目。

附註整個原始程式碼隨即出現在 「 列出 (Module1.vb) 的完整程式碼 」 區段。

需求

下列清單列出建議的硬體、 軟體、 網路基礎結構及您需要的服務套件:
  • Microsoft Windows 2000]、 [Microsoft Windows XP] 或 [Microsoft Windows Server 2003
  • Microsoft Visual Basic 2005 或 Microsoft Visual 基本.NET
本文假設您已熟悉下列主題:
  • Visual Basic 2005 或 Visual Basic.NET
  • 書寫 Unmanaged 程式碼,在 Visual 基本 2005年或 Visual Basic.NET 中

宣告位於 User32.dll 的外部程序的參考

  1. 執行 Microsoft Visual Studio 2005 或 Microsoft Visual Studio.NET。
  2. 指向 [新增]檔案] 功能表,然後按一下 [專案]。
  3. 在 [專案類型,按一下以選取 [Visual Basic 專案

    附註在 Visual 的 Studio 2005 中按一下以選取 [Visual Basic 代替 Visual Basic 專案]。
  4. 在 [範本,按一下以選取 [主控台應用程式
  5. 在 [名稱] 文字方塊中輸入 MyConsoleApplication,],然後再按一下 [確定]]。

    根據預設值,Module1.vb,即建立。
  6. 如果當在您建立專案時預設不加入下列程式碼,Module1.vb 來加入下列程式碼之前將 模組 Module1 陳述式。
    Option Strict On
  7. 將下列的程式碼加入 Module1.vb Sub Main 陳述式之前
    ' Declaring references to external procedures that are in user32.dll.
    Private Declare Function DeleteMenu Lib "user32" (ByVal hMenu As Integer, _
       ByVal uPosition As Integer, ByVal uFlags As Integer) As Boolean
    Private Declare Function GetForegroundWindow Lib "user32" () As Integer
    Private Declare Function GetSystemMenu Lib "user32" (ByVal hWnd As Integer, _
       ByVal bRevert As Boolean) As Integer
    Private Declare Function GetWindow Lib "user32" (ByVal hWnd As Integer, _
       ByVal uCmd As Integer) As Integer
    Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" _
       (ByVal hWnd As Integer, ByVal lpString As String, ByVal nMaxCount As Integer) As Integer

取得主控台應用程式視窗的控制代碼

  1. End Sub 陳述式之後,將下列程式碼附加至 Module1.vb
    Private Function ObtainWindowHandle(ByVal lpstrCaption As String) As Integer
    
       ' To store the handle to a window.
       Dim hWnd As Integer
       ' Maximum number of characters in the GetWindowText method.
       Dim nMaxCount As Integer
       ' Actual number of characters copied in the GetWindowText method.
       Dim nCopiedLength As Integer
       ' To store the text of the title bar of the window.
       Dim lpString As String
    
       nMaxCount = 255
       ' Obtain a handle to the first window.
       hWnd = GetForegroundWindow
    
       ' Loop through the various windows until you encounter the console application window, _
       ' or there are no more windows.
       While hWnd <> 0
    
          ' Fill lpString with spaces.
          lpString = Space(nMaxCount)
          ' Get the text of the title bar of the window in lpString.
          nCopiedLength = GetWindowText(hWnd, lpString, nMaxCount)
    
          ' Verify that lpString is neither empty, nor NULL.
          If Len(Trim(lpString)) <> 0 And Asc(Trim(lpString)) <> 0 Then
             ' Verify that the title of the retrieved window is the same as the title of the console application window.
             If CType(InStr(Left(lpString, nCopiedLength), lpstrCaption), Boolean) Then
                ' Return hWnd to the Main method.
                Return hWnd
             End If
          End If
    
          ' Get the next window.
          hWnd = GetWindow(hWnd, 2)
    
       End While
    
       ' If no corresponding windows are found, return 0.
       Return 0
    
    End Function
  2. 將下列的程式碼加入 Module1.vb,Sub Main 陳述式之後
    ' Obtain a handle to the console application window by passing the title of your application.
    Dim hWnd As Integer = ObtainWindowHandle("<MyConsoleApplication>")
    注意 取代 <MyConsoleApplication> 主控台應用程式的名稱。
您也可以使用 FindWindow 函式以取得主控台應用程式視窗的控制代碼。 如需有關 FindWindow 的詳細資訊,請造訪下列 Microsoft 開發 o 人 h 員 ? 工 u 具 ? 網路 (MSDN) 網站]:
http://msdn2.microsoft.com/en-us/library/ms633499.aspx

取得主控台應用程式系統功能表的控制代碼

End Sub 陳述式之前,將下列程式碼附加至 Module1.vb
' Obtain a handle to the console application system menu.
Dim hMenu As Integer = GetSystemMenu(hWnd, False)

從主控台應用程式系統功能表刪除關閉的功能表項目

新增到 Module1.vb 的下列程式碼之前,End Sub 陳述式,且然後按一下 [檔案] 功能表上的 [全部儲存]
' Delete the Close menu item from the console application system menu.
' This will automatically disable the Close button on the console application title bar.
' 6 indicates the position of the Close menu item.
' 1024 indicates that the second parameter is a positional indicator.
DeleteMenu(hMenu, 6, 1024)
System.Console.WriteLine("Press ENTER to quit")
' Wait for the user to press the ENTER key.
System.Console.ReadLine()

完整的程式碼清單 (Module1.vb)

Option Strict On

Module Module1
   ' Declaring references to external procedures that are in user32.dll.
   Private Declare Function DeleteMenu Lib "user32" (ByVal hMenu As Integer, _
      ByVal uPosition As Integer, ByVal uFlags As Integer) As Boolean
   Private Declare Function GetForegroundWindow Lib "user32" () As Integer
   Private Declare Function GetSystemMenu Lib "user32" (ByVal hWnd As Integer, _
      ByVal bRevert As Boolean) As Integer
   Private Declare Function GetWindow Lib "user32" (ByVal hWnd As Integer, _
      ByVal uCmd As Integer) As Integer
   Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" _
      (ByVal hWnd As Integer, ByVal lpString As String, ByVal nMaxCount As Integer) As Integer

   Sub Main()
      ' Obtain a handle to the console application window by passing the title of your application.
      ' Replace <MyConsoleApplication> with the name of your Console Application.
      Dim hWnd As Integer = ObtainWindowHandle("<MyConsoleApplication>")
      ' Obtain a handle to the console application system menu.
      Dim hMenu As Integer = GetSystemMenu(hWnd, False)
      ' Delete the Close menu item from the console application system menu.
      ' This will automatically disable the Close button on the console application title bar.
      ' 6 indicates the position of the Close menu item.
      ' 1024 indicates that the second parameter is a positional indicator.
      DeleteMenu(hMenu, 6, 1024)
      System.Console.WriteLine("Press ENTER to quit")
      ' Wait for the user to press the ENTER key.
      System.Console.ReadLine()
   End Sub

   Private Function ObtainWindowHandle(ByVal lpstrCaption As String) As Integer

      ' To store the handle to a window.
      Dim hWnd As Integer
      ' Maximum number of characters in the GetWindowText method.
      Dim nMaxCount As Integer
      ' Actual number of characters copied in the GetWindowText method.
      Dim nCopiedLength As Integer
      ' To store the text of the title bar of the window.
      Dim lpString As String

      nMaxCount = 255
      ' Obtain a handle to the first window.
      hWnd = GetForegroundWindow

      ' Loop through the various windows until you encounter the console application window, _
      ' or there are no more windows.
      While hWnd <> 0

         ' Fill lpString with spaces.
         lpString = Space(nMaxCount)
         ' Get the text of the window title bar in lpString.
         nCopiedLength = GetWindowText(hWnd, lpString, nMaxCount)

         ' Verify that lpString is neither empty, nor NULL.
         If Len(Trim(lpString)) <> 0 And Asc(Trim(lpString)) <> 0 Then
            ' Verify that the title of the retrieved window is the same as the title of the console application window.
            If CType(InStr(Left(lpString, nCopiedLength), lpstrCaption), Boolean) Then
               ' Return hWnd to the Main method.
               Return hWnd
            End If
         End If

         ' Get the next window.
         hWnd = GetWindow(hWnd, 2)

      End While

      ' If no corresponding windows are found, return 0.
      Return 0

   End Function

End Module
附註取代 <MyConsoleApplication> 主控台應用程式的名稱。

請確認它可以運作

  1. 在 [建置] 功能表上按一下 建置方案
  2. 在 [偵錯] 功能表上按一下 [開始] 執行應用程式]。

    主控台會以下列文字顯示:
    Press ENTER to quit
  3. 請試著按一下 [關閉] 按鈕。

    您不能按一下 [關閉] 按鈕,因為它已停用。若要結束應用程式,請在主控台中按下 ENTER 鍵。

    附註應用程式可能需要一些時間來取得其主控台視窗的控制代碼。因此,應用程式可能也需要一些時間來停用 [關閉] 按鈕。

疑難排解

  • 如果您宣告 GetWindowText 方法,且並未指定別名,可能會收到 'EntryPointNotFoundException' 無法處理的例外狀況。

    若要解決這個問題,取代
    Private Declare Function GetWindowText Lib "user32" (ByVal hWnd As Integer, ByVal lpString As String, ByVal nMaxCount As Integer) As Integer
    Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hWnd As Integer, ByVal lpString As String, ByVal nMaxCount As Integer) As Integer
  • 如果您在從命令提示字元執行主控台應用程式將 關閉 按鈕將一直維持無效的狀態即使在應用程式結束之後。 因此,Microsoft 建議您按兩下可執行檔執行應用程式的 Visual Studio.NET 中。

?考

如需詳細資訊請造訪下列 MSDN 網站:
Windows API 和其他動態連結程式庫
http://msdn2.microsoft.com/en-us/library/aa141322(office.10).aspx
宣告陳述式
http://msdn2.microsoft.com/en-us/library/4zey12w5(vs.71).aspx
Declare 陳述式的分解模型
http://msdn2.microsoft.com/en-us/library/aa165080(office.10).aspx
瞭解控點
http://msdn2.microsoft.com/en-us/library/aa141354(office.10).aspx

屬性

文章編號: 818361 - 上次校閱: 2007年5月12日 - 版次: 3.2
這篇文章中的資訊適用於:
  • Microsoft Visual Basic 2005 Express Edition
  • Microsoft Visual Basic .NET 2003 Standard Edition
  • Microsoft Visual Basic .NET 2002 Standard Edition
關鍵字:?
kbmt kbvs2005swept kbvs2005applies kbapi kbconsole kbprogramming kbhowtomaster KB818361 KbMtzh
機器翻譯
重要:本文是以 Microsoft 機器翻譯軟體翻譯而成,而非使用人工翻譯而成。Microsoft 同時提供使用者人工翻譯及機器翻譯兩個版本的文章,讓使用者可以依其使用語言使用知識庫中的所有文章。但是,機器翻譯的文章可能不盡完美。這些文章中也可能出現拼字、語意或文法上的錯誤,就像外國人在使用本國語言時可能發生的錯誤。Microsoft 不為內容的翻譯錯誤或客戶對該內容的使用所產生的任何錯誤或損害負責。Microsoft也同時將不斷地就機器翻譯軟體進行更新。
按一下這裡查看此文章的英文版本:818361
Microsoft及(或)其供應商不就任何在本伺服器上發表的文字資料及其相關圖表資訊的恰當性作任何承諾。所有文字資料及其相關圖表均以「現狀」供應,不負任何擔保責任。Microsoft及(或)其供應商謹此聲明,不負任何對與此資訊有關之擔保責任,包括關於適售性、適用於某一特定用途、權利或不侵權的明示或默示擔保責任。Microsoft及(或)其供應商無論如何不對因或與使用本伺服器上資訊或與資訊的實行有關而引起的契約、過失或其他侵權行為之訴訟中的特別的、間接的、衍生性的損害或任何因使用而喪失所導致的之損害、資料或利潤負任何責任。

提供意見