加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

确定特定单元是否打开模态对话框 – Delphi

发布时间:2020-12-15 09:04:51 所属栏目:大数据 来源:网络整理
导读:我在Delphi 7中有一个应用程序,它会弹出几个条件的模态对话框.我正在尝试确定来自特定单元的对话框是否从另一个单元打开并关闭它.到目前为止,我已尝试使用以下代码: Wnd := GetLastActivePopup(Application.Handle);if (Wnd 0) and (Wnd Application.Handle
我在Delphi 7中有一个应用程序,它会弹出几个条件的模态对话框.我正在尝试确定来自特定单元的对话框是否从另一个单元打开并关闭它.到目前为止,我已尝试使用以下代码:

Wnd := GetLastActivePopup(Application.Handle);
if (Wnd <> 0) and (Wnd <> Application.Handle) then
   PostMessage(Wnd,wm_close,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);
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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读