Link to home
Start Free TrialLog in
Avatar of asukhu
asukhu

asked on

C# windows Form Application menuitem Hover

Using Microsoft.net 2008 , Framework 3.5:

Is there a way to remove the annoying blue highlight that appears on a menuitem when the mouse moves over it?

Is there a way to change the blue highlight to another color?

is there a way to have a menuitem disable without it being grayed out?

Thanks,
Avatar of chrisbray
chrisbray
Flag of United Kingdom of Great Britain and Northern Ireland image

Hi asukhu

I am not sure you have thought through the proposed changes carefully enough...

The highlight is there for a reason - it indicates which menu item the mouse is currently over and thereby prevents accidental clicking of the wrong menu item.  You should therefore not remove it is it is likely to lead to support problems with your users!

You can disable a menu item without graying it out, but how will the user know that it is disabled?  This is likely to lead to users trying to click a disabled menu item and thinking your program is broken, or not clicking an important item because they think it does not work.

However, if you really want to do this you can do it by simply removing and replacing the handler:

// Remove the handler
 this.newToolStripMenuItem.Click -= new System.EventHandler(this.newToolStripMenuItem_Click);

// Put the handler back
 this.newToolStripMenuItem.Click += new System.EventHandler(this.newToolStripMenuItem_Click);

In addition to the obvious limitations to your application you are proposing, if you implement them the application will no longer meet the Windows standards and will therefore be seen as of less value than compliant applications - and because it does not work the way users would expect it will be much more difficult for them to work with.  I recommend you rethink the proposal.

Chris Bray
Avatar of asukhu
asukhu

ASKER

I am not trying to use a menu bar as a actual "menu bar" I am using it to display data and information.

I tried removing the handler but that doesn't work:

// Remove the handler
this.newToolStripMenuItem.Click -= new System.EventHandler(this.newToolStripMenuItem_Click);

// Put the handler back
this.newToolStripMenuItem.Click += new System.EventHandler(this.newToolStripMenuItem_Click);

how can we disable a button but let it show that it is enabled?
Hi asukhu:

> I am not trying to use a menu bar as a actual "menu bar" I am using it to display data and > information.

Then in your shoes I would rethink the delivery method.  A menu is designed to have a specific function and if you are not using it as a menu it is a bit like using a car as a cupboard - it will work, but is a pointless and expensive waste of the extra features that are not needed.

> I tried removing the handler but that doesn't work:

If you have actually removed the handler it cannot do anything.  I therefore suspect an error in your code - think of it logically, and you will realise that if there is no handler nothing can happen when you click a menu item!

Chris Bray  
ASKER CERTIFIED SOLUTION
Avatar of chrisbray
chrisbray
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of asukhu

ASKER

To chrisbray:

I tried your code. But it still doesn't do what I want: When I mouseover the menuitem, the item still highlights. What I want to do is remove that highlight? It there a way to some how override the highlight or totally disable it for this menuitem 'testToolStripMenuItem'?
Hi asukhu:

I was answering the third part of your original question, namely:

> is there a way to have a menuitem disable without it being grayed out?

The code I provided does exactly that.

In order to produce the other effects that you seem to want, I can only suggest that you draw your own OwnerDraw menu.  This basically involves overriding the MeasureItem and DrawItem events on the menu.

Examples, Tutorials, Discussions on this topic:

http://www.csharphelp.com/archives/archive282.html
http://www.java2s.com/Code/CSharp/GUI-Windows-Form/OwnerDrawnMenu.htm
http://www.expresscomputeronline.com/20040119/techspace02.shtml

Chris Bray

You may also wixh to review the MSDN entry on OwnerDraw menus:

http://msdn.microsoft.com/en-us/library/system.windows.forms.menuitem.ownerdraw.aspx

Chris Bray
Avatar of asukhu

ASKER

ok, Thanks for the info. I am going to do some more investigation and will post my results.
Thanks ChrisBray.
Avatar of asukhu

ASKER

I wrote my own customized menuRender to handling the highlight effects:

noHighlights : Arraylist contains all the names of the Tool Strip MenuItems that I wanted no highlighting for.

OnRenderMenuItemBackground : void Handles the logic for  handling this effect.
In addition to your code, i had to add to edit Form1.cs:

public Form1()
{
myCustomRender = new CustomRenderer();
InitializeComponent();
menuStrip1.Renderer = myCustomRender;
}


// CustomizedMenuItem.cs
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Collections;
 
namespace WindowsFormsUtils
{
    public class CustomizedMenuRenderer : ToolStripRenderer
    {
        ArrayList noHighlights = new ArrayList();
 
        public void addDisableHighlights(string menuItemName)
        {
            noHighlights.Add(menuItemName);
        }
        public void removeDisableHighlights(string menuItemName)
        {
            noHighlights.Remove(menuItemName);
        }
 
 
        protected override void OnRenderToolStripBackground(
         ToolStripRenderEventArgs e)
        {
            LinearGradientBrush brush = new LinearGradientBrush(e.AffectedBounds,
             Color.White, Color.Gray, 90f);
            e.Graphics.FillRectangle(brush, e.AffectedBounds);
            brush.Dispose();
        }
 
        protected override void OnRenderMenuItemBackground(ToolStripItemRenderEventArgs e)
        {
            bool showHighlight = true;
 
            showHighlight = !noHighlights.Contains(e.Item.Name);
            
 
            if (e.Item.Selected && showHighlight)
            {
                {
                    Rectangle rect = e.Item.ContentRectangle;
                    rect.X = e.Item.Padding.Left;
                    rect.Y = e.Item.Padding.Top;
                    rect.X -= 2;
                    rect.Y -= 5;
                    rect.Width -= 14;
                    rect.Height += 8;
 
                    LinearGradientBrush brush = new LinearGradientBrush(e.Item.Bounds,
             Color.Transparent, Color.LightBlue, 90);
                    e.Graphics.FillRectangle(brush, rect);
 
                    Pen outline = new Pen(Color.FromArgb(128, 128, 255), 1);
                    e.Graphics.DrawRectangle(outline, rect);
                }
            }
        }
    }
}

Open in new window

Avatar of asukhu

ASKER

Thanks for the tip (to write my own custom class), that really helped.
Hi asukhu:

Glad to have been of help.  Your solution seems elegant enough and if it does what you want that can only be a good thing!

Chris Bray