Lm th? no ? v hi?u ha nt Close trn thanh tiu ? c?a m?t ?ng d?ng giao di?n i?u khi?n b?ng cch s? d?ng Visual Basic 2005 ho?c Visual Basic.NET

D?ch tiu ? D?ch tiu ?
ID c?a bi: 818361 - Xem s?n ph?m m bi ny p d?ng vo.
Bung t?t c? | Thu g?n t?t c?

? Trang ny

TM T?T

Bi t?ng b?c ny m t? lm th? no ? v hi?u ha cc ong nt trn thanh tiu ? c?a m?t ?ng d?ng giao di?n i?u khi?n. ? lm i?u ny, b?n ph?i khai bo ti li?u tham kh?o bn ngoi cc th? t?c l thnh vin c?a cc User32.dll t?p tin, c ?c m?t x? l? h? th?ng menu c?a ?ng d?ng giao di?n i?u khi?n, v sau xa cc ong m?c tr?nh n t? tr?nh n h? th?ng c?a ?ng d?ng giao di?n i?u khi?n.

Chu y Ton b? ngu?n m? c s?n trong "hon thnh m? listing (Module1.vb) "ph?n.

Yu c?u

Cc danh sch sau y v?ch ra ?c ? ngh? ph?n c?ng, ph?n m?m, m?ng c s? h? t?ng, v gi d?ch v? m b?n c?n:
  • Microsoft Windows 2000, Microsoft Windows XP ho?c Microsoft Windows Server 2003
  • Microsoft Visual Basic 2005 ho?c Microsoft Visual Basic .NET
i?u ny bi gi? ?nh r?ng b?n ? quen thu?c v?i cc ch? ? sau:
  • Visual Basic 2005 ho?c Visual Basic.NET
  • Vi?t unmanaged m? vo nm 2005 Visual Basic ho?c trong Visual C b?n.NET

Tuyn b? ti li?u tham kh?o bn ngoi cc th? t?c trong User32.dll

  1. Ch?y Microsoft Visual Studio 2005 ho?c Microsoft Visual Studio .NET.
  2. i?m ?n M?i trn cc Tptr?nh n, v sau nh?p vo D an.
  3. D?i Cc lo?i d? n, nh?n vo y ? ch?nVisual Basic d? n.

    Chu y Trong Visual Studio 2005, b?m vo ? ch?n Th? gic Basic Thay v? Visual Basic d? n.
  4. D?i Khun mu, nh?n vo y ? ch?nGiao di?n i?u khi?n ?ng d?ng.
  5. Trong cc Tn h?p vn b?n, lo?iMyConsoleApplication, sau b?mOk.

    Theo m?c ?nh, Module1.vb ?c t?o ra.
  6. N?u o?n m? sau s? khng ?c thm theo m?c ?nh khi b?n t?o cc d? n, thm m? sau vo Module1.vb tr?c khi cc M-un Module1 tuyn b?.
    Option Strict On
  7. Thm m? sau vo Module1.vb tr?c khi cc Ph? chnh tuyn b?.
    ' 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

C ?c m?t x? l? vo c?a s? ?ng d?ng giao di?n i?u khi?n

  1. N?i thm m? sau y ? Module1.vb sau khi cc End Sub tuyn b?.
    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. Thm m? sau y ? Module1.vb, sau khi cc Ph? chnh tuyn b?.
    ' Obtain a handle to the console application window by passing the title of your application.
    Dim hWnd As Integer = ObtainWindowHandle("<MyConsoleApplication>")
    Chu y Thay th? <myconsoleapplication></myconsoleapplication>v?i tn ?ng d?ng giao di?n i?u khi?n c?a b?n.
B?n c?ng c th? s? d?ng cc FindWindow ch?c nng ? c ?c m?t x? l? vo c?a s? ?ng d?ng giao di?n i?u khi?n. bit thm thng tin v FindWindow, gh thm Web site sau c?a Microsoft Developer Network (MSDN):
http://msdn2.Microsoft.com/en-US/Library/ms633499.aspx

C ?c m?t x? l? giao di?n i?u khi?n ?ng d?ng h? th?ng menu

N?i thm m? sau y ? Module1.vb tr?c khi cc End Sub tuyn b?.
' Obtain a handle to the console application system menu.
Dim hMenu As Integer = GetSystemMenu(hWnd, False)

Xa b? ng m?c tr?nh n t? tr?nh n ?ng d?ng giao di?n i?u khi?n h? th?ng

N?i thm m? sau y ? Module1.vb tr?c khi cc End Sub tuyn b?, v sau nh?p vo Lu t?t c? trn cc Tp tr?nh n.
' 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()

