using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace DPumpHydr.WinFrmUI.WenSkin.Forms { public class LeftButtonForm : WenBottomInfoForm { public LeftButtonForm() { RefreshPadding(); AddOffonButton(); this.Name = "LeftButtonForm"; this.Text = "LeftButtonForm"; flow = new FlowLayoutPanel() { AutoSize = true, Location = new Point(2, this.TitleHeight + 50), Height = 0, Width = LeftWidth - 6, BackColor = Color.Transparent, FlowDirection = FlowDirection.TopDown, Anchor = AnchorStyles.Left | AnchorStyles.Top }; flow.SizeChanged += (s, e) => { foreach (WenSkin.Controls.WenButton button in flow.Controls) { button.Width = flow.Width - 6; } }; this.Controls.Add(flow); this.FrameColorChanged += (s, e) => { if (FrameColor == Color.Transparent) return; foreach (WenSkin.Controls.WenButton button in flow.Controls) { Color color = this.FrameColor; button.CustomForeColor = WenSkin.Controls.ControlHelper.ColorReverse(color); } }; } #region 委托事件 protected virtual void OnLeftMenuClick(LeftMenuItem item) { LeftMenuClick?.Invoke(this, new LeftMenuEventArgs(item)); } public class LeftMenuEventArgs : EventArgs { public LeftMenuEventArgs(LeftMenuItem item) { Item = item; } public LeftMenuItem Item { get; set; } } #endregion #region 委托 public delegate void LeftMenuItemEventHandler(object sender, LeftMenuEventArgs e); [Category("Wen"), Description("左侧按钮点击事件")] public event LeftMenuItemEventHandler LeftMenuClick; #endregion #region 私有属性 private int leftWidth = 50; private FlowLayoutPanel flow; private int leftMaxWidth = 150; private int leftMinWidth = 50; private LeftMenuItemCollection leftMenu; #endregion #region 公有属性 [Category("WenWidth"), Description("左边按钮区域尺寸"), DefaultValue(100)] [RefreshProperties(RefreshProperties.All)] public int LeftWidth { get => leftWidth; set { leftWidth = value; RefreshPadding(); } } [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)] [Category("Wen"), Description("左侧菜单列表")] [Editor("WenSkin.Design.Editor.WenCollectionEditor", typeof(System.Drawing.Design.UITypeEditor))] public LeftMenuItemCollection LeftMenu => leftMenu ?? (leftMenu = new LeftMenuItemCollection(this)); [Category("WenWidth"), Description("左边展开最大尺寸"), DefaultValue(150)] public int LeftMaxWidth { get => leftMaxWidth; set => leftMaxWidth = value; } [Category("WenWidth"), Description("左边展开最小尺寸"), DefaultValue(50)] public int LeftMinWidth { get => leftMinWidth; set => leftMinWidth = value; } [DefaultValue(typeof(Padding), "30,50,0,30"), Category("布局"), Description("指定控件内部间距")] public new Padding Padding { get => base.Padding; set => base.Padding = value; } #endregion #region 重绘 protected override void OnPaint(PaintEventArgs e) { base.OnPaint(e); DrawLeft(e.Graphics); } private void DrawLeft(Graphics g) { Brush b = new SolidBrush(FrameColor); g.FillRectangle(b, 0, this.TitleHeight, this.LeftWidth, this.Height - TitleHeight - BottomWidth); } #endregion //添加按钮 private void AddOffonButton() { WenSkin.Controls.WenButtonTriangle wenButton = new Controls.WenButtonTriangle() { Location = new Point(this.LeftWidth - 24, this.TitleHeight + 4), BackColor = Color.Transparent, State = true, ButtonColor = Color.FromArgb(199, 199, 199), OpenStyle = WenSkin.Controls.WenButtonTriangleStyle.Right, CloseStyle = WenSkin.Controls.WenButtonTriangleStyle.Left, Size = new Size(16, 16), }; wenButton.Click += (s, e) => { if (wenButton.State) { LeftWidth = LeftMinWidth; flow.Width = LeftWidth - 6; flow.Location = new Point(3, this.TitleHeight + 50); } else { LeftWidth = LeftMaxWidth; flow.Width = LeftWidth - 6; flow.Location = new Point(3, this.TitleHeight + 50); } wenButton.Location = new Point(this.LeftWidth - 24, this.TitleHeight + 4); }; this.Controls.Add(wenButton); //尝试添加一个按钮 } protected override void RefreshPadding() { this.Padding = new Padding() { Top = TitleHeight, Left = LeftWidth, Bottom = BottomWidth, Right = FrameWidth }; this.Invalidate(); } public class LeftMenuItemCollection : DPumpHydr.WinFrmUI.WenSkin.Controls.WenCollection { private LeftButtonForm owner; public LeftMenuItemCollection(LeftButtonForm owner) { this.owner = owner; } public LeftMenuItem this[int index] => Items[index] as LeftMenuItem; public LeftMenuItem this[string key] => Items.ToArray().ToList().Find(a => (a as LeftMenuItem).Name == key) as LeftMenuItem; public int Add(LeftMenuItem value) { Controls.WenButton button = new Controls.WenButton() { Name = value.Name, Text = value.Text, Image = value.Image, ImageSize = new Size(32, 32), Tag = value, Height = 38, BackColor = Color.Transparent, Width = owner.LeftWidth - 12, CustomForeColor = value.ForeColor, }; button.Click += (s, ex) => { owner.OnLeftMenuClick(value); }; value.WenImageButton = button; owner.flow.Controls.Add(button); owner.flow.Invalidate(); return Items.Add(value); } public int Add(string text) { return (Add(new LeftMenuItem(text))); } public override int Add(object value) { return Add(value as LeftMenuItem); } public void AddRange(LeftMenuItem[] items) { foreach (var item in items) { Add(item); } } public void Remove(LeftMenuItem value) { base.Remove(value); foreach (WenSkin.Controls.WenButton button in owner.flow.Controls) { if (button.Tag is LeftMenuItem leftMenuItem) { if (leftMenuItem == value) owner.flow.Controls.Remove(button); } } } public override void RemoveAt(int index) { base.RemoveAt(index); owner.flow.Controls.RemoveAt(index); } public override void Remove(object value) { base.Remove(value); } public override void Clear() { base.Clear(); owner.flow.Controls.Clear(); } } public class LeftMenuItem { public LeftMenuItem() { } public LeftMenuItem(string text) { Text = text; } #region 私有属性 private string name = "MenuItem"; private Image image = null; private Color foreColor = Color.FromArgb(199, 199, 199); private Color backColor = Color.Transparent; private string text = "菜单"; private bool visible = true; #endregion public Controls.WenButton WenImageButton { get; set; } [Category("Wen"), Description("按钮图标"), DefaultValue("MenuItem")] public string Name { get => name; set { name = value; if (WenImageButton != null) WenImageButton.Name = value; } } [Category("Wen"), Description("按钮图标"), DefaultValue("菜单")] public string Text { get => text; set { text = value; if (WenImageButton != null) WenImageButton.Text = value; } } [Category("Wen"), Description("按钮图标"), DefaultValue(null)] public Image Image { get => image; set { image = value; if (WenImageButton != null) WenImageButton.Image = value; } } [DefaultValue(typeof(Color), "Transparent"), Category("WenColor"), Description("背景色")] public Color BackColor { get => backColor; set { backColor = value; if (WenImageButton != null) WenImageButton.BackColor = value; } } [DefaultValue(typeof(Color), "199, 199, 199"), Category("WenColor"), Description("前景色")] public Color ForeColor { get => foreColor; set { foreColor = value; if (WenImageButton != null) WenImageButton.ForeColor = value; } } [DefaultValue(true), Category("Wen"), Description("获取或设置一个值,该值指示是否显示该控件及其所有子控件")] public bool Visible { get => visible; set { visible = value; if (WenImageButton != null) WenImageButton.Visible = value; } } public override string ToString() { return $"{Name}[{Text}]"; } } } }