我有一个简单的winforms应用程序。该应用程序以MainForm开始,该窗体始终处于隐藏状态。MainForm打开另一个名为Notification的窗体(.Show),该窗体在10秒后关闭。因此,如果MainForm在Notification仍然打开时打开对话框窗体(.ShowDialog),则如果Notification达到10秒并关闭,则打开的对话框也会关闭。
通知窗体和对话框窗体都具有TopMost = True属性。
例如:
短示例: 即使我的应用程序只执行此操作,OtherDialogForm也会关闭:
通知窗体和对话框窗体都具有TopMost = True属性。
例如:
短示例: 即使我的应用程序只执行此操作,OtherDialogForm也会关闭:
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
new Notification("Title", "Content").Show(); // closes itself after 10 seconds
new OtherDialogForm().ShowDialog(); // being closed by form above (not wanted)
}
}
更长的例子:MainForm.cs
public partial class MainForm : Form
{
protected override void SetVisibleCore(bool value)
{
base.SetVisibleCore(false);
}
public MainForm()
{
CreateNotification();
}
public void CreateNotification()
{
new Notification.Show();
}
// triggered while Notification (from above) is still open
private async void TriggeredByHotkey()
{
using (OtherDialogForm dialog = new OtherDialogForm())
{
dialog.ShowDialog();
// if DialogResult == ...
}
}
}
Notification.cs
public partial class Notification : Form
{
Timer timer;
public Notification()
{
// sets timer tick to 10 seconds,
// then calls void with this.Close()
}
// not sure if I really need this, but the problem is still there without
protected override CreateParams CreateParams
{
get
{
var cp = base.CreateParams;
cp.ExStyle |= 8; // Turn on WS_EX_TOPMOST
return cp;
}
}
}
我真的不知道为什么对话框会随着通知一起关闭。