前提 入行已經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
用處及效果
目前支持ScrollableControl,TreeView,TextBox的滾動條,只需要在相應的界面上添加組件ScrollbarComponent即可
準備工作
用到了(一)c#Winform自定義控制項-基類控制項 ,如果你還不瞭解,可以先去看一下
自定義滾動條有2種方式,1:攔截windows消息,重繪,2:做一個新的,蓋上去擋著,這裡我們採用的是第二種。
開始
添加一個類UCVScrollbar,繼承UCControlBase
一些屬性
1 /// <summary> 2 /// The mo large change 3 /// </summary> 4 protected int moLargeChange = 10; 5 /// <summary> 6 /// The mo small change 7 /// </summary> 8 protected int moSmallChange = 1; 9 /// <summary> 10 /// The mo minimum 11 /// </summary> 12 protected int moMinimum = 0; 13 /// <summary> 14 /// The mo maximum 15 /// </summary> 16 protected int moMaximum = 100; 17 /// <summary> 18 /// The mo value 19 /// </summary> 20 protected int moValue = 0; 21 /// <summary> 22 /// The n click point 23 /// </summary> 24 private int nClickPoint; 25 /// <summary> 26 /// The mo thumb top 27 /// </summary> 28 protected int moThumbTop = 0; 29 /// <summary> 30 /// The mo automatic size 31 /// </summary> 32 protected bool moAutoSize = false; 33 /// <summary> 34 /// The mo thumb down 35 /// </summary> 36 private bool moThumbDown = false; 37 /// <summary> 38 /// The mo thumb dragging 39 /// </summary> 40 private bool moThumbDragging = false; 41 /// <summary> 42 /// Occurs when [scroll]. 43 /// </summary> 44 public new event EventHandler Scroll = null; 45 /// <summary> 46 /// Occurs when [value changed]. 47 /// </summary> 48 public event EventHandler ValueChanged = null; 49 50 /// <summary> 51 /// The BTN height 52 /// </summary> 53 private int btnHeight = 18; 54 /// <summary> 55 /// The m int thumb minimum height 56 /// </summary> 57 private int m_intThumbMinHeight = 15; 58 59 /// <summary> 60 /// Gets or sets the height of the BTN. 61 /// </summary> 62 /// <value>The height of the BTN.</value> 63 public int BtnHeight 64 { 65 get { return btnHeight; } 66 set { btnHeight = value; } 67 } 68 /// <summary> 69 /// Gets or sets the large change. 70 /// </summary> 71 /// <value>The large change.</value> 72 [EditorBrowsable(EditorBrowsableState.Always), Browsable(true), DefaultValue(false), Category("自定義"), Description("LargeChange")] 73 public int LargeChange 74 { 75 get { return moLargeChange; } 76 set 77 { 78 moLargeChange = value; 79 Invalidate(); 80 } 81 } 82 83 /// <summary> 84 /// Gets or sets the small change. 85 /// </summary> 86 /// <value>The small change.</value> 87 [EditorBrowsable(EditorBrowsableState.Always), Browsable(true), DefaultValue(false), Category("自定義"), Description("SmallChange")] 88 public int SmallChange 89 { 90 get { return moSmallChange; } 91 set 92 { 93 moSmallChange = value; 94 Invalidate(); 95 } 96 } 97 98 /// <summary> 99 /// Gets or sets the minimum. 100 /// </summary> 101 /// <value>The minimum.</value> 102 [EditorBrowsable(EditorBrowsableState.Always), Browsable(true), DefaultValue(false), Category("自定義"), Description("Minimum")] 103 public int Minimum 104 { 105 get { return moMinimum; } 106 set 107 { 108 moMinimum = value; 109 Invalidate(); 110 } 111 } 112 113 /// <summary> 114 /// Gets or sets the maximum. 115 /// </summary> 116 /// <value>The maximum.</value> 117 [EditorBrowsable(EditorBrowsableState.Always), Browsable(true), DefaultValue(false), Category("自定義"), Description("Maximum")] 118 public int Maximum 119 { 120 get { return moMaximum; } 121 set 122 { 123 moMaximum = value; 124 Invalidate(); 125 } 126 } 127 128 /// <summary> 129 /// Gets or sets the value. 130 /// </summary> 131 /// <value>The value.</value> 132 [EditorBrowsable(EditorBrowsableState.Always), Browsable(true), DefaultValue(false), Category("自定義"), Description("Value")] 133 public int Value 134 { 135 get { return moValue; } 136 set 137 { 138 moValue = value; 139 140 int nTrackHeight = (this.Height - btnHeight * 2); 141 float fThumbHeight = ((float)LargeChange / (float)Maximum) * nTrackHeight; 142 int nThumbHeight = (int)fThumbHeight; 143 144 if (nThumbHeight > nTrackHeight) 145 { 146 nThumbHeight = nTrackHeight; 147 fThumbHeight = nTrackHeight; 148 } 149 if (nThumbHeight < m_intThumbMinHeight) 150 { 151 nThumbHeight = m_intThumbMinHeight; 152 fThumbHeight = m_intThumbMinHeight; 153 } 154 155 //figure out value 156 int nPixelRange = nTrackHeight - nThumbHeight; 157 int nRealRange = (Maximum - Minimum) - LargeChange; 158 float fPerc = 0.0f; 159 if (nRealRange != 0) 160 { 161 fPerc = (float)moValue / (float)nRealRange; 162 163 } 164 165 float fTop = fPerc * nPixelRange; 166 moThumbTop = (int)fTop; 167 168 169 Invalidate(); 170 } 171 } 172 173 /// <summary> 174 /// Gets or sets a value indicating whether [automatic size]. 175 /// </summary> 176 /// <value><c>true</c> if [automatic size]; otherwise, <c>false</c>.</value> 177 public override bool AutoSize 178 { 179 get 180 { 181 return base.AutoSize; 182 } 183 set 184 { 185 base.AutoSize = value; 186 if (base.AutoSize) 187 { 188 this.Width = 15; 189 } 190 } 191 } 192 193 /// <summary> 194 /// The thumb color 195 /// </summary> 196 private Color thumbColor = Color.FromArgb(255, 77, 58); 197 198 /// <summary> 199 /// Gets or sets the color of the thumb. 200 /// </summary> 201 /// <value>The color of the thumb.</value> 202 public Color ThumbColor 203 { 204 get { return thumbColor; } 205 set { thumbColor = value; } 206 }
重繪
1 protected override void OnPaint(PaintEventArgs e) 2 { 3 base.OnPaint(e); 4 e.Graphics.SetGDIHigh(); 5 6 //draw thumb 7 int nTrackHeight = (this.Height - btnHeight * 2); 8 float fThumbHeight = ((float)LargeChange / (float)Maximum) * nTrackHeight; 9 int nThumbHeight = (int)fThumbHeight; 10 11 if (nThumbHeight > nTrackHeight) 12 { 13 nThumbHeight = nTrackHeight; 14 fThumbHeight = nTrackHeight; 15 } 16 if (nThumbHeight < m_intThumbMinHeight) 17 { 18 nThumbHeight = m_intThumbMinHeight; 19 fThumbHeight = m_intThumbMinHeight; 20 } 21 int nTop = moThumbTop; 22 nTop += btnHeight; 23 e.Graphics.FillPath(new SolidBrush(thumbColor), new Rectangle(1, nTop, this.Width - 3, nThumbHeight).CreateRoundedRectanglePath(this.ConerRadius)); 24 25 ControlHelper.PaintTriangle(e.Graphics, new SolidBrush(thumbColor), new Point(this.Width / 2, btnHeight - Math.Min(5, this.Width / 2)), Math.Min(5, this.Width / 2), GraphDirection.Upward); 26 ControlHelper.PaintTriangle(e.Graphics, new SolidBrush(thumbColor), new Point(this.Width / 2, this.Height - (btnHeight - Math.Min(5, this.Width / 2))), Math.Min(5, this.Width / 2), GraphDirection.Downward); 27 28 }
處理下滑鼠事件
1 /// <summary> 2 /// Handles the MouseDown event of the CustomScrollbar control. 3 /// </summary> 4 /// <param name="sender">The source of the event.</param> 5 /// <param name="e">The <see cref="MouseEventArgs"/> instance containing the event data.</param> 6 private void CustomScrollbar_MouseDown(object sender, MouseEventArgs e) 7 { 8 Point ptPoint = this.PointToClient(Cursor.Position); 9 int nTrackHeight = (this.Height - btnHeight * 2); 10 float fThumbHeight = ((float)LargeChange / (float)Maximum) * nTrackHeight; 11 int nThumbHeight = (int)fThumbHeight; 12 13 if (nThumbHeight > nTrackHeight) 14 { 15 nThumbHeight = nTrackHeight; 16 fThumbHeight = nTrackHeight; 17 } 18 if (nThumbHeight < m_intThumbMinHeight) 19 { 20 nThumbHeight = m_intThumbMinHeight; 21 fThumbHeight = m_intThumbMinHeight; 22 } 23 24 int nTop = moThumbTop; 25 nTop += btnHeight; 26 27 28 Rectangle thumbrect = new Rectangle(new Point(1, nTop), new Size(this.Width - 2, nThumbHeight)); 29 if (thumbrect.Contains(ptPoint)) 30 { 31 32 //hit the thumb 33 nClickPoint = (ptPoint.Y - nTop); 34 //MessageBox.Show(Convert.ToString((ptPoint.Y - nTop))); 35 this.moThumbDown = true; 36 } 37 38 Rectangle uparrowrect = new Rectangle(new Point(1, 0), new Size(this.Width, btnHeight)); 39 if (uparrowrect.Contains(ptPoint)) 40 { 41 42 int nRealRange = (Maximum - Minimum) - LargeChange; 43 int nPixelRange = (nTrackHeight - nThumbHeight); 44 if (nRealRange > 0) 45 { 46 if (nPixelRange > 0) 47 { 48 if ((moThumbTop - SmallChange) < 0) 49 moThumbTop = 0; 50 else 51 moThumbTop -= SmallChange; 52 53 //figure out value 54 float fPerc = (float)moThumbTop / (float)nPixelRange; 55 float fValue = fPerc * (Maximum - LargeChange); 56 57 moValue = (int)fValue; 58 59 if (ValueChanged != null) 60 ValueChanged(this, new EventArgs()); 61 62 if (Scroll != null) 63 Scroll(this, new EventArgs()); 64 65 Invalidate(); 66 } 67 } 68 } 69 70 Rectangle downarrowrect = new Rectangle(new Point(1, btnHeight + nTrackHeight), new Size(this.Width, btnHeight)); 71 if (downarrowrect.Contains(ptPoint)) 72 { 73 int nRealRange = (Maximum - Minimum) - LargeChange; 74 int nPixelRange = (nTrackHeight - nThumbHeight); 75 if (nRealRange > 0) 76 { 77 if (nPixelRange > 0) 78 { 79 if ((moThumbTop + SmallChange) > nPixelRange) 80 moThumbTop = nPixelRange; 81 else 82 moThumbTop += SmallChange; 83 84 //figure out value 85 float fPerc = (float)moThumbTop / (float)nPixelRange; 86 float fValue = fPerc * (Maximum - LargeChange); 87 88 moValue = (int)fValue; 89 90 if (ValueChanged != null) 91 ValueChanged(this, new EventArgs()); 92 93 if (Scroll != null) 94 Scroll(this, new EventArgs()); 95 96 Invalidate(); 97 } 98 } 99 } 100 } 101 102 /// <summary> 103 /// Handles the MouseUp event of the CustomScrollbar control. 104 /// </summary> 105 /// <param name="sender">The source of the event.</param> 106 /// <param name="e">The <see cref="MouseEventArgs"/> instance containing the event data.</param> 107 private void CustomScrollbar_MouseUp(object sender, MouseEventArgs e) 108 { 109 this.moThumbDown = false; 110 this.moThumbDragging = false; 111 } 112 113 /// <summary> 114 /// Moves the thumb. 115 /// </summary> 116 /// <param name="y">The y.</param> 117 private void MoveThumb(int y) 118 { 119 int nRealRange = Maximum - Minimum; 120 int nTrackHeight = (this.Height - btnHeight * 2); 121 float fThumbHeight = ((float)LargeChange / (float)Maximum) * nTrackHeight; 122 int nThumbHeight = (int)fThumbHeight; 123 124 if (nThumbHeight > nTrackHeight) 125 { 126 nThumbHeight = nTrackHeight; 127 fThumbHeight = nTrackHeight; 128 } 129 if (nThumbHeight < m_intThumbMinHeight) 130 { 131 nThumbHeight = m_intThumbMinHeight; 132 fThumbHeight = m_intThumbMinHeight; 133 } 134 135 int nSpot = nClickPoint; 136 137 int nPixelRange = (nTrackHeight - nThumbHeight); 138 if (moThumbDown && nRealRange > 0) 139 { 140 if (nPixelRange > 0) 141 { 142 int nNewThumbTop = y - (btnHeight + nSpot); 143 144 if (nNewThumbTop < 0) 145 { 146 moThumbTop = nNewThumbTop = 0; 147 } 148 else if (nNewThumbTop > nPixelRange) 149 { 150 moThumbTop = nNewThumbTop = nPixelRange; 151 } 152 else 153 { 154 moThumbTop = y - (btnHeight + nSpot); 155 } 156 157 158 float fPerc = (float)moThumbTop / (float)nPixelRange; 159 float fValue = fPerc * (Maximum - LargeChange); 160 moValue = (int)fValue; 161 162 Application.DoEvents(); 163 164 Invalidate(); 165 } 166 } 167 } 168 169 /// <summary> 170 /// Handles the MouseMove event of the CustomScrollbar control. 171 /// </summary> 172 /// <param name="sender">The source of the event.</param> 173 /// <param name="e">The <see cref="MouseEventArgs"/> instance containing the event data.</param> 174 private void CustomScrollbar_MouseMove(object sender, MouseEventArgs e) 175 { 176 if (!moThumbDown) 177 return; 178 179 if (moThumbDown == true) 180 { 181 this.moThumbDragging = true; 182 } 183 184 if (this.moThumbDragging) 185 { 186 MoveThumb(e.Y); 187 } 188 189 if (ValueChanged != null