关于使用enumwindows关闭窗口的问题?
我这段程序请高手给看看?
实现通过按钮关闭 与此窗口有关联的所有窗体
form1 启动窗口 form2,form3 与form1关联需关闭的窗口
以下程式在.BAS
Declare Function GetParent Lib "user32" (ByVal hwnd As Long) As Long
Declare Function EnumWindows Lib "user32" (ByVal lpEnumFunc As Long, ByVal lParam As Long) As Long
Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, lpdwProcessId As Long) As Long
Declare Function SetForegroundWindow Lib "user32" Alias "SetForegroundWindow" (ByVal hwnd As Long) As Long
Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Declare Function OpenProcess Lib "kernel32" _
(ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, _
ByVal dwProcessId As Long) As Long
Declare Function WaitForSingleObject Lib "kernel32" _
(ByVal hHandle As Long, ByVal dwMilliseconds As Long) As Long
Declare Function CloseHandle Lib "kernel32" _
(ByVal hObject As Long) As Long
Declare Function GetExitCodeProcess Lib "kernel32" _
(ByVal hProcess As Long, lpExitCode As Long) As Long
Declare Function TerminateProcess Lib "kernel32" _
(ByVal hProcess As Long, ByVal uExitCode As Long) As Long
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
Public Const SYNCHRONIZE = &H100000
Public Const STILL_ALIVE = &H103
Public Const INFINITE = &HFFFF
Public Const WM_CLOSE = &H10
Public hWnd5 As Long
Function EnumWindowsProc(ByVal hwnd As Long, ByVal lParam As Long) As Boolean
Dim S As String
If GetParent(hwnd) = 0 Then
Dim tid As Long, pid As Long
tid = GetWindowThreadProcessId(hwnd, pid)
If pid = lParam Then
hWnd5 = hwnd
EnumWindowsProc = False
End If
End If
EnumWindowsProc = True ' 表示继续列举 hWnd
End Function
以下程式在Form
Option Explicit
Private ExitCode As Long
Private hProcess As Long
Private isDone As Long
Private Sub Command1_Click()
Dim pid As Long
'我觉得就 这句话有问题?
pid = FindWindow("form1", vbNullString) '查找提示窗体的标题,“操作提示”
Call EnumWindows(AddressOf EnumWindowsProc, pid) '设定hWnd5的值
hProcess = OpenProcess(SYNCHRONIZE , 0, pid)
isDone = False
Do
Call GetExitCodeProcess(hProcess, ExitCode)
DoEvents
Loop While ExitCode = STILL_ALIVE Or isDone
Call CloseHandle(hProcess)
isDone = True
Label1.Caption = "Over"
End Sub
Private Sub Command2_Click()
Dim i As Long
Call SetForegroundWindow(hWnd5)
Call PostMessage(hWnd5, WM_CLOSE, 0, 0)
End Sub
Private Sub Form_Unload(Cancel As Integer)
isDone = True
End Sub
Private Sub Command3_Click()
Form2.Show
End Sub
实现通过按钮关闭 与此窗口有关联的所有窗体
form1 启动窗口 form2,form3 与form1关联需关闭的窗口
以下程式在.BAS
Declare Function GetParent Lib "user32" (ByVal hwnd As Long) As Long
Declare Function EnumWindows Lib "user32" (ByVal lpEnumFunc As Long, ByVal lParam As Long) As Long
Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, lpdwProcessId As Long) As Long
Declare Function SetForegroundWindow Lib "user32" Alias "SetForegroundWindow" (ByVal hwnd As Long) As Long
Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Declare Function OpenProcess Lib "kernel32" _
(ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, _
ByVal dwProcessId As Long) As Long
Declare Function WaitForSingleObject Lib "kernel32" _
(ByVal hHandle As Long, ByVal dwMilliseconds As Long) As Long
Declare Function CloseHandle Lib "kernel32" _
(ByVal hObject As Long) As Long
Declare Function GetExitCodeProcess Lib "kernel32" _
(ByVal hProcess As Long, lpExitCode As Long) As Long
Declare Function TerminateProcess Lib "kernel32" _
(ByVal hProcess As Long, ByVal uExitCode As Long) As Long
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
Public Const SYNCHRONIZE = &H100000
Public Const STILL_ALIVE = &H103
Public Const INFINITE = &HFFFF
Public Const WM_CLOSE = &H10
Public hWnd5 As Long
Function EnumWindowsProc(ByVal hwnd As Long, ByVal lParam As Long) As Boolean
Dim S As String
If GetParent(hwnd) = 0 Then
Dim tid As Long, pid As Long
tid = GetWindowThreadProcessId(hwnd, pid)
If pid = lParam Then
hWnd5 = hwnd
EnumWindowsProc = False
End If
End If
EnumWindowsProc = True ' 表示继续列举 hWnd
End Function
以下程式在Form
Option Explicit
Private ExitCode As Long
Private hProcess As Long
Private isDone As Long
Private Sub Command1_Click()
Dim pid As Long
'我觉得就 这句话有问题?
pid = FindWindow("form1", vbNullString) '查找提示窗体的标题,“操作提示”
Call EnumWindows(AddressOf EnumWindowsProc, pid) '设定hWnd5的值
hProcess = OpenProcess(SYNCHRONIZE , 0, pid)
isDone = False
Do
Call GetExitCodeProcess(hProcess, ExitCode)
DoEvents
Loop While ExitCode = STILL_ALIVE Or isDone
Call CloseHandle(hProcess)
isDone = True
Label1.Caption = "Over"
End Sub
Private Sub Command2_Click()
Dim i As Long
Call SetForegroundWindow(hWnd5)
Call PostMessage(hWnd5, WM_CLOSE, 0, 0)
End Sub
Private Sub Form_Unload(Cancel As Integer)
isDone = True
End Sub
Private Sub Command3_Click()
Form2.Show
End Sub
...全文
相关推荐
用EnumWindows枚举当前所有已打开窗口标题栏名称
总的来说,`EnumWindows`函数是Windows API中用于枚举系统窗口的重要工具,通过合理使用它和相关的API函数,我们可以获取到丰富的系统窗口信息,从而实现各种功能强大的应用程序。
EnumWindows-枚举桌面窗口
另外,要注意的是,由于权限问题,枚举系统进程中的窗口可能需要更高的权限运行。
enumwindows 遍历窗口
`EnumWindows`是Windows API中的一个非常重要的函数,主要用于遍历系统中所有的顶层窗口。这个函数在Windows编程中被广泛使用,特别是在需要获取或者操作所有可见窗口的场景下。
易语言-EnumWindows枚举窗口
理解并掌握EnumWindows函数的使用,对于提升易语言编程能力,尤其是系统编程方向的技能至关重要。同时,这也是一种深入学习Windows API和操作系统原理的有效途径。
利用Win32 API EnumWindows找出系统现有的窗口示范
ShowMessageFmt('窗口句柄: %x, 标题: %s', [hwnd, WindowText(hwnd)]); Result := True; // 继续枚举下一个窗口 end; EnumWindows


API
