For example, if you have a WinForms application that pops up a MessageBox when you press some button1, you will still be able to close the app using Windows "Close Window" menu (right click in the task bar): private void button1_Click(object sender, EventArgs e) { MessageBox.Show("Don't click me. I want to be closed automatically!"); } protected override void WndProc(ref System.Windows.Forms.Message m) { const int WM_SYSCOMMAND = 0x0112; const int SC_CLOSE = 0xF060; if (m.Msg == WM_SYSCOMMAND) // this is sent even if a modal MessageBox is shown { if ((int)m.WParam == SC_CLOSE) { CloseModalWindows(); Close(); } } base.WndProc(ref m); }您当然可以在代码中的其他地方使用CloseModalWindows,这只是一个示例.You could use CloseModalWindows somewhere else in your code of course, this is just a sample. 这篇关于强制以编程方式关闭MessageBox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-05 07:14