小智 31
if (this.Visible && !this.CanFocus)
{
// modal child windows are open
}
Run Code Online (Sandbox Code Playgroud)
简而言之:只要模态窗口打开,打开模态窗体就是在主窗体上执行块执行,因此在模态窗体关闭之前,您的主窗体永远无法检查它是否打开了任何模态窗体.换句话说,你的问题是基于对模态形式如何运作的误解,所以它完全没有实际意义.
对于什么它的价值,它是可以说,如果有任何模式窗体打开:
foreach (Form f in Application.OpenForms)
{
if (f.Modal)
{
// do stuff
}
}
Run Code Online (Sandbox Code Playgroud)
您可以将事件用于EnterThreadModal和LeaveThreadModal.以下是如何执行此操作的示例:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
static class Program
{
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.EnterThreadModal += new EventHandler(Application_EnterThreadModal);
Application.LeaveThreadModal += new EventHandler(Application_LeaveThreadModal);
Application.Run(new Form1());
}
private static void Application_EnterThreadModal(object sender, EventArgs e)
{
IsModalDialogOpen = true;
}
private static void Application_LeaveThreadModal(object sender, EventArgs e)
{
IsModalDialogOpen = false;
}
public static bool IsModalDialogOpen { get; private set; }
}
}
Run Code Online (Sandbox Code Playgroud)