Results 1 to 3 of 3

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

  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

  2. #2
    Frenzied Member
    Join Date
    Apr 2005
    Posts
    1,907

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

    Thank u for sharing this code. Could show me how i can highlight an externial listview item ?

  3. #3
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

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

    Highlighting an external listivews item(s) is a bit more difficult then finding a windows handle. You havew to find the child handle for the listview and then send a few messages to find the item and highlight it. I know there are a few threads on the forums that covered this already. Try a search and I know you will find what you need.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

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