[转]关于c#winform禁用关闭按钮的方法

 

Code:
  1. 窗口样式中的ControlBox选为False就可以去掉右上角的叉    
  2.   
  3. 下面是一个简单的例子,调用API实现了禁用关闭按钮的功能    
  4.   
  5. using System;    
  6. using System.Collections.Generic;    
  7. using System.ComponentModel;    
  8. using System.Data;    
  9. using System.Drawing;    
  10. using System.Text;    
  11. using System.Windows.Forms;    
  12. using System.Runtime.InteropServices;    
  13.   
  14. namespace winFormTest    
  15. {    
  16. public partial class Form2 : Form    
  17. {    
  18. [DllImport("USER32.DLL")]    
  19. public static extern int GetSystemMenu(int hwnd, int bRevert);    
  20. [DllImport("USER32.DLL")]    
  21. public static extern int RemoveMenu(int hMenu, int nPosition, int wFlags);    
  22.   
  23. const int MF_REMOVE = 0x1000;    
  24.   
  25. const int SC_RESTORE = 0xF120; //还原    
  26. const int SC_MOVE = 0xF010; //移动    
  27. const int SC_SIZE = 0xF000; //大小    
  28. const int SC_MINIMIZE = 0xF020; //最小化    
  29. const int SC_MAXIMIZE = 0xF030; //最大化    
  30. const int SC_CLOSE = 0xF060; //关闭    
  31.   
  32. public Form2()    
  33. {    
  34. InitializeComponent();    
  35. }    
  36.   
  37. private void Form2_Load(object sender, EventArgs e)    
  38. {    
  39. int hMenu;    
  40. hMenu = GetSystemMenu(this.Handle.ToInt32(), 0);    
  41. RemoveMenu(hMenu, SC_CLOSE, MF_REMOVE);    
  42. }    
  43.   
  44. }    
  45. }   
  46.   
  47. //来自http://hi.baidu.com/flydragon1125/blog/item/946158ed4381c6d1b21cb1a9.html   
  48.   
  49. [DllImport("user32.dll")]   
  50. static extern IntPtr GetSystemMenu(IntPtr hwnd, bool bRevert);   
  51. [DllImport("user32.dll")]   
  52. static extern bool DeleteMenu(IntPtr hMenu, uint uPosition, uint uFlags);   
  53. [DllImport("user32.dll")]   
  54. static extern bool EnableMenuItem(IntPtr hMenu, uint uIDEnableItem, uint uEnable);   
  55.   
  56. const uint SC_MOVE = 0xF010; //移动   
  57. const uint SC_CLOSE = 0xF060;//关闭   
  58.   
  59. const uint MF_BYCOMMAND = 0x00; //按命令方式   
  60. const uint MF_GRAYED = 0x01;    //灰掉   
  61. const uint MF_DISABLED = 0x02;  //不可用   
  62.   
  63. private void Form1_Load(object sender, EventArgs e)   
  64. {   
  65.   
  66.     IntPtr hMenu = GetSystemMenu(this.Handle, false); //获取程序窗体的句柄   
  67.   
  68.     if (hMenu != IntPtr.Zero)   
  69.     {   
  70.         DeleteMenu(hMenu, SC_MOVE, MF_BYCOMMAND); //删除移动菜单,禁用移动功能   
  71.         EnableMenuItem(hMenu, SC_CLOSE, MF_BYCOMMAND | MF_GRAYED | MF_DISABLED); //禁用关闭功能   
  72.     }   
  73. }   
  74. //来自http://hi.baidu.com/stevenlimin/blog/item/296092af0cbe1cc57cd92aa7.html   

在C#中的Form属性没有禁用关闭按钮的属性了。但我们可能通知设置构造参数来进行禁用,方法如下:

using  System;
using  System.Collections.Generic;
using  System.ComponentModel;
using  System.Data;
using  System.Drawing;
using  System.Text;
using  System.Windows.Forms;

namespace  ECT
{
    
public partial class NoCloseForm : FormBase
    
{
        
/// <summary>
        
/// 获取已设置无法关闭窗口创建参数。就是这里
        
/// </summary>

        protected override CreateParams CreateParams
        
{
            
get
            
{
                
int CS_NOCLOSE = 0x200;
                CreateParams parameters 
= base.CreateParams;
                parameters.ClassStyle 
|= CS_NOCLOSE;

                
return parameters;
            }

        }



        
public NoCloseForm()
        
{
            InitializeComponent();
        }

    }

}

 //来自http://www.cnychao.cn/post/41.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值