问题描述
我认为这里多余的皱纹使这个问题不再是其他居中问题的重复。
我想对自动关闭的消息框使用@BSharp given here的方法:
var w = new Form() { Size = new Size(0,0) };
Task.Delay(TimeSpan.FromSeconds(10))
.ContinueWith((t) => w.Close(),TaskScheduler.FromCurrentSynchronizationContext());
MessageBox.Show(w,message,caption);
但是,在我的两个监视器设置中,该应用程序在右监视器上运行,并且消息框显示在左监视器上。如何使其显示在同一位置但在右监视器上?
解决方法
private static Form CreateDummyForm(Form mainForm) {
Form dummy = new Form(); // { Opacity = 0,ShowInTaskbar = false };
dummy.Location = mainForm.Location;
IntPtr hwnd = dummy.Handle; // force handle creation
mainForm.Load += delegate {
IntPtr blah = dummy.Handle;
};
mainForm.LocationChanged += delegate {
dummy.Location = mainForm.Location;
};
return dummy;
}
[STAThread]
static void Main() {
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Form mf = new Form() { Size = new Size(400,400) };
Button btn = new Button { Text = "Button" };
Form dummy = CreateDummyForm(mf);
btn.Click += delegate {
Task.Delay(TimeSpan.FromSeconds(5)).ContinueWith((t) => { dummy.Close(); },TaskScheduler.FromCurrentSynchronizationContext());
MessageBox.Show(dummy,"blah",MessageBoxButtons.OKCancel);
dummy = CreateDummyForm(mf); // Close disposes the dummy form
};
mf.Controls.Add(btn);
Application.Run(mf);
}