Lm th? no ? k khai Windows b?ng cch s? d?ng WIN32 API

D?ch tiu ? D?ch tiu ?
ID c?a bi: 183009 - 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

B?n c th? danh sch Windows, bao g?m c? tr? em Windows, b?ng cch s? d?ng GetWindow API. Tuy nhin, m?t ?ng d?ng cho cc cu?c g?i GetWindow ? th?c hi?n i?u ny cng vi?c r?i ro ang ?c nh b?t trong m?t v?ng l?p v h?n ho?c tham kh?o m?t x? l? m?t c?a s? ? ?c tiu hu?. B?ng cch s? d?ng EnumWindows cho qu?c gia Windows v EnumChildWindows cho tr? em Windows ho?c EnumThreadWindows cho t?t c? cc con Windows k?t h?p v?i m?t ch? ? l m?t phng php a thch v ?c th? hi?n trong bi vi?t ny.

THNG TIN THM

EnumWindows li?t k t?t c? cc c?p cao nh?t Windows, nhng cha ?a tr? Windows. Cc Ch?c nng EnumChildWindows khng k khai qu?c gia windows thu?c s? h?u c?a cc c?a s? c? th? c?ng nh n hi?n k khai b?t k? c?a s? thu?c s? h?u khc. Cc Ch?c nng EnumThreadWindows li?t k t?t c? c?a s? ?a tr? khng lin k?t v?i m?t ch? ?. M?u ny s? d?ng c? ba vo danh sch cc l?p v tiu ? c?a Windows n t?m th?y. Lu ? r?ng trong khi t?t c? c?a s? c m?t l?p h?c, khng ph?i t?t c? c m?t tiu ?.

