views:

686

answers:

2

I have a C# form into which I've placed a left-docked MenuStrip. This MenuStrip contains some menu items which contain submenus, and some menu items which are effectively buttons (clicking on them results in an action taking place; n.b., I realize this is not a good design).

I would like to have the menu items which have menus associated with them draw the right-pointing arrow on the menu item, the same way a contextual menu does. I've subclassed ToolStripProfessionalRenderer and can call OnRenderArrow() at the appropriate time (e.g., within OnRenderItemText() or similar), but I don't seem to have a way to determine the correct location of the arrow.

So, two interrelated questions here:

  1. Is there a way to force the arrows to be drawn on top-level menu items?
  2. If not, is there a way to determine the proper location of the arrow on the menu item so that OnRenderArrow() can be called manually?

Thanks!

A: 

I was able to hack this together as a solution, but I'd still like something less obtuse:

protected override void OnRenderItemText(ToolStripItemTextRenderEventArgs e)
{
 base.OnRenderItemText(e);
 if (e.Item.GetType() == typeof(ToolStripMenuItem))
 {
  ToolStripMenuItem tsmi = (ToolStripMenuItem)e.Item;
  if (tsmi.HasDropDownItems && tsmi.OwnerItem == null)
  {
   Rectangle bounds = tsmi.Bounds;
   bounds.X = bounds.Right - 25;
   bounds.Width = 25;
   bounds.Y = 0;
   ToolStripArrowRenderEventArgs tsarea = new ToolStripArrowRenderEventArgs(
    e.Graphics,
    e.Item,
    bounds,
    e.TextColor,
    ArrowDirection.Right);
   OnRenderArrow(tsarea);
  }
 }
}
Stephen Deken
+1  A: 

Why do you not look into using a System.Windows.Forms.ToolStrip rather than a MenuStrip. This will allow you to have the arrow functionality build in and will even solve the bad desing problem you are having.

Should you want you can specify that the toolstrip items do not show images and only show text. In this way you can almost exactly mimic the functionality of the menustrip, but get the "drop down arrows" free.

FryHard