首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > VB >

关于EnumWindows的有关问题

2012-08-02 
关于EnumWindows的问题?EnumWindows如果没有找到要找的窗口,就让它执行一段代码,该怎么做?[解决办法]VB co

关于EnumWindows的问题?
EnumWindows如果没有找到要找的窗口,就让它执行一段代码,该怎么做?

[解决办法]

VB code
'Example Name:EnumWindows and EnumChildWindows Callbacks  '------------------------------------------'' BAS Moduel Code''------------------------------------------Option Explicit Private Const LVIF_INDENT As Long = &H10Private Const LVIF_TEXT As Long = &H1Private Const LVM_FIRST As Long = &H1000Private Const LVM_SETITEM As Long = (LVM_FIRST + 6)Private Type LVITEM   mask As Long   iItem As Long   iSubItem As Long   state As Long   stateMask As Long   pszText As String   cchTextMax As Long   iImage As Long   lParam As Long   iIndent As LongEnd TypePublic Declare Function EnumWindows Lib "user32" _  (ByVal lpEnumFunc As Long, _   ByVal lParam As Long) As Long   Public Declare Function EnumChildWindows Lib "user32" _  (ByVal hWndParent As Long, _   ByVal lpEnumFunc As Long, _   ByVal lParam As Long) As LongPrivate Declare Function GetWindowTextLength Lib "user32" _    Alias "GetWindowTextLengthA" _   (ByVal hwnd As Long) As Long    Private Declare Function GetWindowText Lib "user32" _    Alias "GetWindowTextA" _   (ByVal hwnd As Long, _    ByVal lpString As String, _    ByVal cch As Long) As Long    Private Declare Function GetClassName Lib "user32" _    Alias "GetClassNameA" _   (ByVal hwnd As Long, _    ByVal lpClassName As String, _    ByVal nMaxCount As Long) As LongPrivate Declare Function IsWindowVisible Lib "user32" _   (ByVal hwnd As Long) As Long   Private Declare Function GetParent Lib "user32" _   (ByVal hwnd As Long) As LongPrivate Declare Function SendMessage Lib "user32" _   Alias "SendMessageA" _  (ByVal hwnd As Long, _   ByVal wMsg As Long, _   ByVal wParam As Long, _   lParam As Any) As LongPublic Function EnumWindowProc(ByVal hwnd As Long, _                               ByVal lParam As Long) As Long     'working vars   Dim nSize As Long   Dim sTitle As String   Dim sClass As String      Dim sIDType As String   Dim itmX As ListItem   Dim nodX As Node     'eliminate windows that are not top-level.   If GetParent(hwnd) = 0& And _      IsWindowVisible(hwnd) Then           'get the window title / class name      sTitle = GetWindowIdentification(hwnd, sIDType, sClass)     'add to the listview      Set itmX = Form1.ListView1.ListItems.Add(Text:=sTitle, Key:=CStr(hwnd) & "h")      itmX.SmallIcon = Form1.ImageList1.ListImages("parent").Key      itmX.SubItems(1) = CStr(hwnd)      itmX.SubItems(2) = sIDType      itmX.SubItems(3) = sClass         End If     'To continue enumeration, return True  'To stop enumeration return False (0).  'When 1 is returned, enumeration continues  'until there are no more windows left.   EnumWindowProc = 1   End FunctionPrivate Function GetWindowIdentification(ByVal hwnd As Long, _                                         sIDType As String, _                                         sClass As String) As String   Dim nSize As Long   Dim sTitle As String  'get the size of the string required  'to hold the window title   nSize = GetWindowTextLength(hwnd)     'if the return is 0, there is no title   If nSize > 0 Then         sTitle = Space$(nSize + 1)      Call GetWindowText(hwnd, sTitle, nSize + 1)      sIDType = "title"            sClass = Space$(64)      Call GetClassName(hwnd, sClass, 64)      Else        'no title, so get the class name instead      sTitle = Space$(64)      Call GetClassName(hwnd, sTitle, 64)      sClass = sTitle      sIDType = "class"      End If      GetWindowIdentification = TrimNull(sTitle)End FunctionPublic Function EnumChildProc(ByVal hwnd As Long, _                              ByVal lParam As Long) As Long     'working vars   Dim sTitle As String   Dim sClass As String   Dim sIDType As String   Dim itmX As ListItem  'get the window title / class name   sTitle = GetWindowIdentification(hwnd, sIDType, sClass)  'add to the listview   Set itmX = Form2.ListView1.ListItems.Add(Text:=sTitle)   itmX.SmallIcon = Form2.ImageList1.ListImages("child").Key   itmX.SubItems(1) = CStr(hwnd)   itmX.SubItems(2) = sIDType   itmX.SubItems(3) = sClass         Listview_IndentItem Form2.ListView1.hwnd, CLng(itmX.Index), 1      EnumChildProc = 1   End FunctionPrivate Function TrimNull(startstr As String) As String  Dim pos As Integer  pos = InStr(startstr, Chr$(0))    If pos Then      TrimNull = Left$(startstr, pos - 1)      Exit Function  End If   'if this far, there was 'no Chr$(0), so return the string  TrimNull = startstr  End FunctionPrivate Sub Listview_IndentItem(hwnd As Long, _                                nItem As Long, _                                nIndent As Long)   Dim LV As LVITEM  'if nIndent indicates that indentation  'is requested nItem is the item to indent   If nIndent > 0 Then            With LV        .mask = LVIF_INDENT        .iItem = nItem - 1 'have to subtract 1        .iIndent = nIndent      End With            Call SendMessage(hwnd, LVM_SETITEM, 0&, LV)         End If       End Sub'--end block--''------------------------------------------'' Form Code''------------------------------------------Option ExplicitPrivate Sub Command1_Click()   ListView1.ListItems.Clear   Call EnumWindows(AddressOf EnumWindowProc, &H0)End SubPrivate Sub Form_Load()   Me.Move (Screen.Width - Me.Width) / 2, (Screen.Height - Me.Height) / 2End SubPrivate Sub ListView1_DblClick()   Dim hwndSelected As Long      hwndSelected = Val(ListView1.SelectedItem.Key)      Load Form2   Call Form2.EnumSelectedWindow(ListView1.SelectedItem.Text, hwndSelected)   End Sub'--end block--'   Form2 Code   Add the following code to Form2: -------------------------------------------- Option ExplicitPublic Sub EnumSelectedWindow(sItem As String, hwnd As Long)    ListView1.ListItems.Clear   ListView1.ListItems.Add Text:=sItem, SmallIcon:="parent"      Call EnumChildWindows(hwnd, AddressOf EnumChildProc, &H0)      Me.Show vbModal   End SubPrivate Sub Form_Load()   Me.Move (Screen.Width - Me.Width) / 2, (Screen.Height - Me.Height) / 2   End Sub 

热点排行