当“关闭窗口”从任务栏点击时,如何关注消息框而不是主UI? [英] How to get the focus on message box instead of main UI, when 'close window' click from task bar?

查看:89
本文介绍了当“关闭窗口”从任务栏点击时,如何关注消息框而不是主UI?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的,



我创建了一个基于Windows的应用程序。



如果用户点击'窗口顶部的X'按钮将显示一个消息框(Yes\No)以确认退出应用程序。

当从任务栏单击关闭窗口时,焦点移动到主窗口用户界面代替消息框。



如果从关键窗口点击任务栏,如何关注消息框而不是主用户界面?

解决方案

任何时候你使用'MessageBox.Show由WinForms提出的模态对话框具有独占焦点,并且当用户单击其中一个按钮时它被关闭(并处置)。当MessageBox对话框关闭时,Application Focus应该在显示MessageBox之前返回到具有Focus的最后一个Window / Control。



如果你想得到MessageBox那么允许取消关闭应用程序,以便在用户选择TaskBar中的关闭窗口时显示,这是一个示例;在这个例子中,如果在取消关闭时主窗体被最小化,那么我将WindowState重置为'Normal,并将主窗体置于z顺序的前面并调用Focus on it:

 使用 System.Windows.Forms; 

命名空间 YourNameSapce
{
public partial class MainForm:Form
{
public MainForm()
{
InitializeComponent();
}

// 这假设您将MainForm连接到此EventHandler在设计时
私有 void MainForm_FormClosing( object sender,FormClosingEventArgs e)
{
if

MessageBox.Show ( 退出应用程序?
退出?
MessageBoxButtons.OKCancel,
MessageBoxIcon.Warning,
MessageBoxDefaultButton.Button1)
== DialogResult.Cancel

{
// 取消
e.Cancel = true ;

if this .WindowState == FormWindowState.Minimized)
{
this .WindowState = FormWindowState.Normal;
}

this .BringToFront();
this .Focus();
}
}
}
}


因为你没有提供你如何展示的任何代码消息框,很难给出确切的答案。



这里有一些你可以试试的东西。



1.提供主表格作为 MessageBox <的所有者/ a> [ ^ ]。

通常是这样的:

 MessagtBox.Show( this   Hello World); 





2.探索不同的 MessageBox选项 [ ^ ]。

尝试使用 MessageBoxOptions.DefaultDesktopOnly


Dear All,

I have created an windows based application.

If user click 'X' button on the top of window it will display a message box (Yes\No) to confirm the exit application.
When click on 'Close window' from task bar, focus moves to the Main UI instead of message box.

How to get the focus on message box instead of main UI, when 'close window' click from task bar?

解决方案

Any time you use 'MessageBox.Show the modal dialog box put up by WinForms has exclusive focus, and it is closed (and disposed) when the user clicks one of its Buttons. When the MessageBox Dialog is closed, Application Focus should return to the last Window/Control that had Focus before the MessageBox was shown.

If you want to get the MessageBox that enables cancelling closing the Application to show when user chooses 'Close Window' in the TaskBar, here's an example; in this example, if the Main Form is minimized when the 'Close is canceled, then I reset the WindowState to 'Normal, and bring the Main Form to the front of the z-order and invoke Focus on it:

using System.Windows.Forms;

namespace YourNameSapce
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }

        // this assumes you "connected" the MainForm to this EventHandler at design-time
        private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            if
            (
                MessageBox.Show("Exit the Application ?",
                    "Exit ?",
                    MessageBoxButtons.OKCancel,
                    MessageBoxIcon.Warning,
                    MessageBoxDefaultButton.Button1)
                == DialogResult.Cancel
            )
            {
                // cancel
                e.Cancel = true;

                if (this.WindowState == FormWindowState.Minimized)
                {
                    this.WindowState = FormWindowState.Normal;
                }

                this.BringToFront();
                this.Focus();
            }
        }
    }
}


As you don't present any code for how you show the message box, it is difficult to give an exact answer.

Here are a few things you can try.

1. Provide your main form as the owner of the MessageBox[^].
Usually like this:

MessagtBox.Show(this, "Hello World");



2. Explore the different MessageBox options[^].
Try with MessageBoxOptions.DefaultDesktopOnly


这篇关于当“关闭窗口”从任务栏点击时,如何关注消息框而不是主UI?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