您的位置:首页 > 编程语言 > C#

c# 禁用Excel窗口关闭按钮

2009-09-22 23:03 337 查看
先是声明API的类:

using System;

using System.Runtime.InteropServices;

class Win32
{
    /// <summary>
    /// 查找窗口句柄
    /// </summary>
    /// <param name="lpClassName">窗口类名</param>
    /// <param name="lpWindowName">窗口标题</param>
    /// <returns></returns>
    [DllImport("user32")]
    public static extern int FindWindow(string lpClassName, string lpWindowName);

    [DllImport("user32")]
    public static extern int GetSystemMenu(int hwnd, int bRevert);

    [DllImport("user32")]
    public static extern int RemoveMenu(int hMenu, int nPosition, int wFlags);

}




然后是窗体部分,比较简单:

private void button1_Click(object sender, EventArgs e)
        {
            if (button1.Text == "禁用关闭")
            {
                DisableClose();
                MessageBox.Show("Excel关闭已禁用");
                button1.Text = "已禁用";
            }
        }

        public const int MF_BYPOSITION = 0x400;

        protected void DisableClose()
        {
            int excel = Win32.FindWindow("XLMAIN", null);//Excel2003窗体类

            int close = Win32.GetSystemMenu(excel,0);

            Win32.RemoveMenu(close, 6, MF_BYPOSITION);
        }




暂时只支持Excel2003,
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: