为什么最大化/最小化事件造成的关闭按钮重新启用禁用后? [英] Why are Maximize/Minimize events causing the Close Button to be re-enabled after disabling it?

查看:210
本文介绍了为什么最大化/最小化事件造成的关闭按钮重新启用禁用后?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用P / Invoke来调用GetSystemMenu和EnableMenuItem(WIN32API)禁用关闭功能。然而,最小化或最大化后,我的Windows窗体应用程序重新启用按钮。

显然,最小化或最大化导致出现该问题,但如何?我不知道去哪里找,以prevent这种行为。

我应该是preventing的最大化和最小化的行为或者是有什么特别不妥其中IP /调用调用的方法是什么?一旦应用程序(主要形式)已加载,我称之为从一个按钮,点击静态方法。

 类的PInvoke
{
    //的P / Invoke签名
    [的DllImport(user32.dll中)
    静态外部的IntPtr GetSystemMenu(IntPtr的的HWND,布尔bR​​evert);
    [的DllImport(user32.dll中)
    静态外部布尔EnableMenuItem(IntPtr的HMENU,UINT uIDEnableItem,UINT uEnable);

    // SysCommand(WM_SYSCOMMAND)不变
    内部常量UInt32的SC_CLOSE = 0xF060;

    使用添加/检查/ EnableMenuItem使用//常量
    内部常量UInt32的MF_BYCOMMAND = 00000000;
    内部常量UInt32的MF_ENABLED = 00000000;
    内部常量UInt32的MF_GRAYED = 00000001;
    内部常量UInt32的MF_DISABLED = 0x00000002;

    ///<总结>
    ///设置关闭(X)按钮和系统菜单关闭功能的状态。
    ///< /总结>
    ///< PARAM NAME =窗口>窗口或表格< /参数>
    ///< PARAM NAME =bEnabled>启用状态< /参数>
    公共静态无效EnableCloseButton(IWin32Window窗口,布尔bEnabled)
    {
        IntPtr的hSystemMenu = GetSystemMenu(window.Handle,假);

        EnableMenuItem(hSystemMenu,SC_CLOSE,MF_BYCOMMAND |(bEnabled MF_ENABLED:MF_GRAYED));
    }
}
 

解决方案

每个窗口都有一个窗口类,它定义样式该类的所有窗口。您可以使用 CS_NOCLOSE 类样式删除关闭按钮该类的窗口。请参见这里和的此处详细说明如何设置这个级别的标记。

如果这不会给你想要的东西,我不会禁用最小化/最大化的缘故可用性,但你可以听最小化/ maximimize事件,并重新运行code禁用关闭按钮。最后,它能够处理的关闭事件,并且根本就没有关闭。那么你知道你的窗口肯定不会被关闭,即使不无意中成为启用关闭按钮。

I have used P/Invoke to call GetSystemMenu and EnableMenuItem (win32api) to disable the close functionality. However, after minimizing or maximizing my Windows Forms application the button is re-enabled.

Obviously minimizing or maximizing is causing the behavior, but how? I'm not sure where to look to prevent this behavior.

Should I be preventing the maximize and minimize behavior or is there something particularly wrong with the way in which I P/Invoked the calls? Once the application (main form) has loaded, I call the static method from a button click.

class PInvoke
{
    // P/Invoke signatures
    [DllImport("user32.dll")]
    static extern IntPtr GetSystemMenu(IntPtr hWnd, bool bRevert);
    [DllImport("user32.dll")]
    static extern bool EnableMenuItem(IntPtr hMenu, uint uIDEnableItem, uint uEnable);

    // SysCommand (WM_SYSCOMMAND) constant
    internal const UInt32 SC_CLOSE = 0xF060;

    // Constants used with Add/Check/EnableMenuItem
    internal const UInt32 MF_BYCOMMAND = 0x00000000;
    internal const UInt32 MF_ENABLED = 0x00000000;
    internal const UInt32 MF_GRAYED = 0x00000001;
    internal const UInt32 MF_DISABLED = 0x00000002;

    /// <summary>
    /// Sets the state of the Close (X) button and the System Menu close functionality.
    /// </summary>
    /// <param name="window">Window or Form</param>
    /// <param name="bEnabled">Enabled state</param>
    public static void EnableCloseButton(IWin32Window window, bool bEnabled)
    {
        IntPtr hSystemMenu = GetSystemMenu(window.Handle, false);

        EnableMenuItem(hSystemMenu, SC_CLOSE, MF_BYCOMMAND | (bEnabled ? MF_ENABLED : MF_GRAYED));
    }
}

解决方案

Each window has a window class, which defines styles for all windows of that class. You can use CS_NOCLOSE class style to remove the close button for windows of that class. See here and here for details how to set this class flag.

If this doesn't give you what you want, I wouldn't disable minimize/maximize for sake of usability, but you could listen for minimize/maximimize events and re-run the code to disable the close button. Finally, it is possible to handle the close event, and simply not close. Then you know your window will definitely not be closed, even if the close button does inadvertently become enabled.

这篇关于为什么最大化/最小化事件造成的关闭按钮重新启用禁用后?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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