|
-
Sep 7th, 2001, 09:35 AM
#1
Thread Starter
Hyperactive Member
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!!!
-
Sep 7th, 2001, 11:11 AM
#2
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)
-
Sep 7th, 2001, 11:34 AM
#3
Addicted Member
If you do not know the caption of the window you are trying to close, you can obtain it using GetForegroundWindow() and GetWindowText() functions.
-
Sep 7th, 2001, 11:46 PM
#4
Thread Starter
Hyperactive Member
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!!!
^_^
-
Sep 8th, 2001, 01:31 AM
#5
VB Code:
Private Declare Function GetForegroundWindow _
Lib "user32" () As Long
Private Sub Command1_Click()
Msgbox "Active Window: " & GetForegroundWindow()
End Sub
-
Sep 8th, 2001, 07:30 AM
#6
Thread Starter
Hyperactive Member
since i am a lazy person who doesn't have much time everyday, is there a quick shortcut to close the active application onscreen???
-
Sep 8th, 2001, 01:24 PM
#7
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
-
Sep 10th, 2001, 07:36 AM
#8
Thread Starter
Hyperactive Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|