V d?-by-step

  1. B?t ?u m?t d? n m?i trong Visual Basic. Form1 ?c t?o ra theo m?c ?nh.
  2. Thm hai CommandButtons v m?t h?p.
  3. Copy o?n m? sau vo m?u c?a module:
          Option Explicit
    
          Private Sub Command1_Click()
             Dim lRet As Long, lParam As Long
             Dim lhWnd As Long
    
             lhWnd = Me.hwnd  ' Find the Form's Child Windows
             ' Comment the line above and uncomment the line below to
             ' enumerate Windows for the DeskTop rather than for the Form
             'lhWnd = GetDesktopWindow()  ' Find the Desktop's Child Windows
             ' enumerate the list
             lRet = EnumChildWindows(lhWnd, AddressOf EnumChildProc, lParam)
          End Sub
    
          Private Sub Command2_Click()
             Dim lRet As Long
             Dim lParam As Long
    
             'enumerate the list
             lRet = EnumWindows(AddressOf EnumWinProc, lParam)
             ' How many Windows did we find?
             Debug.Print TopCount; " Total Top level Windows"
             Debug.Print ChildCount; " Total Child Windows"
             Debug.Print ThreadCount; " Total Thread Windows"
             Debug.Print "For a grand total of "; TopCount + _
             ChildCount + ThreadCount; " Windows!"
          End Sub
    					
  4. Thm m?t m-un v?i o?n m? sau:
          Option Explicit
    
          Type RECT
             Left As Long
             Top As Long
             Right As Long
             Bottom As Long
          End Type
    
          Declare Function GetWindowRect Lib "user32" (ByVal hwnd As Long, _
             lpRect As RECT) As Long
    
          Declare Function MoveWindow Lib "user32" (ByVal hwnd As Long, _
             ByVal x As Long, ByVal y As Long, ByVal nWidth As Long, _
             ByVal nHeight As Long, ByVal bRepaint As Long) As Long
    
          Declare Function GetDesktopWindow Lib "user32" () As Long
    
          Declare Function EnumWindows Lib "user32" _
             (ByVal lpEnumFunc As Long, ByVal lParam As Long) As Long
    
          Declare Function EnumChildWindows Lib "user32" (ByVal hWndParent _
             As Long, ByVal lpEnumFunc As Long, ByVal lParam As Long) As Long
    
          Declare Function EnumThreadWindows Lib "user32" (ByVal dwThreadId _
             As Long, ByVal lpfn As Long, ByVal lParam As Long) As Long
    
          Declare Function GetWindowThreadProcessId Lib "user32" _
             (ByVal hwnd As Long, lpdwProcessId As Long) As Long
    
          Declare Function GetClassName Lib "user32" Alias "GetClassNameA" _
             (ByVal hwnd As Long, ByVal lpClassName As String, _
             ByVal nMaxCount As Long) As Long
    
          Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" _
             (ByVal hwnd As Long, ByVal lpString As String, _
             ByVal cch As Long) As Long
    
          Public TopCount As Integer     ' Number of Top level Windows
          Public ChildCount As Integer   ' Number of Child Windows
          Public ThreadCount As Integer  ' Number of Thread Windows
    
          Function EnumWinProc(ByVal lhWnd As Long, ByVal lParam As Long) _
             As Long
             Dim RetVal As Long, ProcessID As Long, ThreadID As Long
             Dim WinClassBuf As String * 255, WinTitleBuf As String * 255
             Dim WinClass As String, WinTitle As String
    
             RetVal = GetClassName(lhWnd, WinClassBuf, 255)
             WinClass = StripNulls(WinClassBuf)  ' remove extra Nulls & spaces
             RetVal = GetWindowText(lhWnd, WinTitleBuf, 255)
             WinTitle = StripNulls(WinTitleBuf)
             TopCount = TopCount + 1
             ' see the Windows Class and Title for each top level Window
             Debug.Print "Top level Class = "; WinClass; ", Title = "; WinTitle
             ' Usually either enumerate Child or Thread Windows, not both.
             ' In this example, EnumThreadWindows may produce a very long list!
             RetVal = EnumChildWindows(lhWnd, AddressOf EnumChildProc, lParam)
             ThreadID = GetWindowThreadProcessId(lhWnd, ProcessID)
             RetVal = EnumThreadWindows(ThreadID, AddressOf EnumThreadProc, _
             lParam)
             EnumWinProc = True
          End Function
    
          Function EnumChildProc(ByVal lhWnd As Long, ByVal lParam As Long) _
             As Long
             Dim RetVal As Long
             Dim WinClassBuf As String * 255, WinTitleBuf As String * 255
             Dim WinClass As String, WinTitle As String
             Dim WinRect As RECT
             Dim WinWidth As Long, WinHeight As Long
    
             RetVal = GetClassName(lhWnd, WinClassBuf, 255)
             WinClass = StripNulls(WinClassBuf)  ' remove extra Nulls & spaces
             RetVal = GetWindowText(lhWnd, WinTitleBuf, 255)
             WinTitle = StripNulls(WinTitleBuf)
             ChildCount = ChildCount + 1
             ' see the Windows Class and Title for each Child Window enumerated
             Debug.Print "   Child Class = "; WinClass; ", Title = "; WinTitle
             ' You can find any type of Window by searching for its WinClass
             If WinClass = "ThunderTextBox" Then    ' TextBox Window
                RetVal = GetWindowRect(lhWnd, WinRect)  ' get current size
                WinWidth = WinRect.Right - WinRect.Left ' keep current width
                WinHeight = (WinRect.Bottom - WinRect.Top) * 2 ' double height
                RetVal = MoveWindow(lhWnd, 0, 0, WinWidth, WinHeight, True)
                EnumChildProc = False
             Else
                EnumChildProc = True
             End If
          End Function
    
          Function EnumThreadProc(ByVal lhWnd As Long, ByVal lParam As Long) _
             As Long
             Dim RetVal As Long
             Dim WinClassBuf As String * 255, WinTitleBuf As String * 255
             Dim WinClass As String, WinTitle As String
    
             RetVal = GetClassName(lhWnd, WinClassBuf, 255)
             WinClass = StripNulls(WinClassBuf)  ' remove extra Nulls & spaces
             RetVal = GetWindowText(lhWnd, WinTitleBuf, 255)
             WinTitle = StripNulls(WinTitleBuf)
             ThreadCount = ThreadCount + 1
             ' see the Windows Class and Title for top level Window
             Debug.Print "Thread Window Class = "; WinClass; ", Title = "; _
             WinTitle
             EnumThreadProc = True
          End Function
    
          Public Function StripNulls(OriginalStr As String) As String
             ' This removes the extra Nulls so String comparisons will work
             If (InStr(OriginalStr, Chr(0)) > 0) Then
                OriginalStr = Left(OriginalStr, InStr(OriginalStr, Chr(0)) - 1)
             End If
             StripNulls = OriginalStr
          End Function
    
    					
  5. Ch?y cc d? n v b?m vo Command1. i?u ny li?t k cc h?nh th?c tr? em Windows cho ?n khi n t?m th?y cc h?p, sau tng g?p i chi?u cao c?a cc TextBox v di chuy?n n ?n v? tr 0, 0 (bn tri c?a cc h?nh th?c.)
  6. Nh?p vo Command2. N by gi? danh sch cc l?p v tiu ? (n?u c m?t) cho t?t c? cc c?p cao nh?t Windows cho c?a s? con c?a h?, v b?t c? ?a tr? khng Windows g?n li?n v?i ch? ? c?a h?, ? c?a s? ngay l?p t?c. Chu y i?u ny c th? l m?t danh sch r?t di v c th? m?t vi pht ? hon thnh.
  7. Ch?y tr?nh n, ch?n k?t thc, ho?c nh?p vo k?t thc ? ngn ch?n cc chng tr?nh. Thay ?i m? trong Sub Command1_Click theo cc ? ki?n ? v?t qua x? l? t? GetDesktopWindow. Ch?y cc d? n v b?m vo Command1 li?t k t?t c? cc tr? em Windows c?a my tnh ? bn. Lu ? r?ng th? t?c ny c th? m?t m?t th?i gian ? hon thnh.

THAM KH?O

? bi?t thm chi ti?t, xin vui l?ng t?m ki?m trn cc ch? ? sau trong ho?c cc Win32 l?p tr?nh c?a ti li?u tham kh?o ho?c Microsoft Developer Network (MSDN) Th vi?n ?a CD-ROM:

  • EnumWindows
  • EnumChildWindows
  • EnumThreadWindows
  • GetDesktopWindow
  • GetClassName
  • GetWindowRect
  • GetWindowText
  • GetWindowThreadProcessId
  • MoveWindow

Thu?c tnh

ID c?a bi: 183009 - L?n xem xt sau cng: 20 Thang Tam 2011 - Xem xt l?i: 2.0
p d?ng
  • Microsoft Visual Basic 6.0 Professional Edition
  • Microsoft Visual Basic 5.0 Control Creation Edition
  • Microsoft Visual Basic 5.0 Learning Edition
  • Microsoft Visual Basic 5.0 Professional Edition
  • Microsoft Visual Basic 5.0 Enterprise Edition
T? kha:
kbapi kbhowto kbmt KB183009 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:183009

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