How can I prevent a disabled ToolStripMenuItem from showing a border on mouse over?

眉间皱痕 提交于 2019-12-10 13:39:39

问题


Yes, exactly same as the title, in my project some of toolstripmenuitems were disabled. But when i bring the cursor over the menu item, a blue border is appearing like this:


But i don't want this. I want it like this:


Could you help me, how do i prevent this blue border?

回答1:


You should create a custom ToolStripRenderer, take a look at this - How to: Set the ToolStrip Renderer for an Application

  1. You have to create a custom renderer like this:

    class CutomToolStripMenuRenderer : ToolStripProfessionalRenderer
    {
        protected override void OnRenderMenuItemBackground(ToolStripItemRenderEventArgs e)
        {
            if (e.Item.Enabled)
            base.OnRenderMenuItemBackground(e);
        }
    
        protected override void OnRenderButtonBackground(ToolStripItemRenderEventArgs e)
        {
            if (e.Item.Enabled)
            base.OnRenderMenuItemBackground(e);
        }
    } 
    
  2. And then set this renderer to your menu strip:

    menuStrip1.Renderer = new CustomToolStripRenderer();
    



回答2:


I get a little more control of my "Disabled" menuItems by Switching between ToolStripMenuItem and a copy of the item as a ToolStripLabel. Then instead of setting Enabled = false, I just toggle the visibility between the two. This way I can have custom forecolors for the "Disabled" menuItem (Label).

    bool m_isStartSvcEnabled = true;


    void ToggleServiceEnabled()
    {
        m_isStartSvcEnabled = !m_isStartSvcEnabled;

        mnuStartSvc.Visible = m_isStartSvcEnabled;
        lblStartSvc.Visible = !m_isStartSvcEnabled;

        mnuStopSvc.Visible = !m_isStartSvcEnabled;
        lblStopSvc.Visible = m_isStartSvcEnabled;
    }

(White menu forecolor, and and lighter gray forecolor for the label I defined in the creation of the items.)



来源:https://stackoverflow.com/questions/13364923/how-can-i-prevent-a-disabled-toolstripmenuitem-from-showing-a-border-on-mouse-ov

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