protected override void OnMouseUp(MouseEventArgs e) { if (!Enabled) { return; } RaiseClick(); if (DropDownItems.Count == 0) { ResetSelected(); if (Parent.Parent == null) { Parent.Dispose(); } return; } if (_dropDownToolStrip != null) { _dropDownToolStrip = null; return; } if (!_pressed) { _dropDownToolStrip = new ToolStrip(); _dropDownToolStrip.Context = true; _dropDownToolStrip.OwnerItem = this; if (Parent.ShadowHandler != null) { _dropDownToolStrip.MakeShadow(); } int index = Parent.Items.IndexOf(this); int x = 0, y = 0; for (int i = 0; i < index; i++) { if (Parent.Orientation == Forms.Orientation.Horizontal) { x += Parent.Items[i].Width; } if (Parent.Orientation == Forms.Orientation.Vertical) { y += Parent.Items[i].Height; } } //_dropDownToolStrip.BackColor = Parent.BackColor; _dropDownToolStrip.Items.AddRange(DropDownItems); for (int i = 0; i < _dropDownToolStrip.Items.Count; i++) { _dropDownToolStrip.Items[i].OwnerItem = this; _dropDownToolStrip.Items[i].Selected = false; } _dropDownToolStrip.ShadowBox = true; _dropDownToolStrip.Orientation = Orientation.Vertical; int height = 0; for (int i = 0; i < DropDownItems.Count; i++) { height += DropDownItems[i].Height; } _dropDownToolStrip.Size = new Size(DropDownItems[0].Width, height); var parentLocationClient = Parent.PointToScreen(Point.Zero); if (Parent.Orientation == Orientation.Horizontal) { _dropDownToolStrip.Location = new Point(parentLocationClient.X + x + Parent.Padding.Left, parentLocationClient.Y + Parent.Height - HoverPadding.Y - 1); _dropDownToolStrip.BorderColor = Color.Transparent; } else { _dropDownToolStrip.Location = new Point(parentLocationClient.X + x + Parent.Width, parentLocationClient.Y + y); _dropDownToolStrip.BorderColor = Parent.BorderColor; if (_dropDownToolStrip.Location.X + _dropDownToolStrip.Width > Screen.PrimaryScreen.WorkingArea.Width) { _dropDownToolStrip.Location = new Point(parentLocationClient.X - _dropDownToolStrip.Width, _dropDownToolStrip.Location.Y); } } _dropDownToolStrip.OnDisposing += (object sender, EventArgs args) => { var clientRect = new System.Drawing.Rectangle(x, y, Width, Height); var contains = clientRect.Contains(Parent.PointToClient(Control.MousePosition)); if (!contains) { _pressed = false; } else { _pressed = !_pressed; } _dropDownToolStrip = null; }; } else { _pressed = false; } }
protected override void OnMouseUp(MouseEventArgs e) { if (!Enabled) return; RaiseClick(); if (DropDownItems.Count == 0) { ResetSelected(); if (Parent.Parent == null) Parent.Dispose(); return; } if (_dropDownToolStrip != null) { _dropDownToolStrip = null; return; } if (!_pressed) { _dropDownToolStrip = new ToolStrip(); _dropDownToolStrip.Context = true; _dropDownToolStrip.OwnerItem = this; if (Parent.ShadowHandler != null) _dropDownToolStrip.MakeShadow(); int index = Parent.Items.IndexOf(this); int x = 0, y = 0; for (int i = 0; i < index; i++) { if (Parent.Orientation == Forms.Orientation.Horizontal) x += Parent.Items[i].Width; if (Parent.Orientation == Forms.Orientation.Vertical) y += Parent.Items[i].Height; } //_dropDownToolStrip.BackColor = Parent.BackColor; _dropDownToolStrip.Items.AddRange(DropDownItems); for (int i = 0; i < _dropDownToolStrip.Items.Count; i++) { _dropDownToolStrip.Items[i].OwnerItem = this; _dropDownToolStrip.Items[i].Selected = false; } _dropDownToolStrip.ShadowBox = true; _dropDownToolStrip.Orientation = Orientation.Vertical; int height = 0; for (int i = 0; i < DropDownItems.Count; i++) height += DropDownItems[i].Height; _dropDownToolStrip.Size = new Size(DropDownItems[0].Width, height); var parentLocationClient = Parent.PointToScreen(Point.Zero); if (Parent.Orientation == Orientation.Horizontal) { _dropDownToolStrip.Location = new Point(parentLocationClient.X + x + Parent.Padding.Left, parentLocationClient.Y + Parent.Height - HoverPadding.Y - 1); _dropDownToolStrip.BorderColor = Color.Transparent; } else { _dropDownToolStrip.Location = new Point(parentLocationClient.X + x + Parent.Width, parentLocationClient.Y + y); _dropDownToolStrip.BorderColor = Parent.BorderColor; if (_dropDownToolStrip.Location.X + _dropDownToolStrip.Width > Screen.PrimaryScreen.WorkingArea.Width) { _dropDownToolStrip.Location = new Point(parentLocationClient.X - _dropDownToolStrip.Width, _dropDownToolStrip.Location.Y); } } _dropDownToolStrip.OnDisposing += (object sender, EventArgs args) => { var clientRect = new System.Drawing.Rectangle(x, y, Width, Height); var contains = clientRect.Contains(Parent.PointToClient(Control.MousePosition)); if (!contains) _pressed = false; else _pressed = !_pressed; _dropDownToolStrip = null; }; } else { _pressed = false; } }