Danh sch m? hon ch?nh (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
Chu y Thay th? <myconsoleapplication></myconsoleapplication>v?i tn ?ng d?ng giao di?n i?u khi?n c?a b?n.

Xc minh r?ng n ho?t ?ng

  1. Trn cc Xy d?ng tr?nh n, nh?p vo Xy d?ng Gi?i php.
  2. Trn cc G? l?i tr?nh n, nh?p voB?t ?u ? ch?y cc ?ng d?ng.

    Bn i?u khi?n xu?t hi?n v?i vn b?n sau y:
    Press ENTER to quit
  3. H?y th? b?m cc ong nt.

    B?n khng th? b?m vo cc ong nt v? n v hi?u ho. Thot kh?i ?ng d?ng, nh?n phm ENTER trong cc giao di?n i?u khi?n.

    Chu y Cc ?ng d?ng c th? m?t m?t th?i gian ? c ?c m?t x? l? ? c?a n giao di?n i?u khi?n c?a s?. V? v?y, cc ?ng d?ng c?ng c th? m?t m?t th?i gian ? v hi?u ho cc ong nt.

G? r?i

  • N?u b?n tuyn b? cc GetWindowText phng php v khng xc ?nh m?t b danh, b?n c th? nh?n di?n 'EntryPointNotFoundException' unhandled ngo?i l?.

    Ch?nh s?a v?n ? ny, thay th?
    Private Declare Function GetWindowText Lib "user32" (ByVal hWnd As Integer, ByVal lpString As String, ByVal nMaxCount As Integer) As Integer
    v?i
    Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hWnd As Integer, ByVal lpString As String, ByVal nMaxCount As Integer) As Integer
  • N?u b?n ch?y ?ng d?ng giao di?n i?u khi?n c?a b?n t? cu l?nh d?u nh?c, cc ong nt v?n b? v hi?u ho ngay c? sau khi thot kh?i ?ng d?ng. V? v?y, Microsoft khuy?n co b?n nh?p p vo t?p tin th?c thi trong Visual Studio.NET ? ch?y ?ng d?ng c?a b?n.

THAM KH?O

? bi?t thm chi ti?t, h?y truy c?p trang web MSDN Web sau y:
Windows API v cc th vi?n lin k?t ?ng khc
http://msdn2.Microsoft.com/en-US/Library/aa141322 (office.10) .aspx
Tuyn b? Tuyn b?
http://msdn2.Microsoft.com/en-US/Library/4zey12w5 (vs.71) .aspx
Gi?i ph?u c?a m?t tuyn b? tuyn b?
http://msdn2.Microsoft.com/en-US/Library/aa165080 (office.10) .aspx
S? hi?u bi?t X? l?
http://msdn2.Microsoft.com/en-US/Library/aa141354 (office.10) .aspx

Thu?c tnh

ID c?a bi: 818361 - L?n xem xt sau cng: 28 Thang Tam 2011 - Xem xt l?i: 2.0
p d?ng
  • Microsoft Visual Basic .NET 2003 Standard Edition
  • Microsoft Visual Basic .NET 2002 Standard Edition
T? kha:
kbvs2005swept kbvs2005applies kbapi kbconsole kbprogramming kbhowtomaster kbmt KB818361 KbMtvi
My d?ch
QUAN TRONG: Bi vi?t ny ?c d?ch b?ng ph?n m?m d?ch my c?a Microsoft ch? khng ph?i do con ng?i d?ch. Microsoft cung c?p cc bi vi?t do con ng?i d?ch v c? cc bi vi?t do my d?ch ? b?n c th? truy c?p vo t?t c? cc bi vi?t trong C s? Ki?n th?c c?a chng ti b?ng ngn ng? c?a b?n. Tuy nhin, bi vi?t do my d?ch khng ph?i lc no c?ng hon h?o. Lo?i bi vi?t ny c th? ch?a cc sai st v? t? v?ng, c php ho?c ng? php, gi?ng nh m?t ng?i n?c ngoi c th? m?c sai st khi ni ngn ng? c?a b?n. Microsoft khng ch?u trch nhi?m v? b?t k? s? thi?u chnh xc, sai st ho?c thi?t h?i no do vi?c d?ch sai n?i dung ho?c do ho?t ?ng s? d?ng c?a khch hng gy ra. Microsoft c?ng th?ng xuyn c?p nh?t ph?n m?m d?ch my ny.
Nh?p chu?t vo y ? xem b?n ti?ng Anh c?a bi vi?t ny:818361

Cung cp Phan hi

 

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com