Autohide MenuStrip - How to activate it when showing?

主宰稳场 提交于 2019-12-11 04:30:00

问题


I have a MenuStrip and I make it AutoHide using following code. It hides/shows prefect but when a control get focus, by pressing Alt key, MenuStrip shows but it is not active and there is not small underline under shortcut keys for example under 'F' for File , and pressing 'F' will not open it). How can I correctly active it?

Note: I used MenuDeactivate instead of it but it did not work prefect.

bool menuBarIsHide = true;
bool altKeyIsDown = false;
bool alwaysShowMenuBar=false;
//KeyPreview is true;
//for prevent glitch(open/close rapidly)
void Form1_KeyUp(object sender, KeyEventArgs e)
{
    if ((Control.ModifierKeys & Keys.Alt) != 0)
        altKeyIsDown = false;
}
void Form1_KeyDown(object sender, KeyEventArgs e)
{
    if ((Control.ModifierKeys & Keys.Alt) != 0)
    {
        if (altKeyIsDown)
            return;
        if (!alwaysShowMenuBar)
        {
            if (menuBarIsHide)
            {
                menuBar.Show();
                menuBarIsHide = false;
                //manage container height
            }
            else
            {
                menuBar.Hide();
                menuBarIsHide = true;
                //manage container height
            }
        }
    }
}

回答1:


You can override ProcessCmdKey to handle Alt key to toggle the menu visibility. Also to activate menu, call internal OnMenuKey method of MenuStrip. Also handle MenuDeactivate to make the menu invisible after finishing your work with menu, but you need to make the menu invisible using BeginInvoke.

protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
    if (keyData == (Keys.Alt | Keys.Menu))
    {
        if (!this.menuStrip1.Visible)
        {
            this.menuStrip1.Visible = true;
            var OnMenuKey = menuStrip1.GetType().GetMethod("OnMenuKey", 
                System.Reflection.BindingFlags.NonPublic | 
                System.Reflection.BindingFlags.Instance);
            OnMenuKey.Invoke(this.menuStrip1, null);
        }
        else
        {
            this.menuStrip1.Visible = false;
        }
        return true;
    }
    return base.ProcessCmdKey(ref msg, keyData);
}
private void menuStrip1_MenuDeactivate(object sender, EventArgs e)
{
    this.BeginInvoke(new Action(() => { this.menuStrip1.Visible = false; }));
}


来源:https://stackoverflow.com/questions/42827906/autohide-menustrip-how-to-activate-it-when-showing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!