Go homepage(回首页) Upload pictures (上传图片) Write articles (发文字帖)
The author:(作者)qqpublished in(发表于) 2014/7/11 9:23:20 C#教程:使用键盘热键实例
使用键盘热键编写应用程序时,经常会遇到使用键盘热键的情况。使用键盘热键不仅方便操作软件,而且能够大大地提高工作效率。热键通常在控件的text属性中设置,下面以设置Button控件的热键为例,详细介绍设置热键的步骤。具体操作步骤如下。(1)在窗体中添加一个Button控件,在该控件的属性窗口中找到Text属性,并输入要显示的文本及热键。输入热键的方法是将输入法切换到英文状态下,输入左右小括号“()”,然后在括号内输入组合字符“&T”,运行效果如图1所示。图1 热键设置(2)当程序运行时,想要激活热键,按下组合键即可。示例热键的使用本示例中,按下热键,弹出消息对话框,示例运行结果如图2所示。图2 热键效果private void button1_Click(object sender, EventArgs e){MessageBox.Show("快捷方便的热键");}完整程序代码如下:★ ★★★★frmHot.cs窗体代码文件完整程序代码★★★★★using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Text;using System.Windows.Forms;namespace _2_10{public partial class frmHot : Form{public frmHot(){InitializeComponent();}private void button1_Click(object sender, EventArgs e){MessageBox.Show("快捷方便的热键");}private void frmHot_Load(object sender, EventArgs e){}}}★ ★★★★frmHot.designer.cs窗体设计文件完整程序代码★★★★★namespace _2_10{partial class frmHot{/// /// 必需的设计器变量。/// private System.ComponentModel.IContainer components = null;/// /// 清理所有正在使用的资源。/// /// 如果应释放托管资源,为 true;否则为 false。protected override void Dispose(bool disposing){if (disposing && (components != null)){components.Dispose();}base.Dispose(disposing);}#region Windows 窗体设计器生成的代码/// /// 设计器支持所需的方法 - 不要/// 使用代码编辑器修改此方法的内容。/// private void InitializeComponent(){this.button1 = new System.Windows.Forms.Button();this.SuspendLayout();//// button1//this.button1.Location = new System.Drawing.Point(92, 45);this.button1.Name = "button1";this.button1.Size = new System.Drawing.Size(75, 23);this.button1.TabIndex = 0;this.button1.Text = "热键(&T)";this.button1.UseVisualStyleBackColor = true;this.button1.Click += new System.EventHandler(this.button1_Click);//// frmHot//this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;this.ClientSize = new System.Drawing.Size(292, 132);this.Controls.Add(this.button1);this.Name = "frmHot";this.Text = "热键";this.Load += new System.EventHandler(this.frmHot_Load);this.ResumeLayout(false);}#endregionprivate System.Windows.Forms.Button button1;}}★ ★★★★Program.cs主程序文件完整程序代码★★★★★using System;using System.Collections.Generic;using System.Windows.Forms;namespace _2_10{static class Program{/// /// 应用程序的主入口点。/// [STAThread]static void Main(){Application.EnableVisualStyles();Application.SetCompatibleTextRenderingDefault(false);Application.Run(new frmHot());}}}
赞