Nogen kan få det her til at virke?
Programmet har kun et formål, at lukke et andet program eller vindue.Option Explicit
'This API is for the SendMessage function call - basically _
sends a message to the specified hWnd
Private Declare Function SendMessage Lib "user32" _
Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As _
Long, ByVal wParam As Long, ByVal lParam As Any) As Long
'This API is for the FindWindow call - is used to Find a _
window based on a given Window class name or Window title _
(caption)
Private Declare Function FindWindow Lib "user32" _
Alias "FindWindowA" (ByVal lpClassName As String, ByVal _
lpWindowName As String) As Long
'This API will return a formatted string for a give DLL _
error
Private Declare Function FormatMessage Lib "kernel32" _
Alias "FormatMessageA" (ByVal dwFlags As Long, ByVal _
lpSource As Long, ByVal dwMessageId As Long, ByVal _
dwLanguageId As Long, ByVal lpBuffer As String, ByVal nSize _
As Long, Arguments As Any) As Long
'const required for FormatMessage
Private Const FORMAT_MESSAGE_FROM_SYSTEM = &H1000
'Our string buffer length
Private Const BUFFER_LENGTH As Long = 255
'This const is used with the SendMessage call
Private Const WM_CLOSE As Long = &H10
'Our custom consts (for readability)
Private Const NO_ERROR As Long = 0
Private Const BYVAL_NULL As Long = 0&
Private Sub Command1_Click()
'Function call
'in this case we are looking for a window with the _
Caption "VB"
'change it to suit your needs
KillWindow , "VB"
End Sub
Public Function KillWindow(Optional WindowClass As String = _
vbNullString, Optional WindowTitle As String = vbNullString)
Dim lhWnd As Long
Dim lRes As Long
Dim lErrorNumber As Long
Dim sErrorDescription As String * BUFFER_LENGTH
Dim lFlags As Long
'On Error GoTo Err_Handler
'Ensure our strings are null terminated
WindowClass = WindowClass & vbNullString
WindowTitle = WindowTitle & vbNullString
'In this case we are going to try and find the the _
WindowHandle,
'if we already knew the hWnd we could just pass that _
straight to the SendMessage call.
'Call the FindWindow API, this will return us a valid _
WindowHandle,
'If its not a valid window (window isn't found) _
FindWindow will return
'0 and Err.LastDLLError will be populated with an _
Windows error number
'(see the FormatMessage call)
lhWnd = FindWindow(WindowClass, WindowTitle)
'We assume we have a valid WindowHandle, now its time _
to send the
'WM_CLOSE message to tell the window \ app to close.
lRes = SendMessage(lhWnd, WM_CLOSE, BYVAL_NULL, BYVAL_NULL)
Err_Handler:
'Our Error Handler.
'Check the standard windows error number *and* the DLL Error to ensure
'that neither the FindWindow or the SendMessage call failed.
If Err.Number <> NO_ERROR Or Err.LastDllError <> NO_ERROR Then
'Something failed - need to decide what it was
If Err.Number <> NO_ERROR Then
'Must be a straight VB error
lErrorNumber = Err.Number
sErrorDescription = Err.Description
Else
'An API failed - probably FindWindow due to the _
Window not being found.
lErrorNumber = Err.LastDllError
'This API will take the Error number generated _
from the API call and
'Format it into a English string for us to _
report back to the user.
lRes = FormatMessage _
(FORMAT_MESSAGE_FROM_SYSTEM, BYVAL_NULL, lErrorNumber, _
BYVAL_NULL, sErrorDescription, BUFFER_LENGTH, BYVAL_NULL)
End If
'Raise the error up to whoever called the function.
Err.Raise lErrorNumber, "KillWindow", sErrorDescription
End If
End Function
