自动隐藏菜单条 [英] Auto-hiding MenuStrip

查看:62
本文介绍了自动隐藏菜单条的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理一个 Windows 窗体项目,我最近添加了代码以保持 MenuStrip 默认隐藏,除非用户按下 Alt 键,这与 Windows 的最新版本的视觉实践保持一致.

我要做的是通过启用 MenuStrip 以在特定时间范围内未检测到活动时自动隐藏来完成此添加.事不宜迟,这是我目前起草的代码:

使用系统;使用 System.Collections.Generic;使用 System.ComponentModel;使用 System.Data;使用 System.Drawing;使用 System.Linq;使用 System.Text;使用 System.Threading.Tasks;使用 System.Windows.Forms;命名空间 SomeWinformProject {公共部分类 MainForm : Form {/* 建造 */公共主窗体(){初始化组件();this.KeyDown += new KeyEventHandler(this.MainForm_KeyDown);this.KeyPreview = true;this.menuStrip.Visible = false;}/* Alt键事件处理程序*/private void MainForm_KeyDown(对象发送者,KeyEventArgs e){/* 如果按下 Alt 键并且当前没有 menuStrip* 可见,取消隐藏 */if (e.Alt && !(this.menuStrip.Visible))this.menuStrip.Visible = true;}}}

以下是我想自己解决这个问题的方法:

  1. 创建一个公共布尔值,称为 menuStripActivity 的效果,并在 MainForm_KeyDown() 中将其设置为 false.然后,我想以 5 秒左右的间隔创建一个计时器实例,并将 MouseClick 事件附加到 menuStrip.如果发生 MouseClick 事件,则 menuStripActivity 将设置为 true,并且定时器中断将选择不执行任何操作,而不是隐藏 MenuStrip.
  2. 我意识到上面存在的问题是它没有考虑到用户正在做某事或导航menuStrip的可能性代码>选项.我在 MSDN 文档中读到 MouseHover 事件处理程序也存在,在这种情况下,我可以在发生计时器跳闸时对两个处理程序的结果进行 OR 运算.

这两个的问题是我对 C# 和整个 .NET 生态系统相当陌生,所以我不太清楚什么是正确的,什么是笨拙的.我想避免我的代码从一开始就变得混乱,并希望在这种情况下在最佳实践"方面犯错.

谁能帮我指出正确的方向(或向我展示我犯的错误)?

解决方案

您可以收听 MenuStrip.MenuDeactivate 事件:

public MainForm () {初始化组件();this.KeyPreview = true;this.KeyDown += new KeyEventHandler(this.MainForm_KeyDown);this.menuStrip.MenuDeactivate += (s, e) =>this.menuStrip.Visible = false;this.menuStrip.Visible = false;}

<块引用>

备注
当由 ALT 键激活时,MenuStrip 或 ToolStrip 通常既不会从当前具有焦点的控件中获取焦点,也不会从该控件中移除焦点.如果在 MenuStrip 或 MenuStrip 的下拉列表中存在一个控件,则当用户按下 TAB 键时,该控件将获得焦点.通常,MenuStrip 的 GotFocus、LostFocus、Enter 和 Leave 事件在通过键盘激活时可能不会引发.在这种情况下,请改用 MenuActivate 和 MenuDeactivate 事件.

I'm working on a Windows Forms project, and I recently added code to keep the MenuStrip hidden by default unless the user presses the Alt key, in keeping with recent versions of Windows' visual practices.

What I'm looking to do is complete this addition by enabling the MenuStrip to auto-hide if activity is not detected in a certain timeframe. Without further ado, here's the code I've drafted so far:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace SomeWinformProject {
    public partial class MainForm : Form {
        /* Construction */
        public MainForm () {
            InitializeComponent ();
            this.KeyDown += new KeyEventHandler(this.MainForm_KeyDown);
            this.KeyPreview = true;
            this.menuStrip.Visible = false;
        }

        /* Alt key event handler */
        private void MainForm_KeyDown (object sender, KeyEventArgs e) {
            /* if the Alt key is pressed and the menuStrip is not currently
             * visible, un-hide it */
             if (e.Alt && !(this.menuStrip.Visible))
                 this.menuStrip.Visible = true;
        }
    }
}

Here's what I've thought of doing to work around this problem myself:

  1. Create a public Boolean called something to the effect of menuStripActivity and have it set to false in MainForm_KeyDown(). Then, I was thinking of creating a timer instance on an interval of 5 seconds or so, and also attaching a MouseClick event to menuStrip. If the MouseClick event occurred, then menuStripActivity would be set to true, and the timer interrupt would elect to do nothing, rather than hiding the MenuStrip.
  2. The issue I've realized exists with the above is that it doesn't account for the possibility that the user is in the process of doing something, or navigating the menuStrip options. I read in the MSDN docs that a MouseHover event handler also exists, in which case I could OR the results of the two handlers when the timer trip occurs.

The issue with both of these is that I am fairly new to C# and the .NET ecosystem at large, so I don't have a good idea of what is proper and what is kludgy. I want to avoid my code being cluttered from the get-go and want to err on the side of "best practice" for this type of situation.

Can anyone help point me in the right direction (or show me errors that I'm making)?

解决方案

You can listen to the MenuStrip.MenuDeactivate event:

public MainForm () {
    InitializeComponent ();

    this.KeyPreview = true;
    this.KeyDown += new KeyEventHandler(this.MainForm_KeyDown);
    this.menuStrip.MenuDeactivate += (s, e) => this.menuStrip.Visible = false;

    this.menuStrip.Visible = false;
}

Remarks
When activated by the ALT key, the MenuStrip or ToolStrip typically neither take nor remove the focus from the control that currently has the focus. If there is a control hosted within the MenuStrip or a drop-down of the MenuStrip, the control gains focus when the user presses the TAB key. In general, the GotFocus, LostFocus, Enter, and Leave events of MenuStrip might not be raised when they are activated by the keyboard. In such cases, use the MenuActivate and MenuDeactivate events instead.

这篇关于自动隐藏菜单条的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