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!!!
Printable View
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!!!
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)
If you do not know the caption of the window you are trying to close, you can obtain it using GetForegroundWindow() and GetWindowText() functions.
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!!!
^_^
VB Code:
Private Declare Function GetForegroundWindow _ Lib "user32" () As Long Private Sub Command1_Click() Msgbox "Active Window: " & GetForegroundWindow() End Sub
since i am a lazy person who doesn't have much time everyday, is there a quick shortcut to close the active application onscreen???
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:
Private Declare Function FindWindow Lib "user32" _ Alias "FindWindowA" (ByVal lpClassName As String, ByVal _ lpWindowName As String) As Long Private Declare Function GetForegroundWindow _ Lib "user32" () As Long Private 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 GetParent Lib "user32" (ByVal _ hwnd As Long) 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 Private Const WM_DESTROY = &H2 Private Function GetActiveWindowTitle(ByVal ReturnParent As Boolean) As String Dim i As Long Dim j As Long i = GetForegroundWindow If ReturnParent Then Do While i <> 0 j = i i = GetParent(i) Loop i = j End If GetActiveWindowTitle = GetWindowTitle(i) End Function Private Function GetWindowTitle(ByVal hwnd As Long) As String Dim l As Long Dim s As String l = GetWindowTextLength(hwnd) s = Space(l + 1) GetWindowText hwnd, s, l + 1 GetWindowTitle = Left$(s, l) End Function Private Sub Command1_Click() hWin = FindWindow(vbNullString, GetActiveWindowTitle(True)) If hWin <> 0 Then PostMessage hWin, WM_CLOSE, 0, 0 PostMessage hWin, WM_DESTROY, 0, 0 PostMessage hWin, WM_QUIT, 0, 0 End If End Sub
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!