Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

Is is possible to make the underlining hotkey always visible on my controls without having to press the Alt key in a Windows form with Visual Studio - C#?

I only have a certain time when I need the controls of the form to be always underlined with the "_" under a character. So it would be good to have just the code to do it.

I could have the setting for Windows to always show underlining of shortcuts and hotkeys, but I only need it to happen a certain time.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
661 views
Welcome To Ask or Share your Answers For Others

1 Answer

Assuming you are using WinForms, you should be able to rely on the underlying Win32 mechanism. And that is the WM_UPDATEUISTATE message. The documentation says:

An application sends the WM_UPDATEUISTATE message to change the UI state for the specified window and all its child windows.

So you can send the message to the handle of the top-level window. You need to pass UIS_CLEAR for the low-order word of wParam and UISF_HIDEACCEL for the high-order word of wParam.

Here is some rather crude sample code. Bear in mind that my C# expertise is very limited.

public partial class Form1 : Form
{
    private const uint WM_UPDATEUISTATE = 0x0128;
    private const uint WM_QUERYUISTATE = 0x0129;
    private const uint UIS_CLEAR = 2;
    private const uint UISF_HIDEACCEL = 0x2;

    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_HandleCreated(object sender, PaintEventArgs e)
    {
        ClearHideAccel();
    }

    private void ClearHideAccel()
    {
        UIntPtr wParam = (UIntPtr)((UISF_HIDEACCEL << 16) | UIS_CLEAR);
        NativeMethods.SendMessage(this.Handle, WM_UPDATEUISTATE, wParam, IntPtr.Zero);
    }
}

internal class NativeMethods
{
    [DllImport("User32", SetLastError = true)]
    public static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, UIntPtr wParam, IntPtr lParam);
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...