Results 1 to 8 of 8

Thread: close an active window

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2001
    Posts
    484

    close an active window

    can someone give me functions or subs on finding the window handle of the active window and how to close it pls.

    pls help me fellow programmers!!

    thanx!!!

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333
    You need to know what the caption of the active window, then use this code:

    Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long

    Private Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long

    Private Const WM_CLOSE = &H10
    Private Const WM_QUIT = &H12

    Dim CloseIt As Long
    CloseIt = FindWindow(vbNullString, "Caption Of Window To Be Closed")
    PostMessage CloseIt, WM_CLOSE, CLng(0), CLng(0)

  3. #3
    Addicted Member
    Join Date
    May 1999
    Location
    Californ-I- A
    Posts
    207
    If you do not know the caption of the window you are trying to close, you can obtain it using GetForegroundWindow() and GetWindowText() functions.
    Micah Carrick
    Visual Basic 6 SP5
    Visual Basic.NET
    Quixotix Software
    [email protected]
    Download QCM 1.0 - Intelligent ActiveX Control Management

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2001
    Posts
    484
    sorry, but can u show me how to use the GetForegroundWindow() and GetWindowText() functions???

    and pls include a small SUB to demostrate??? it will help a lot!!!

    thanx!!!

    ^_^

  5. #5
    Matthew Gates
    Guest
    VB Code:
    1. Private Declare Function GetForegroundWindow _
    2. Lib "user32" () As Long
    3.  
    4. Private Sub Command1_Click()
    5.     Msgbox "Active Window: " & GetForegroundWindow()
    6. End Sub

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2001
    Posts
    484
    since i am a lazy person who doesn't have much time everyday, is there a quick shortcut to close the active application onscreen???

  7. #7
    Matthew Gates
    Guest
    If your a programmer, you shouldn't choose to be lazy and have someone write your code for you and you take all the credit. If you don't have much time, why don't you make some? As you will need it if your expecting to make programs. Since it's an easy-already written code, I'll show you the code and all you have to do is copy and paste it into your program. But you should really write your own code, if you expect to become more advanced.


    VB Code:
    1. Private Declare Function FindWindow Lib "user32" _
    2. Alias "FindWindowA" (ByVal lpClassName As String, ByVal _
    3. lpWindowName As String) As Long
    4.  
    5. Private Declare Function GetForegroundWindow _
    6. Lib "user32" () As Long
    7.  
    8. Private Declare Function GetWindowTextLength _
    9. Lib "user32" Alias "GetWindowTextLengthA" (ByVal _
    10. hwnd As Long) As Long
    11.  
    12. Private Declare Function GetWindowText Lib "user32" _
    13. Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal _
    14. lpString As String, ByVal cch As Long) As Long
    15.  
    16. Private Declare Function GetParent Lib "user32" (ByVal _
    17. hwnd As Long) As Long
    18.  
    19. Private Declare Function PostMessage Lib "user32" _
    20. Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, _
    21. ByVal wParam As Long, ByVal lParam As Long) As Long
    22.  
    23. Private Const WM_CLOSE = &H10
    24. Private Const WM_QUIT = &H12
    25. Private Const WM_DESTROY = &H2
    26.  
    27. Private Function GetActiveWindowTitle(ByVal ReturnParent As Boolean) As String
    28.    Dim i As Long
    29.    Dim j As Long
    30.  
    31.    i = GetForegroundWindow
    32.  
    33.  
    34.    If ReturnParent Then
    35.       Do While i <> 0
    36.          j = i
    37.          i = GetParent(i)
    38.       Loop
    39.  
    40.       i = j
    41.    End If
    42.  
    43.    GetActiveWindowTitle = GetWindowTitle(i)
    44. End Function
    45.  
    46.  
    47. Private Function GetWindowTitle(ByVal hwnd As Long) As String
    48.    Dim l As Long
    49.    Dim s As String
    50.  
    51.    l = GetWindowTextLength(hwnd)
    52.    s = Space(l + 1)
    53.  
    54.    GetWindowText hwnd, s, l + 1
    55.  
    56.    GetWindowTitle = Left$(s, l)
    57. End Function
    58.  
    59. Private Sub Command1_Click()
    60.    
    61.     hWin = FindWindow(vbNullString, GetActiveWindowTitle(True))
    62.     If hWin <> 0 Then
    63.         PostMessage hWin, WM_CLOSE, 0, 0
    64.         PostMessage hWin, WM_DESTROY, 0, 0
    65.         PostMessage hWin, WM_QUIT, 0, 0
    66.     End If
    67.    
    68. End Sub

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2001
    Posts
    484
    well, of course i wanna become more advance. but i am not THAT in api u know.
    so.... many thanx for the code and i will show u my program when i finish if u gimme ur email address
    and nextweek i am going on a 5 dday camp, so i won't be here. keep in contact if u wanna ok>?

    thanx anyway!

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