我在Delphi 7中有一个应用程序,它会弹出几个条件的模态对话框.我正在尝试确定来自特定单元的对话框是否从另一个单元打开并关闭它.到目前为止,我已尝试使用以下代码: Wnd := GetLast
Wnd := GetLastActivePopup(Application.Handle); if (Wnd <> 0) and (Wnd <> Application.Handle) then PostMessage(Wnd, wm_close,0,0);
但是,它会关闭所有打开的对话框.当我尝试指定特定表单时,例如:
if (Wnd <> 0) and (Wnd <> FormTest.Handle) then
它会引发访问冲突错误.
如何确定是否正在弹出特定单元的对话框?
有一个简单的解决方案可行.你可以使用:
procedure TForm1.Button2Click(Sender: TObject); var h: hwnd; begin h := FindWindow(PChar('TForm1'), PChar('Form1')); if h <> 0 then PostMessage(h, WM_CLOSE, 0,0); end;
它可以很好地识别TForm1窗口是否有句柄.显而易见的是,FindWindow将在整个OS系统中寻找窗口.现在,如果你想要更快的东西,你可以使用@Remy解决方案,它只会寻找应用程序的表单.
来自MSDN:
FindWindow function:
Retrieves a handle to the top-level window whose class name and window
name match the specified strings. This function does not search child
windows. This function does not perform a case-sensitive search.
要搜索子窗口,请使用以下功能:
FindWindowEx function:
Retrieves a handle to a window whose class name and window name match
the specified strings. The function searches child windows, beginning
with the one following the specified child window. This function does
not perform a case-sensitive search.
这些是两个功能的链接:FindWindow和FindWindowEx