暂时禁用关闭按钮

暂时禁用关闭按钮

本文介绍了暂时禁用关闭按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要禁用的只是的关闭按钮(最小化和最大化应该被允许)暂时的。

I need to disable just the close button (minimize and maximize should be allowed) temporarily.

每一个解决方案,我已经试过禁用的所有的按钮或只是禁用关闭按钮永久。有没有办法来暂时办呢?

Every solution I've tried disables all the buttons or just disables the close button permanently. Is there a way to do it temporarily?

推荐答案

该办法的永久的禁用关闭按钮设置,在窗体的窗口类。从WinForms应用程序做到这一点,覆盖表单的的和 SC_NOCLOSE 标志使用添加 | 运算符,如:

The way to permanently disable the close button is to set the CS_NOCLOSE style for the form's window class. To do this from a WinForms application, you override the form's CreateParams property and add the SC_NOCLOSE flag using the | operator, e.g.:

protected override CreateParams CreateParams
{
    get
    {
        const int CS_NOCLOSE = 0x200;
        CreateParams cp = base.CreateParams;
        cp.ClassStyle = cp.ClassStyle | CS_NOCLOSE;
        return cp;
    }
}

这是一个永久的解决方案,不过,因为你不能在即时更新的窗口类样式。你将不得不销毁并重新创建窗口类。

This is a permanent solution, though, since you cannot update window class styles on-the-fly. You would have to destroy and recreate the window class.

不过,可以改为禁用系统菜单,这也同样会自动禁用的关闭按钮关闭命令。在标题栏中

However, you can instead disable the "Close" command in the system menu, which also also automatically disables the close button in the title bar.

internal static class NativeMethods
{
    public const int SC_CLOSE     = 0xF060;
    public const int MF_BYCOMMAND = 0;
    public const int MF_ENABLED   = 0;
    public const int MF_GRAYED    = 1;

    [DllImport("user32.dll")]
    public static extern IntPtr GetSystemMenu(IntPtr hWnd, bool revert);

    [DllImport("user32.dll")]
    public static extern int EnableMenuItem(IntPtr hMenu, int IDEnableItem, int enable);
}

public class MyForm : Form
{

    // ...

    // If "enable" is true, the close button will be enabled (the default state).
    // If "enable" is false, the Close button will be disabled.
    bool SetCloseButton(bool enable)
    {
        IntPtr hMenu = NativeMethods.GetSystemMenu(this.Handle, false);
        if (hMenu != IntPtr.Zero)
        {
            NativeMethods.EnableMenuItem(hMenu,
                                         SC_CLOSE,
                                         NativeMethods.MF_BYCOMMAND | (enable ? NativeMethods.MF_ENABLED : NativeMethods.MF_GRAYED));
        }
    }
}

请注意,这确实是一个瞬态操作。如果你这样做的任何的导致系统菜单由框架进行修改(比如最大化或最小化的形式),你的修改将被冲掉。更多细节见的。这通常是一个问题,为什么你宁愿使用第一个解决方案。但在这种情况下,由于要动态禁用并重新启用,这也没什么大不了的。

Note that this really is a transient operation. If you do anything that causes the system menu to be modified by the framework (such as maximizing or minimizing the form), your modifications will be obliterated. More details are in my related answer here. This is normally a problem, and why you'd prefer to use the first solution. But in this case, since you want to dynamically disable and re-enable, it is no big deal.

最后,千万要留意的是,你在做什么建议背道而驰 。他们说,从本质上讲,用户希望看到一个关闭按钮,而且它的存在给了他们安全感,他们可以始终安全滚蛋的任何弹出的画面。因此,你不应该禁用它。它召唤出一个进度对话框作为例外,但它接着说,一个进度对话框应该始终有一个取消按钮,允许中止运行。在这种情况下,你可以简单地在标题栏中的关闭按钮调用这个取消按钮,没有必要将其禁用。

Finally, do be mindful of the fact that what you're proposing runs counter to the Windows UI Guidelines for dialog boxes. They say, in essence, that users expect to see a close button and that its presence gives them a feeling of security that they can always safely "get out" of whatever popped up of the screen. Thus, you should not disable it. It does call out a progress dialog as an exception, but it goes on to say that a progress dialog should always have a "Cancel" button that allows aborting the operation. In that case, you can simply make the close button in the title bar invoke this "Cancel" button—no need to disable it.

这篇关于暂时禁用关闭按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-16 04:26