Results 1 to 3 of 3

Thread: [VB/API] Find hWnd, ClassName, or Window Caption. And close external apps

Threaded View

  1. #1

    Thread Starter
    Hyperactive Member Daskalos's Avatar
    Join Date
    Jun 2002
    Location
    Just behind YOU...
    Posts
    364

    [VB/API] Find hWnd, ClassName, or Window Caption. And close external apps

    With this app you can find a window caption by its hwnd or ClassName, can find hwnd by caption or classname, find classname by hwnd or window caption and so on ;-)
    also can close an external app by hWnd, Window Caption or ClassName

    VB Code:
    1. Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, _
    2.                                                                       ByVal lpWindowName As String) As Long
    3. Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, _
    4.                                                                         ByVal wMsg As Long, _
    5.                                                                         ByVal wParam As Long, _
    6.                                                                         ByVal lParam As Long) As Long
    7. Private Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hwnd As Long, _
    8.                                                                           ByVal lpClassName As String, _
    9.                                                                           ByVal nMaxCount As Long) _
    10.                                                                           As Long
    11. Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, _
    12.                                                                            ByVal lpString As String, _
    13.                                                                            ByVal cch As Long) As Long
    14. Private Declare Function GetWindowTextLength Lib "user32" Alias "GetWindowTextLengthA" (ByVal hwnd As Long) As Long
    15.                                                                            
    16. Private Const WM_SYSCOMMAND = &H112
    17. Private Const SC_CLOSE = &HF060&
    18. Public dl&
    19. Public xhwnd&
    20.  
    21.  
    22. Private Function GetText(Get_hWnd As Long) As String
    23. Dim lenTxt As Long, retText As String
    24.  
    25. lenTxt = GetWindowTextLength(Get_hWnd) + 1
    26.  
    27. retText = String$(lenTxt, " ")
    28. GetWindowText Get_hWnd, retText, lenTxt
    29.  
    30. GetText = retText
    31.  
    32. End Function
    33.  
    34. Private Sub Command1_Click()
    35.     Text2.Text = FindWindow(vbNullString, Text1.Text)
    36. End Sub
    37.  
    38. Private Sub Command2_Click()
    39. Dim s As String
    40.     s = Space$(255)
    41.  
    42.     Call GetClassName(Text2.Text, s, Len(s))
    43.    
    44.     If InStr(1, s, vbNullChar) Then
    45.         s = Left$(s, InStr(1, s, vbNullChar) - 1)
    46.     End If
    47.     Text3.Text = s
    48. End Sub
    49.  
    50. Private Sub Command3_Click()
    51.     Text2.Text = FindWindow(Text3.Text, vbNullString)
    52. End Sub
    53.  
    54. Private Sub Command4_Click()
    55. Text1.Text = GetText(Text2.Text)
    56. End Sub
    57.  
    58. Private Sub Command5_Click()
    59.     Text1.Text = GetText(FindWindow(Text3.Text, vbNullString))
    60. End Sub
    61.  
    62. Private Sub Command6_Click()
    63. Select Case True
    64.     Case Option1.Value = True
    65.         xhwnd = FindWindow(vbNullString, Text1.Text)
    66.         dl = SendMessage(xhwnd, WM_SYSCOMMAND, SC_CLOSE, 0&)
    67.     Case Option2.Value = True
    68.         dl = SendMessage(Text2.Text, WM_SYSCOMMAND, SC_CLOSE, 0&)
    69.     Case Option3.Value = True
    70.         xhwnd = FindWindow(Text3.Text, vbNullString)
    71.         dl = SendMessage(xhwnd, WM_SYSCOMMAND, SC_CLOSE, 0&)
    72.     Case Else
    73.         Exit Sub
    74. End Select
    75. End Sub
    Attached Files Attached Files
    [vbcode]Dim Daskalos As NewBie
    If My.english = Wrong Then
    Forgive My.Poor.English
    End If[/vbcode]
    Ða§kalø§
    ICQ#: 36146307
    Current ICQ status:
    More ways to contact me

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width