Hello, in the attached code I creating a style for menustrip. For example when hover a root menu item looks like pressed. The problem is that I want to keep it pressed while it's dropdown opened but for a reason attached code not working. If I put a messagebox when a dropdown child selected it working (keeps style of the root item). So seems possible need right management of application events. Or...?
Appriciate in advance any ideas.
public class NextInnovationsToolStripRenderer : ToolStripRenderer
{
private bool IsChildSelected(ToolStripMenuItem toolStripItem)
{
for (int i = 0; i < toolStripItem.DropDownItems.Count; i++)
{
if (toolStripItem.DropDownItems[i].Selected)
{
return true;
}
else return IsChildSelected((ToolStripMenuItem)toolStripItem.DropDownItems[i]);
}
return false;
}
protected override void OnRenderMenuItemBackground(ToolStripItemRenderEventArgs e)
{
//base.OnRenderItemBackground(e);
Rectangle rc = new Rectangle(Point.Empty, e.Item.Size);
if (!e.Item.IsOnDropDown )
{
if (e.Item.Selected || IsChildSelected((ToolStripMenuItem)e.Item))
{
base.OnRenderItemBackground(e);
e.Graphics.DrawLine(Pens.DarkGray, 1, 0, rc.Width - 1, 0);
e.Graphics.DrawLine(Pens.DarkGray, 1, 0, 1, rc.Height - 1);
e.Graphics.DrawLine(Pens.Wheat, rc.Width - 1, 0, rc.Width - 1, rc.Height - 1);
e.Graphics.DrawLine(Pens.Wheat, 1, rc.Height - 1, rc.Width - 1, rc.Height - 1);
}
}
else if (e.Item.IsOnDropDown & e.Item.Selected)
{
e.Graphics.FillRectangle(Brushes.DarkBlue, rc);
e.Item.ForeColor = Color.White;
}
if (e.Item.IsOnDropDown & !e.Item.Selected)
{
e.Item.ForeColor = Color.Black;
}
Application.DoEvents();
}
protected override void OnRenderToolStripBorder(ToolStripRenderEventArgs e)
{
base.OnRenderToolStripBorder(e);
ControlPaint.DrawFocusRectangle(
e.Graphics,
e.AffectedBounds,
SystemColors.ControlDarkDark,
SystemColors.ControlDarkDark);
}
}
Alexander Israel