惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

S
Secure Thoughts
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
S
Securelist
云风的 BLOG
云风的 BLOG
M
MIT News - Artificial intelligence
Engineering at Meta
Engineering at Meta
雷峰网
雷峰网
C
CERT Recently Published Vulnerability Notes
H
Hackread – Cybersecurity News, Data Breaches, AI and More
S
SegmentFault 最新的问题
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
K
Kaspersky official blog
WordPress大学
WordPress大学
I
Intezer
L
Lohrmann on Cybersecurity
V
Vulnerabilities – Threatpost
C
Check Point Blog
A
About on SuperTechFans
AWS News Blog
AWS News Blog
Latest news
Latest news
宝玉的分享
宝玉的分享
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
小众软件
小众软件
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
C
Cyber Attacks, Cyber Crime and Cyber Security
SecWiki News
SecWiki News
Recorded Future
Recorded Future
Last Week in AI
Last Week in AI
D
Darknet – Hacking Tools, Hacker News & Cyber Security
The Register - Security
The Register - Security
A
Arctic Wolf
V2EX - 技术
V2EX - 技术
Hacker News: Ask HN
Hacker News: Ask HN
V
V2EX
Scott Helme
Scott Helme
I
InfoQ
Project Zero
Project Zero
Security Archives - TechRepublic
Security Archives - TechRepublic
Recent Announcements
Recent Announcements
Spread Privacy
Spread Privacy
Attack and Defense Labs
Attack and Defense Labs
大猫的无限游戏
大猫的无限游戏
Webroot Blog
Webroot Blog
N
News and Events Feed by Topic
博客园 - 司徒正美
Microsoft Security Blog
Microsoft Security Blog
The GitHub Blog
The GitHub Blog
NISL@THU
NISL@THU
L
LangChain Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com

博客园 - wangbin

Provider 错误 '80004005' 未指定的错误 的最终解决方法 C#(WinForm)实现软件注册 设置字段默认值sql语句 .net WINFORM 界面怎么做凹凸效果的分割线?就是横线 游标Cursor 使用小例 (SQLServer) SQL Server 2005 数据类型和.Net数据类型的对应关系 我改行了 PHP $_FILES详解 - wangbin - 博客园 php $GLOBALS 超全局变量的理解 [转载]php-数组操作foreach、each、reset、list .net 随机数 C#混淆 xenocode使用说明 [原创]一个简单的药店用的会员积分管理系统 [原创]xml序列化 [原创]c# as用法 [原创]闲来无事,写了个c#的数据库附加工具,现附上源代码 一个能够在线创建flash网页的站点 [原创]我的cms项目 简洁、标准的对联广告代码
winform屏蔽Alt+F4组合键以防止用户关闭对话框
wangbin · 2012-01-14 · via 博客园 - wangbin

winform屏蔽Alt+F4组合键以防止用户关闭对话框,屏蔽Alt + F4的方法有很多,这里列出了一个比较简单而奏效的方法。

1. 捕获窗体的KeyDown事件

  给窗体添加KeyDown事件,然后加入下面的代码:

1 private void Form2_KeyDown(object sender, KeyEventArgs e)
2 {
3     if (e.KeyCode == Keys.F4 && e.Modifiers == Keys.Alt)
4     {
5         e.Handled = true;
6     }
7 }

  代码中判断当前用户是否按下了Alt + F4组合键,如果是则执行e.Handled = true用于指示事件已经被成功执行,从而屏蔽了系统的Alt + F4功能。

2. 改写窗体的Closing事件

  给窗体添加Closing事件,然后加入下面的代码:

1 public void Form2_FormClosing(object sender, FormClosingEventArgs e)
2 {
3     e.Cancel = true;
4 }

  当窗体被关闭时(用户点击窗体右上角的关闭按钮或通过程序调用窗体的Close()方法)关闭动作会被取消,那么如何才能通过程序来关闭窗体呢?可以在关闭窗体的代码前去掉窗体的Closing事件,如:

1 this.FormClosing -= new System.Windows.Forms.FormClosingEventHandler(this.Form1_FormClosing);

2 this.Close();

  当然,你也可以选择不关闭窗体而只是隐藏它,这样该窗体不会在内存中被注销。上面两行代码只能在窗体内部运行,如果你想实现在父窗体中通过程序关闭子窗体,可以在子窗体中添加一个public类型的方法,将上面两行代码加入到方法中,然后在父窗体中调用子窗体的这个方法。如:

主窗体:

1 Form2 frm = new Form2();
2 frm.CloseWindow();

子窗体:

 1 private void Form2_FormClosing(object sender, FormClosingEventArgs e)
 2 {
 3     e.Cancel = true;
 4 }
 5
 6 public void CloseWindow()
 7 {
 8     this.FormClosing -= new FormClosingEventHandler(this.Form2_FormClosing);
 9     this.Close();
10 }

3. 调用COM组建或全局钩子

  这类方法需要借助于第三方的代码来实现,过程稍微复杂一些,不太推荐使用

本文来自: IT知道网(http://www.itwis.com/) 详细出处参考:http://www.itwis.com/html/net/winform/20110518/10311.html