Results 1 to 4 of 4

Thread: VB6 - How to close the application on top with Hotkeys

  1. #1

    Thread Starter
    Lively Member
    Join Date
    May 2006
    Posts
    84

    VB6 - How to close the application on top with Hotkeys

    Hi, this is a code to close the Application on top with an hotkey, copy this and add a timer, press F2 to close the active application

    VB Code:
    1. Option Explicit
    2.  
    3.       Private Declare Function GetAsyncKeyState Lib "user32.dll" _
    4.  (ByVal vKey As Long) As Integer
    5.      
    6.       Private Declare Function GetForegroundWindow Lib "user32" () As Long
    7.      
    8.       Private Declare Function GetWindowText Lib "user32" _
    9.         Alias "GetWindowTextA" (ByVal hwnd As Long, _
    10.         ByVal lpString As String, ByVal cch As Long) As Long
    11.      
    12.       Private Declare Function WaitForSingleObject Lib "kernel32" _
    13.          (ByVal hHandle As Long, _
    14.          ByVal dwMilliseconds As Long) As Long
    15.  
    16.       Private Declare Function FindWindow Lib "user32" _
    17.          Alias "FindWindowA" _
    18.          (ByVal lpClassName As String, _
    19.          ByVal lpWindowName As String) As Long
    20.  
    21.       Private Declare Function PostMessage Lib "user32" _
    22.          Alias "PostMessageA" _
    23.          (ByVal hwnd As Long, _
    24.          ByVal wMsg As Long, _
    25.          ByVal wParam As Long, _
    26.          ByVal lParam As Long) As Long
    27.  
    28.       Private Declare Function IsWindow Lib "user32" _
    29.          (ByVal hwnd As Long) As Long
    30.  
    31.       Private Declare Function OpenProcess Lib "kernel32" _
    32.          (ByVal dwDesiredAccess As Long, _
    33.          ByVal bInheritHandle As Long, _
    34.          ByVal dwProcessId As Long) As Long
    35.      
    36.       Private Declare Function GetWindowThreadProcessId Lib "user32" _
    37.          (ByVal hwnd As Long, _
    38.          lpdwProcessId As Long) As Long
    39.  
    40.       'Constants that are used by the API
    41.       Const WM_CLOSE = &H10
    42.       Const INFINITE = &HFFFFFFFF
    43.       Const SYNCHRONIZE = &H100000
    44.      
    45.      
    46.       Function GetActiveWindowText() As String
    47.         Dim strBuff As String
    48.         Dim RetVal As Long
    49.         strBuff = Space$(255)
    50.         RetVal = GetWindowText(GetForegroundWindow(), strBuff, Len(strBuff))
    51.         If RetVal Then GetActiveWindowText = Left$(strBuff, RetVal)
    52.     End Function
    53.      
    54.       Function KillActive()
    55.       'Closes Windows
    56.          Dim hWindow As Long
    57.          Dim hThread As Long
    58.          Dim hProcess As Long
    59.          Dim lProcessId As Long
    60.          Dim lngResult As Long
    61.          Dim lngReturnValue As Long
    62.  
    63.          hWindow = FindWindow(vbNullString, GetActiveWindowText)
    64.          hThread = GetWindowThreadProcessId(hWindow, lProcessId)
    65.          hProcess = OpenProcess(SYNCHRONIZE, 0&, lProcessId)
    66.          lngReturnValue = PostMessage(hWindow, WM_CLOSE, 0&, 0&)
    67.          lngResult = WaitForSingleObject(hProcess, INFINITE)
    68.       End Function
    69.  
    70. Private Sub Timer1_Timer()
    71. If GetAsyncKeyState(vbKeyF2) Then
    72. KillActive
    73. End If
    74. End Sub
    Attached Files Attached Files

  2. #2
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    Re: VB6 - How to close the application on top with Hotkeys

    a couple of comments:

    you should use the RegisterHotKey API to create a global hotkey, rather than using a Timer.

    also, GetForegroundWindow already gives you the hWnd of the window. There's no need to get the window text and then use FindWindow to get the value you already had.

  3. #3

    Thread Starter
    Lively Member
    Join Date
    May 2006
    Posts
    84

    Re: VB6 - How to close the application on top with Hotkeys

    Thx for informations, sorry but i'm not an expert, so i'm sorry if i wrote not essential code.

  4. #4
    Member
    Join Date
    Mar 2009
    Posts
    40

    Re: VB6 - How to close the application on top with Hotkeys

    alot thanks @paraidy
    i was search for it for VB6 all i was found for Vb.net
    i hope it can help me on my program
    thanks

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