C#快捷键下划线没有内置应用程序中显示下划线、快捷键、应用程序

2023-09-03 01:42:04 作者:优雅的

我有一个小问题,与.net 4.0 ToolStripMenuItem标题。 我想它强调的快捷方式(访问)在项目文本键字母。 我用在项目文本字段中的符号标志:'和;新的地图,它看起来很好的编辑器:

I have a small problem with .Net 4.0 ToolStripMenuItem caption. I want it to underscore the Shortcut (access) key letter in the item text. I used the ampersand sign in the item text field: '&New map', and it looks fine in the editor:

但是,当我构建应用程序,下划线消失:

But, when I build the application, the underscores disappear:

有谁知道它为什么会发生,以及如何使下划线显示,在建筑形式?

Does anyone know why does it happen and how to make the underscored display in the built form?

推荐答案

正如在其他的答案,这个默认的行为。只有在 ALT 加速键被显示为pressed。

As mentioned in other answers, this the default behaviour. Accelerators are being shown only after the ALT key is pressed.

不过似乎可以强制Windows显示快捷键不断:

However it seems possible to force Windows to display accelerator keys constantly:

[System.Runtime.InteropServices.DllImport("user32.dll")]
private static extern int SystemParametersInfo(int uAction, int uParam, int lpvParam, int fuWinIni);

private const int SPI_SETKEYBOARDCUES = 4107; //100B
private const int SPIF_SENDWININICHANGE = 2;

[STAThread]
static void Main()
{
    // always show accelerator underlines
    SystemParametersInfo(SPI_SETKEYBOARDCUES, 0, 1, 0);

    Application.Run(new MainForm());
}

这里。

正如我刚才验证(在评论 ken2k 的建议后),这个不幸的是会影响整个系统。因此,它需要一些调整: 1)记住 SPI_SETKEYBOARDCUES 对启动电流值 2)重置设置这个值退出, 3)创建域异常处理程序,以确保设置总是被重置。

As I've just verified (after ken2k's suggestion in the comments), this unfortunately affects the whole system. So it needs some tweaking: 1) remember current value of SPI_SETKEYBOARDCUES on startup 2) reset the setting to this value on exit, 3) create a domain exception handler, to be sure that the setting always gets reset back.

不幸的是这种行为这样即使最后一个参数是零,即使的文档说:

Unfortunately this behaves this way even if the last parameter is zero, even though documentation says:

此参数可以是零,如果你不想要更新的用户配置文件或广播WM_SETTINGCHANGE消息

This parameter can be zero if you do not want to update the user profile or broadcast the WM_SETTINGCHANGE message

简单版本,当然只是:

[System.Runtime.InteropServices.DllImport("user32.dll")]
private static extern int SystemParametersInfo(int uAction, int uParam, int lpvParam, int fuWinIni);

private const int SPI_SETKEYBOARDCUES = 4107; //100B
private const int SPIF_SENDWININICHANGE = 2;

[STAThread]
static void Main()
{
    // always show accelerator underlines
    SystemParametersInfo(SPI_SETKEYBOARDCUES, 0, 1, 0);

    Application.Run(new MainForm());

    SystemParametersInfo(SPI_SETKEYBOARDCUES, 0, 0, 0);
}

修改:在这个答案你可以找到如何在code为例要做到这一点只在本地为您的应用程序。

EDIT: In this answer you can find a code example on how to achieve this locally only for your application.