private void RenderItemInternal(ToolStripItemRenderEventArgs e) { ToolStripItem item = e.Item; Graphics dc = e.Graphics; ToolBarState toolBarState = GetToolBarState(item); VisualStyleElement normal = VisualStyleElement.ToolBar.Button.Normal; if (ToolStripManager.VisualStylesEnabled && System.Windows.Forms.VisualStyles.VisualStyleRenderer.IsElementDefined(normal)) { System.Windows.Forms.VisualStyles.VisualStyleRenderer visualStyleRenderer = VisualStyleRenderer; visualStyleRenderer.SetParameters(normal.ClassName, normal.Part, (int)toolBarState); visualStyleRenderer.DrawBackground(dc, new Rectangle(Point.Empty, item.Size)); } else { this.RenderSmall3DBorderInternal(dc, new Rectangle(Point.Empty, item.Size), toolBarState, item.RightToLeft == RightToLeft.Yes); } Rectangle contentRectangle = item.ContentRectangle; if (item.BackgroundImage != null) { ControlPaint.DrawBackgroundImage(dc, item.BackgroundImage, item.BackColor, item.BackgroundImageLayout, contentRectangle, contentRectangle); } else { ToolStrip currentParent = item.GetCurrentParent(); if (((currentParent != null) && (toolBarState != ToolBarState.Checked)) && (item.BackColor != currentParent.BackColor)) { FillBackground(dc, contentRectangle, item.BackColor); } } }
/// <summary> /// This exists mainly so that buttons, labels and items, etc can share the same implementation. /// If OnRenderButton called OnRenderItem we would never be able to change the implementation /// as it would be a breaking change. If in v1, the user overrode OnRenderItem to draw green triangles /// and in v2 we decided to add a feature to button that would require us to no longer call OnRenderItem - /// the user's version of OnRenderItem would not get called when he upgraded his framework. Hence /// everyone should just call this private shared method. Users need to override each item they want /// to change the look and feel of. /// </summary> private void RenderItemInternal(ToolStripItemRenderEventArgs e) { ToolStripItem item = e.Item; Graphics g = e.Graphics; ToolBarState state = GetToolBarState(item); VisualStyleElement toolBarElement = VisualStyleElement.ToolBar.Button.Normal; if (ToolStripManager.VisualStylesEnabled && (VisualStyleRenderer.IsElementDefined(toolBarElement))) { VisualStyleRenderer vsRenderer = VisualStyleRenderer; vsRenderer.SetParameters(toolBarElement.ClassName, toolBarElement.Part, (int)state); vsRenderer.DrawBackground(g, new Rectangle(Point.Empty, item.Size)); } else { RenderSmall3DBorderInternal(g, new Rectangle(Point.Empty, item.Size), state, (item.RightToLeft == RightToLeft.Yes)); } Rectangle fillRect = item.ContentRectangle; if (item.BackgroundImage != null) { ControlPaint.DrawBackgroundImage(g, item.BackgroundImage, item.BackColor, item.BackgroundImageLayout, fillRect, fillRect); } else { ToolStrip parent = item.GetCurrentParent(); if ((parent != null) && (state != ToolBarState.Checked) && (item.BackColor != parent.BackColor)) { FillBackground(g, fillRect, item.BackColor); } } }
public static Control GetMenuConrtol(this ToolStripItem menu) { if (menu.OwnerItem != null) { //多级菜单 //MessageBox.Show("" + menu.OwnerItem.Text); return(GetMenuConrtol(menu.OwnerItem)); } if (menu.GetCurrentParent() is ContextMenuStrip) { //一级菜单 ContextMenuStrip cm = (ContextMenuStrip)menu.GetCurrentParent(); return(cm.SourceControl); } else { MessageBox.Show("" + (menu.GetCurrentParent().GetType())); } return(null); }
public static void InvalidateSelection(ArrayList originalSelComps, ToolStripItem nextSelection, IServiceProvider provider, bool shiftPressed) { if ((nextSelection != null) && (provider != null)) { Region r = null; Region region = null; int width = 1; int num2 = 2; ToolStripItemDesigner designer = null; bool flag = false; try { Rectangle empty = Rectangle.Empty; IDesignerHost host = (IDesignerHost) provider.GetService(typeof(IDesignerHost)); if (host != null) { foreach (Component component in originalSelComps) { ToolStripItem item = component as ToolStripItem; if ((item != null) && (((originalSelComps.Count > 1) || ((originalSelComps.Count == 1) && (item.GetCurrentParent() != nextSelection.GetCurrentParent()))) || (((item is ToolStripSeparator) || (item is ToolStripControlHost)) || (!item.IsOnDropDown || item.IsOnOverflow)))) { designer = host.GetDesigner(item) as ToolStripItemDesigner; if (designer != null) { empty = designer.GetGlyphBounds(); GetAdjustedBounds(item, ref empty); empty.Inflate(width, width); if (r == null) { r = new Region(empty); empty.Inflate(-num2, -num2); r.Exclude(empty); } else { region = new Region(empty); empty.Inflate(-num2, -num2); region.Exclude(empty); r.Union(region); } } else if (item is DesignerToolStripControlHost) { flag = true; } } } } if (((r != null) || flag) || shiftPressed) { BehaviorService service = (BehaviorService) provider.GetService(typeof(BehaviorService)); if (service != null) { if (r != null) { service.Invalidate(r); } designer = host.GetDesigner(nextSelection) as ToolStripItemDesigner; if (designer != null) { empty = designer.GetGlyphBounds(); GetAdjustedBounds(nextSelection, ref empty); empty.Inflate(width, width); r = new Region(empty); empty.Inflate(-num2, -num2); r.Exclude(empty); service.Invalidate(r); } } } } finally { if (r != null) { r.Dispose(); } if (region != null) { region.Dispose(); } } } }
private void SetToolStripItemText(ToolStripItem toolStripItem, string text) { if(toolStripItem.GetCurrentParent().InvokeRequired) { toolStripItem.GetCurrentParent().Invoke(new Action<ToolStripItem, string>(SetToolStripItemText), new object[] { toolStripItem, text }); } else { toolStripItem.Text = text; } }
private void RenderSeparatorInternal(Graphics g, ToolStripItem item, Rectangle bounds, bool vertical) { Color foreColor = ColorTable.SeparatorDark; Color highlightColor = ColorTable.SeparatorLight; Pen foreColorPen = new Pen(foreColor); Pen highlightColorPen = new Pen(highlightColor); // undone emplore caching. bool disposeForeColorPen = true; bool disposeHighlightColorColorPen = true; bool isASeparator = item is ToolStripSeparator; bool isAHorizontalSeparatorNotOnDropDownMenu = false; if (isASeparator) { if (vertical) { if (!item.IsOnDropDown) { // center so that it matches office bounds.Y +=3; bounds.Height = Math.Max(0, bounds.Height -6); } } else { // offset after the image margin ToolStripDropDownMenu dropDownMenu = item.GetCurrentParent() as ToolStripDropDownMenu; if (dropDownMenu != null) { if (dropDownMenu.RightToLeft == RightToLeft.No) { // scoot over by the padding (that will line you up with the text - but go two PX before so that it visually looks // like the line meets up with the text). bounds.X += dropDownMenu.Padding.Left -2; bounds.Width = dropDownMenu.Width - bounds.X; } else { // scoot over by the padding (that will line you up with the text - but go two PX before so that it visually looks // like the line meets up with the text). bounds.X += 2; bounds.Width = dropDownMenu.Width - bounds.X - dropDownMenu.Padding.Right; } } else { isAHorizontalSeparatorNotOnDropDownMenu = true; } } } try { if (vertical) { if (bounds.Height >= 4) { bounds.Inflate(0, -2); // scoot down 2PX and start drawing } bool rightToLeft = (item.RightToLeft == RightToLeft.Yes); Pen leftPen = (rightToLeft) ? highlightColorPen : foreColorPen; Pen rightPen = (rightToLeft) ? foreColorPen : highlightColorPen; // Draw dark line int startX = bounds.Width / 2; g.DrawLine(leftPen, startX, bounds.Top, startX, bounds.Bottom - 1); // Draw highlight one pixel to the right startX++; g.DrawLine(rightPen, startX, bounds.Top + 1, startX, bounds.Bottom); } else { // // horizontal separator // Draw dark line if (isAHorizontalSeparatorNotOnDropDownMenu && bounds.Width >= 4) { bounds.Inflate(-2, 0); // scoot down 2PX and start drawing } int startY = bounds.Height / 2; g.DrawLine(foreColorPen, bounds.Left, startY, bounds.Right - 1, startY); if (!isASeparator || isAHorizontalSeparatorNotOnDropDownMenu) { // Draw highlight one pixel to the right startY++; g.DrawLine(highlightColorPen, bounds.Left + 1, startY, bounds.Right - 1, startY); } } } finally { if (disposeForeColorPen && foreColorPen != null) { foreColorPen.Dispose(); } if (disposeHighlightColorColorPen && highlightColorPen != null) { highlightColorPen.Dispose(); } } }
private static bool LooksPressed(ToolStripItem item) { var parent = item.GetCurrentParent(); if (parent is MenuStrip) return false; if (parent is ContextMenuStrip) return false; if (parent is ToolStripDropDown) return false; if (item.Pressed) return true; { var a = item as ToolStripDropDownButton; if (a != null && a.IsOnDropDown) return true; } { var a = item as ToolStripSplitButton; if (a != null && a.IsOnDropDown) return true; } return false; }
private void SetFormCaptionByToolStripItem(ToolStripItem tsi) { tsi.GetCurrentParent().FindForm().Text = string.Format("Журнал (Смена: {0})", GetFormCaption()); }
private bool CommonParent(ToolStripItem oldSelection, ToolStripItem newSelection) { ToolStrip currentParent = oldSelection.GetCurrentParent(); ToolStrip strip2 = newSelection.GetCurrentParent(); return (currentParent == strip2); }
private void RenderSeparatorInternal(Graphics g, ToolStripItem item, Rectangle bounds, bool vertical) { Color separatorDark = this.ColorTable.SeparatorDark; Color separatorLight = this.ColorTable.SeparatorLight; Pen pen = new Pen(separatorDark); Pen pen2 = new Pen(separatorLight); bool flag = true; bool flag2 = true; bool flag3 = item is ToolStripSeparator; bool flag4 = false; if (flag3) { if (vertical) { if (!item.IsOnDropDown) { bounds.Y += 3; bounds.Height = Math.Max(0, bounds.Height - 6); } } else { ToolStripDropDownMenu currentParent = item.GetCurrentParent() as ToolStripDropDownMenu; if (currentParent != null) { if (currentParent.RightToLeft == RightToLeft.No) { bounds.X += currentParent.Padding.Left - 2; bounds.Width = currentParent.Width - bounds.X; } else { bounds.X += 2; bounds.Width = (currentParent.Width - bounds.X) - currentParent.Padding.Right; } } else { flag4 = true; } } } try { if (vertical) { if (bounds.Height >= 4) { bounds.Inflate(0, -2); } bool flag5 = item.RightToLeft == RightToLeft.Yes; Pen pen3 = flag5 ? pen2 : pen; Pen pen4 = flag5 ? pen : pen2; int num = bounds.Width / 2; g.DrawLine(pen3, num, bounds.Top, num, bounds.Bottom - 1); num++; g.DrawLine(pen4, num, bounds.Top + 1, num, bounds.Bottom); } else { if (flag4 && (bounds.Width >= 4)) { bounds.Inflate(-2, 0); } int num2 = bounds.Height / 2; g.DrawLine(pen, bounds.Left, num2, bounds.Right - 1, num2); if (!flag3 || flag4) { num2++; g.DrawLine(pen2, bounds.Left + 1, num2, bounds.Right - 1, num2); } } } finally { if (flag && (pen != null)) { pen.Dispose(); } if (flag2 && (pen2 != null)) { pen2.Dispose(); } } }