如何处理接近(大X)按钮在源?如何处理、按钮

2023-09-03 21:00:26 作者:煙

我们想在某些情况下阻止关闭按钮的标题栏的动作。问题是,这是一个MDI应用程序,它看起来我们将不得不增加code在每个表格取消操作,在关闭事件处理程序。看来,子窗体是第一个接收事件。有没有办法来增加code在一个地方取消关闭操作。在接近事件是如何传播到子窗体的信息将受到欢迎。有没有做什么,我们想要做一个简单的方法?

We would like under some circumstances to block the action of the close button in the title bar. The problem is that it's a MDI applications and it seems that we will have to add code in each forms to cancel the operation in the Closingevent handler. It seems that the child forms are the first to receive the event. There is no way to add code at a single place to cancel the close operation. Information on how the close event is propagated to the child form would be welcome. Is there a simple way of doing what we want to do?

推荐答案

Windows提供了一种方法可以做到这一点,你可以用系统菜单鼓捣。提供了很好的反馈,以及,关闭按钮实际上会照看残疾。您可以在系统菜单中禁用SC_CLOSE命令。最好的证明了样表,获得通过删除就可以了按钮启动:

Windows provides a way to do this, you can tinker with the system menu. Provides for nice feedback as well, the close button will actually look disabled. You can disable the SC_CLOSE command in the system menu. Best demonstrated with a sample form, get started by dropping a button on it:

using System.Runtime.InteropServices;
...

public partial class Form1 : Form {
    public Form1() {
        InitializeComponent();
        button1.Click += new EventHandler(button1_Click);
    }

    private bool mCloseEnabled = true;
    public bool CloseEnabled {
        get { return mCloseEnabled; }
        set {
            if (value == mCloseEnabled) return;
            mCloseEnabled = value;
            setSystemMenu();
        }
    }

    protected override void OnHandleCreated(EventArgs e) {
        base.OnHandleCreated(e);
        setSystemMenu();
    }
    private void setSystemMenu() {
        IntPtr menu = GetSystemMenu(this.Handle, false);
        EnableMenuItem(menu, SC_CLOSE, mCloseEnabled ? 0 : 1);
    }
    private const int SC_CLOSE = 0xf060;
    [DllImport("user32.dll")]
    private static extern IntPtr GetSystemMenu(IntPtr hWnd, bool revert);
    [DllImport("user32.dll")]
    private static extern int EnableMenuItem(IntPtr hMenu, int IDEnableItem, int wEnable);

    private void button1_Click(object sender, EventArgs e) {
        CloseEnabled = !CloseEnabled;
    }
}