Windows 当触发两个连续的 MessageBox pop 时表单失去焦点
Windows forms lost focus when two consecutive MessageBox pop are fired
Class MainForm:Form {
Public CheckValidation()
{
var controller = new FormController();
controller.checkValidation();
}
}
class FormController {
public checkValidation ()
{
MessageBox.Show('test_a',MessageBoxButtons.OK, MessageBoxDefaultButton.Button1, MessageBoxOptions.ServiceNotification);
MessageBox.Show('test_b',MessageBoxButtons.OK, MessageBoxDefaultButton.Button1, MessageBoxOptions.ServiceNotification);
}
}
问题是第一个消息框弹出框关闭后,焦点丢失到windows上的一些其他应用程序,例如outlook。
期望是将焦点发送到弹出的第二个消息框,排队等候下一个执行。这样我就不用手动点击第二个弹出框使其激活来关闭它了。而在第二个弹出框关闭焦点后return到主窗体
解法:
删除 'MessageBoxOptions.ServiceNotification'
参数有效。
备选方案一:
如果我们希望明确指定消息框应始终位于最前面和最上面 window,我们可以指定参数 ((0x40000),它是 MB_TOPMOST 选项的标志。
MessageBox.Show('test_a', MessageBoxButtons.OK, MessageBoxDefaultButton.Button1, (MessageBoxOptions)0x40000); //Message Box Top Most (MB_TOPMOST) = 0x40000
备选方案二:
另一种明确指定消息框应始终位于最前面和最上面的方法 window,我们可以创建一个新的表单对象并将“TopMost”布尔选项设置为“True”。
MessageBox.Show(new Form() { TopMost = true }, 'test_b', MessageBoxButtons.OK, MessageBoxDefaultButton.Button1);
Class MainForm:Form {
Public CheckValidation()
{
var controller = new FormController();
controller.checkValidation();
}
}
class FormController {
public checkValidation ()
{
MessageBox.Show('test_a',MessageBoxButtons.OK, MessageBoxDefaultButton.Button1, MessageBoxOptions.ServiceNotification);
MessageBox.Show('test_b',MessageBoxButtons.OK, MessageBoxDefaultButton.Button1, MessageBoxOptions.ServiceNotification);
}
}
问题是第一个消息框弹出框关闭后,焦点丢失到windows上的一些其他应用程序,例如outlook。
期望是将焦点发送到弹出的第二个消息框,排队等候下一个执行。这样我就不用手动点击第二个弹出框使其激活来关闭它了。而在第二个弹出框关闭焦点后return到主窗体
解法:
删除 'MessageBoxOptions.ServiceNotification'
参数有效。
备选方案一:
如果我们希望明确指定消息框应始终位于最前面和最上面 window,我们可以指定参数 ((0x40000),它是 MB_TOPMOST 选项的标志。
MessageBox.Show('test_a', MessageBoxButtons.OK, MessageBoxDefaultButton.Button1, (MessageBoxOptions)0x40000); //Message Box Top Most (MB_TOPMOST) = 0x40000
备选方案二: 另一种明确指定消息框应始终位于最前面和最上面的方法 window,我们可以创建一个新的表单对象并将“TopMost”布尔选项设置为“True”。
MessageBox.Show(new Form() { TopMost = true }, 'test_b', MessageBoxButtons.OK, MessageBoxDefaultButton.Button1);