AW: 10 x starten = 10 x schliessen!
08.03.2010 15:34:38
Josef
Hallo Aki,
Label1_Click startet die Awendung und stellt, wenn die Anwendung schon läuft, das Fenster wieder her.
cmdMinimize_Click minimiert die Anwendung, cmdTerminate_Click beendet die Anwendung.
' **********************************************************************
' Modul: Modul2 Typ: Allgemeines Modul
' **********************************************************************
Option Explicit
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As _
Long
Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As _
Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Private Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As _
Long, ByVal uExitCode As Long) As Long
Private Declare Function ShowWindow Lib "user32" (ByVal hWnd As Long, ByVal _
nCmdShow As Long) As Long
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal _
lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hWnd As _
Long, lpdwProcessId As Long) As Long
Private Declare Function GetWindow Lib "user32" (ByVal hWnd As Long, ByVal wCmd _
As Long) As Long
Private Declare Function GetParent Lib "user32" (ByVal hWnd As Long) As Long
Private Const PROCESS_TERMINATE = &H1
Private Const GW_HWNDNEXT As Long = &H2
Private Const SW_MINIMIZE = &H6
Private Const SW_RESTORE = &H9
Public lTaskID As Long
Public hWnd As Long
Private Retval As Long
Private Function ShellTohWnd(ByVal hhwPfad As String, Optional Mode As VbAppWinStyle)
Dim ProcHWN As Long
lTaskID = Shell(hhwPfad, Mode)
Retval = FindWindow(vbNullString, vbNullString)
Do While Retval <> 0
If GetParent(Retval) = 0 Then
Call GetWindowThreadProcessId(Retval, ProcHWN)
If ProcHWN = lTaskID Then
ShellTohWnd = Retval
Exit Do
End If
End If
Retval = GetWindow(Retval, GW_HWNDNEXT)
Loop
End Function
Private Sub cmdTerminate_Click()
Dim hTask As Long
Dim lResult As Long
If lTaskID > 0 Then
hTask = OpenProcess(PROCESS_TERMINATE, 0&, lTaskID)
lResult = TerminateProcess(hTask, 1&)
lResult = CloseHandle(hTask)
lTaskID = 0
hWnd = 0
End If
End Sub
Private Sub cmdMinimize_Click()
Dim lResult As Long
If lTaskID > 0 Then lResult = ShowWindow(hWnd, SW_MINIMIZE)
End Sub
Private Sub Label1_Click()
Dim lResult As Long
If lTaskID = 0 Then
hWnd = ShellTohWnd("c:\windows\notepad.exe", vbNormalNoFocus)
Else
lResult = ShowWindow(hWnd, SW_RESTORE)
End If
End Sub
Gruß Sepp