为 Windows 应用程序启用热键( 快捷键)






1.54/5 (7投票s)
为 Windows 应用程序启用热键(
引言
在 .Net Windows 应用程序中,快捷键默认情况下是未启用的。
背景
通常,只有在按下 ALT 键后才会显示下划线,但你可以通过更改操作系统设置来启用它。在 Windows XP 中,右键单击 桌面 以调出 显示属性 对话框,然后选择 外观 选项卡,然后单击 效果 按钮并 取消选中 复选框“在按下 ALT 键之前隐藏键盘导航的下划线字母”。
但是如何以编程方式实现呢?下面的代码演示了如何操作!
使用代码
通常在 Windows 项目中,我们会有一个 General.cs 类,其中包含其他应用程序类使用的通用函数、常量、委托和全局变量。
将代码粘贴到 General.cs 中
[DllImport ("user32.dll")]
static extern void SystemParametersInfo(uint uiAction, uint uiParam, ref int pvParam,
uint fWinIni);
/* Constants used for User32 calls. */
const uint SPI_SETKEYBOARDCUES = 0x100B;
/// <summary>
/// Change the setting programmatically
/// for keyboard shortcut Issue
/// </summary>
private static void GetAltKeyFixed()
{
int pv = 1;
/* Call to systemparametersinfo to set true of pv variable.*/
SystemParametersInfo(SPI_SETKEYBOARDCUES, 0, ref pv, 0);
//Set pvParam to TRUE to always underline menu access keys,
}
请添加
using System.Runtime.InteropServices;
在 General.cs 中添加程序集
并在 Program.cs 中 Application.Run(new <要显示的窗体名称>);
之前调用 GetAltKeyFixed
。
或者在 Application.Run(new ......)
之前复制以下代码
General _General=new general();
_General.GetAltKeyFixed();
关注点
这个问题似乎存在于所有 .net Windows 应用程序中。
添加两行代码,将解决应用程序快捷键的下划线显示问题