前提 入行已經7,8年了,一直想做一套漂亮點的自定義控制項,於是就有了本系列文章。 GitHub:https://github.com/kwwwvagaa/NetWinformControl 碼雲:https://gitee.com/kwwwvagaa/net_winform_custom_contr ...
前提
入行已經7,8年了,一直想做一套漂亮點的自定義控制項,於是就有了本系列文章。
GitHub:https://github.com/kwwwvagaa/NetWinformControl
碼雲:https://gitee.com/kwwwvagaa/net_winform_custom_control.git
如果覺得寫的還行,請點個 star 支持一下吧
來都來了,點個【推薦】再走吧,謝謝
NuGet
Install-Package HZH_Controls
目錄
https://www.cnblogs.com/bfyx/p/11364884.html
用處及效果
準備工作
主要用的就是停靠窗體了,(十九)c#Winform自定義控制項-停靠窗體,不瞭解的可以先去看一下
思路:
通過實體對象設置的對齊方式來實現左右對齊,
當滑鼠進入一項的時候,判斷是否彈出下拉列表,或關閉其他列表
開始
添加一個類用來設置節點信息
1 public class NavigationMenuItem 2 { 3 /// <summary> 4 /// The icon 5 /// </summary> 6 private Image icon; 7 /// <summary> 8 /// Gets or sets the icon. 9 /// </summary> 10 /// <value>The icon.</value> 11 [Description("圖標,僅頂級節點有效")] 12 public Image Icon 13 { 14 get { return icon; } 15 set { icon = value; } 16 } 17 18 /// <summary> 19 /// The text 20 /// </summary> 21 private string text; 22 /// <summary> 23 /// Gets or sets the text. 24 /// </summary> 25 /// <value>The text.</value> 26 27 [Description("文本")] 28 public string Text 29 { 30 get { return text; } 31 set { text = value; } 32 } 33 34 /// <summary> 35 /// The show tip 36 /// </summary> 37 private bool showTip; 38 /// <summary> 39 /// Gets or sets a value indicating whether [show tip].當TipText為空時只顯示一個小圓點,否則顯示TipText文字 40 /// </summary> 41 /// <value><c>true</c> if [show tip]; otherwise, <c>false</c>.</value> 42 [Description("是否顯示角標,僅頂級節點有效")] 43 public bool ShowTip 44 { 45 get { return showTip; } 46 set { showTip = value; } 47 } 48 49 /// <summary> 50 /// The tip text 51 /// </summary> 52 private string tipText; 53 /// <summary> 54 /// Gets or sets the tip text 55 /// </summary> 56 /// <value>The tip text.</value> 57 [Description("角標文字,僅頂級節點有效")] 58 public string TipText 59 { 60 get { return tipText; } 61 set { tipText = value; } 62 } 63 /// <summary> 64 /// The items 65 /// </summary> 66 private NavigationMenuItem[] items; 67 /// <summary> 68 /// Gets or sets the items. 69 /// </summary> 70 /// <value>The items.</value> 71 [Description("子項列表")] 72 public NavigationMenuItem[] Items 73 { 74 get { return items; } 75 set 76 { 77 items = value; 78 if (value != null) 79 { 80 foreach (var item in value) 81 { 82 item.ParentItem = this; 83 } 84 } 85 } 86 } 87 88 /// <summary> 89 /// The anchor right 90 /// </summary> 91 private bool anchorRight; 92 93 /// <summary> 94 /// Gets or sets a value indicating whether [anchor right]. 95 /// </summary> 96 /// <value><c>true</c> if [anchor right]; otherwise, <c>false</c>.</value> 97 [Description("是否靠右對齊")] 98 public bool AnchorRight 99 { 100 get { return anchorRight; } 101 set { anchorRight = value; } 102 } 103 104 /// <summary> 105 /// The item width 106 /// </summary> 107 private int itemWidth = 100; 108 109 /// <summary> 110 /// Gets or sets the width of the item. 111 /// </summary> 112 /// <value>The width of the item.</value> 113 [Description("寬度")] 114 public int ItemWidth 115 { 116 get { return itemWidth; } 117 set { itemWidth = value; } 118 } 119 120 /// <summary> 121 /// Gets or sets the data source. 122 /// </summary> 123 /// <value>The data source.</value> 124 [Description("數據源")] 125 public object DataSource { get; set; } 126 /// <summary> 127 /// Gets or sets a value indicating whether this instance has split lint at top. 128 /// </summary> 129 /// <value><c>true</c> if this instance has split lint at top; otherwise, <c>false</c>.</value> 130 [Description("是否在此項頂部顯示一個分割線")] 131 public bool HasSplitLintAtTop { get; set; } 132 133 /// <summary> 134 /// Gets the parent item. 135 /// </summary> 136 /// <value>The parent item.</value> 137 [Description("父節點")] 138 public NavigationMenuItem ParentItem { get; private set; } 139 }
添加一個自定義控制項UCNavigationMenu
添加一些屬性
1 /// <summary> 2 /// Occurs when [click itemed]. 3 /// </summary> 4 [Description("點擊節點事件"), Category("自定義")] 5 6 public event EventHandler ClickItemed; 7 /// <summary> 8 /// The select item 9 /// </summary> 10 private NavigationMenuItem selectItem = null; 11 12 /// <summary> 13 /// Gets the select item. 14 /// </summary> 15 /// <value>The select item.</value> 16 [Description("選中的節點"), Category("自定義")] 17 public NavigationMenuItem SelectItem 18 { 19 get { return selectItem; } 20 private set { selectItem = value; } 21 } 22 23 24 /// <summary> 25 /// The items 26 /// </summary> 27 NavigationMenuItem[] items; 28 29 /// <summary> 30 /// Gets or sets the items. 31 /// </summary> 32 /// <value>The items.</value> 33 [Description("節點列表"), Category("自定義")] 34 public NavigationMenuItem[] Items 35 { 36 get { return items; } 37 set 38 { 39 items = value; 40 ReloadMenu(); 41 } 42 } 43 44 /// <summary> 45 /// The tip color 46 /// </summary> 47 private Color tipColor = Color.FromArgb(255, 87, 34); 48 49 /// <summary> 50 /// Gets or sets the color of the tip. 51 /// </summary> 52 /// <value>The color of the tip.</value> 53 [Description("角標顏色"), Category("自定義")] 54 public Color TipColor 55 { 56 get { return tipColor; } 57 set { tipColor = value; } 58 } 59 60 /// <summary> 61 /// 獲取或設置控制項的前景色。 62 /// </summary> 63 /// <value>The color of the fore.</value> 64 /// <PermissionSet> 65 /// <IPermission class="System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true" /> 66 /// </PermissionSet> 67 public override System.Drawing.Color ForeColor 68 { 69 get 70 { 71 return base.ForeColor; 72 } 73 set 74 { 75 base.ForeColor = value; 76 foreach (Control c in this.Controls) 77 { 78 c.ForeColor = value; 79 } 80 } 81 } 82 /// <summary> 83 /// 獲取或設置控制項顯示的文字的字體。 84 /// </summary> 85 /// <value>The font.</value> 86 /// <PermissionSet> 87 /// <IPermission class="System.Security.Permissions.EnvironmentPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true" /> 88 /// <IPermission class="System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true" /> 89 /// <IPermission class="System.Security.Permissions.SecurityPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Flags="UnmanagedCode, ControlEvidence" /> 90 /// <IPermission class="System.Diagnostics.PerformanceCounterPermission, System, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true" /> 91 /// </PermissionSet> 92 public override Font Font 93 { 94 get 95 { 96 return base.Font; 97 } 98 set 99 { 100 base.Font = value; 101 foreach (Control c in this.Controls) 102 { 103 c.Font = value; 104 } 105 } 106 } 107 108 /// <summary> 109 /// The m LST anchors 110 /// </summary> 111 Dictionary<NavigationMenuItem, FrmAnchor> m_lstAnchors = new Dictionary<NavigationMenuItem, FrmAnchor>();
重載菜單
1 private void ReloadMenu() 2 { 3 try 4 { 5 ControlHelper.FreezeControl(this, true); 6 this.Controls.Clear(); 7 if (items != null && items.Length > 0) 8 { 9 foreach (var item in items) 10 { 11 var menu = (NavigationMenuItem)item; 12 Label lbl = new Label(); 13 lbl.AutoSize = false; 14 lbl.TextAlign = ContentAlignment.MiddleCenter; 15 lbl.Width = menu.ItemWidth; 16 lbl.Text = menu.Text; 17 18 lbl.Font = Font; 19 lbl.ForeColor = ForeColor; 20 21 lbl.Paint += lbl_Paint; 22 lbl.MouseEnter += lbl_MouseEnter; 23 lbl.Tag = menu; 24 lbl.Click += lbl_Click; 25 if (menu.AnchorRight) 26 { 27 lbl.Dock = DockStyle.Right; 28 } 29 else 30 { 31 lbl.Dock = DockStyle.Left; 32 } 33 this.Controls.Add(lbl); 34 35 lbl.BringToFront(); 36 } 37 38 39 } 40 } 41 finally 42 { 43 ControlHelper.FreezeControl(this, false); 44 } 45 }
顯示下級菜單
1 private void ShowMoreMenu(Label lbl) 2 { 3 var menu = (NavigationMenuItem)lbl.Tag; 4 if (CheckShow(menu)) 5 { 6 if (menu.Items != null && menu.Items.Length > 0) 7 { 8 Panel panel = new Panel(); 9 panel.BackColor = Color.White; 10 panel.Paint += panel_Paint; 11 panel.Padding = new System.Windows.Forms.Padding(1); 12 Size size = GetItemsSize(menu.Items); 13 var height = size.Height * menu.Items.Length + 2; 14 height += menu.Items.Count(p => p.HasSplitLintAtTop);//分割線 15 if (size.Width < lbl.Width) 16 size.Width = lbl.Width; 17 panel.Size = new Size(size.Width, height); 18 19 foreach (var item in menu.Items) 20 { 21 if (item.HasSplitLintAtTop) 22 { 23 UCSplitLine_H line = new UCSplitLine_H(); 24 line.Dock = DockStyle.Top; 25 panel.Controls.Add(line); 26 line.BringToFront(); 27 } 28 Label _lbl = new Label(); 29 _lbl.Font = Font; 30 _lbl.ForeColor = this.BackColor; 31 _lbl.AutoSize = false; 32 _lbl.TextAlign = ContentAlignment.MiddleCenter; 33 _lbl.Height = size.Height; 34 _lbl.Text = item.Text; 35 _lbl.Dock = DockStyle.Top; 36 _lbl.BringToFront(); 37 _lbl.Paint += lbl_Paint; 38 _lbl.MouseEnter += lbl_MouseEnter; 39 _lbl.Tag = item; 40 _lbl.Click += lbl_Click; 41 _lbl.Size = new System.Drawing.Size(size.Width, size.Height); 42 panel.Controls.Add(_lbl); 43 _lbl.BringToFront(); 44 } 45 Point point = Point.Empty; 46 47 if (menu.ParentItem != null) 48 { 49 Point p = lbl.Parent.PointToScreen(lbl.Location); 50 if (p.X + lbl.Width + panel.Width > Screen.PrimaryScreen.Bounds.Width) 51 { 52 point = new Point(-1 * panel.Width - 2, -1 * lbl.Height); 53 } 54 else 55 { 56 point = new Point(panel.Width + 2, -1 * lbl.Height); 57 } 58 } 59 m_lstAnchors[menu] = new FrmAnchor(lbl, panel, point); 60 m_lstAnchors[menu].FormClosing += UCNavigationMenu_FormClosing; 61 m_lstAnchors[menu].Show(); 62 m_lstAnchors[menu].Size = new Size(size.Width, height); 63 } 64 } 65 66 }
輔助函數
1 /// <summary> 2 /// Checks the show. 3 /// </summary> 4 /// <param name="menu">The menu.</param> 5 /// <returns><c>true</c> if XXXX, <c>false</c> otherwise.</returns> 6 private bool CheckShow(NavigationMenuItem menu) 7 { 8 //檢查已經打開的節點 9 if (m_lstAnchors.ContainsKey(menu)) 10 { 11 CloseList(menu); 12 return false; 13 } 14 if (HasInCacheChild(menu)) 15 { 16 if (m_lstAnchors.ContainsKey(menu.ParentItem)) 17 { 18 CloseList(menu.ParentItem); 19 return true; 20 } 21 return false; 22 } 23 else 24 { 25 for (int i = 0; i < 1; ) 26 { 27 try 28 { 29 foreach (var item in m_lstAnchors) 30 { 31 if (m_lstAnchors[item.Key] != null && !m_lstAnchors[item.Key].IsDisposed) 32 { 33 m_lstAnchors[item.Key].Close(); 34 } 35 } 36 } 37 catch 38 { 39 continue; 40 } 41 i++; 42 } 43 m_lstAnchors.Clear(); 44 return true; 45 } 46 } 47 48 /// <summary> 49 /// Determines whether [has in cache child] [the specified menu]. 50 /// </summary> 51 /// <param name="menu">The menu.</param> 52 /// <returns><c>true</c> if [has in cache child] [the specified menu]; otherwise, <c>false</c>.</returns> 53 private bool HasInCacheChild(NavigationMenuItem menu) 54 { 55 foreach (var item in m_lstAnchors) 56 { 57 if (item.Key == menu) 58 { 59 return true; 60 } 61 else 62 { 63 if (item.Key.Items != null)