1. 程式人生 > >(八十九)c#Winform自定義控制元件-自定義滾動條(treeview、panel、datagridview、listbox、listview、textbox)

(八十九)c#Winform自定義控制元件-自定義滾動條(treeview、panel、datagridview、listbox、listview、textbox)

官網

http://www.hzhcontrols.com/

前提

入行已經7,8年了,一直想做一套漂亮點的自定義控制元件,於是就有了本系列文章。

GitHub:https://github.com/kwwwvagaa/NetWinformControl

碼雲:https://gitee.com/kwwwvagaa/net_winform_custom_control.git

如果覺得寫的還行,請點個 star 支援一下吧

歡迎前來交流探討: 企鵝群568015492 

來都來了,點個【推薦】再走吧,謝謝

NuGet

Install-Package HZH_Controls

目錄

http://www.hzhcontrols.com/blog-63.html

用處及效果

 

 準備工作

這個是在前面滾動條上完善的,加入了對常用控制元件滾動條的美化,實現邏輯是新增一個滾動條控制元件,然後覆蓋在滾動條上面,然後實現聯動

繪製滾動條的工作,你可以參考

(六十九)c#Winform自定義控制元件-垂直滾動條-HZHControls 

開始

新增一個元件ScrollbarComponent,

實現介面IExtenderProvider

 1   Dictionary<Control, bool> m_controlCache = new Dictionary<Control, bool>();
 2         public bool CanExtend(object extendee)
 3         {
 4             if (extendee is ScrollableControl)
 5             {
 6                 ScrollableControl control = (ScrollableControl)extendee;
 7                 if (control.AutoScroll == true)
 8                 {
 9                     return true;
10                 }
11             }
12             else if (extendee is TreeView)
13             {
14                 TreeView control = (TreeView)extendee;
15                 if (control.Scrollable)
16                 {
17                     return true;
18                 }
19             }
20             else if (extendee is TextBox)
21             {
22                 TextBox control = (TextBox)extendee;
23                 if (control.Multiline && control.ScrollBars != ScrollBars.None)
24                 {
25                     return true;
26                 }
27             }
28             else if (extendee is RichTextBox)
29             {
30                 return true;
31             }
32             else if (extendee is ListBox)
33             {
34                 return true;
35             }
36             //else if (extendee is ListView)
37             //{
38             //    return true;
39             //}
40             else if (extendee is DataGridView)
41             {
42                 return true;
43             }
44             return false;
45         }

新增擴充套件屬性

 1  [Browsable(true), Category("自定義屬性"), Description("是否使用自定義滾動條"), DisplayName("UserCustomScrollbar"), Localizable(true)]
 2         public bool GetUserCustomScrollbar(Control control)
 3         {
 4             if (m_controlCache.ContainsKey(control))
 5                 return m_controlCache[control];
 6             return true;
 7         }
 8 
 9         public void SetUserCustomScrollbar(Control control, bool blnUserCustomScrollbar)
10         {
11             m_controlCache[control] = blnUserCustomScrollbar;
12             if (!blnUserCustomScrollbar)
13                 return;
14 
15             if (control is TreeView)
16             {
17                 TreeView tv = (TreeView)control;
18                 //tv.MouseWheel += tv_MouseWheel;
19                 //tv.AfterSelect += tv_AfterSelect;
20                 tv.AfterExpand += tv_AfterExpand;
21                 tv.AfterCollapse += tv_AfterCollapse;
22             }
23             else if (control is TextBox)
24             {
25                 TextBox txt = (TextBox)control;
26                 //txt.MouseWheel += txt_MouseWheel;
27                 txt.TextChanged += txt_TextChanged;
28                 txt.KeyDown += txt_KeyDown;
29             }
30             else if (control is RichTextBox)
31             {
32                 RichTextBox txt = (RichTextBox)control;
33                 //txt.MouseWheel += txt_MouseWheel;
34                 txt.TextChanged += txt_TextChanged;
35                 txt.KeyDown += txt_KeyDown;
36             }
37             else if (control is ListBox)
38             {
39                 ListBox lb = (ListBox)control;
40                 lb.DataSourceChanged += Lb_DataSourceChanged;
41                 lb.SelectedIndexChanged += Lb_SelectedIndexChanged;                
42             }
43             //else if (control is ListView)
44             //{
45             //    ListView lv = (ListView)control;
46 
47             //}
48             else if (control is DataGridView)
49             {
50                 DataGridView dgv = (DataGridView)control;
51                 dgv.DataSourceChanged += dgv_DataSourceChanged;
52                 dgv.RowsAdded += dgv_RowsAdded;
53                 dgv.RowsRemoved += dgv_RowsRemoved;
54                 dgv.Scroll += dgv_Scroll;
55             }
56 
57             control.VisibleChanged += control_VisibleChanged;
58             control.SizeChanged += control_SizeChanged;
59             control.LocationChanged += control_LocationChanged;
60             control.Disposed += control_Disposed;
61             control.MouseWheel += Control_MouseWheel;
62             control_SizeChanged(control, null);
63         }

控制元件大小改變的時候處理滾動條顯示

  1 void control_SizeChanged(object sender, EventArgs e)
  2         {
  3             if (ControlHelper.IsDesignMode())
  4             {
  5                 return;
  6             }
  7             else
  8             {
  9                 var control = sender as Control;
 10 
 11                 bool blnHasVScrollbar = control.IsHandleCreated && (ControlHelper.GetWindowLong(control.Handle, STYLE) & VSCROLL) != 0;
 12                 bool blnHasHScrollbar = control.IsHandleCreated && (ControlHelper.GetWindowLong(control.Handle, STYLE) & HSCROLL) != 0;
 13                 if (control is TextBox)
 14                 {
 15                     var txt = (TextBox)control;
 16                     if (txt.ScrollBars == ScrollBars.Both)
 17                     {
 18                         blnHasVScrollbar = true;
 19                         blnHasHScrollbar = true;
 20                     }
 21                     else if (txt.ScrollBars == ScrollBars.Vertical)
 22                     {
 23                         blnHasVScrollbar = true;
 24                         blnHasHScrollbar = false;
 25                     }
 26                     else if (txt.ScrollBars == ScrollBars.Horizontal)
 27                     {
 28                         blnHasVScrollbar = false;
 29                         blnHasHScrollbar = true;
 30                     }
 31                     else
 32                     {
 33                         blnHasVScrollbar = false;
 34                         blnHasHScrollbar = false;
 35                     }
 36                 }
 37                 else if (control is DataGridView)
 38                 {
 39                     var dgv = (DataGridView)control;
 40                     if (dgv.ScrollBars == ScrollBars.Both || dgv.ScrollBars == ScrollBars.Vertical)
 41                     {
 42                         int _height = dgv.RowTemplate.Height * dgv.Rows.Count;
 43                         if (dgv.ColumnHeadersVisible)
 44                         {
 45                             _height += dgv.ColumnHeadersHeight;
 46                         }
 47                         blnHasVScrollbar = _height > dgv.Height;
 48                     }
 49                     if (dgv.ScrollBars == ScrollBars.Both || dgv.ScrollBars == ScrollBars.Horizontal)
 50                     {
 51                         int _width = 0;
 52                         foreach (DataGridViewColumn com in dgv.Columns)
 53                         {
 54                             _width += com.Width;
 55                         }
 56                         if (dgv.RowHeadersVisible)
 57                         {
 58                             _width += dgv.RowHeadersWidth;
 59                         }
 60                         blnHasHScrollbar = _width > dgv.Width;
 61                     }
 62                 }
 63                 else if (control is ListView)
 64                 {
 65                     if (!((ListView)control).Scrollable)
 66                     {
 67                         blnHasVScrollbar = false;
 68                         blnHasHScrollbar = false;
 69                     }
 70                 }
 71                 if (blnHasVScrollbar)
 72                 {
 73                     if (!m_lstVCache.ContainsKey(control))
 74                     {
 75                         if (control.Parent != null)
 76                         {
 77                             UCVScrollbar barV = new UCVScrollbar();
 78                             barV.SmallChange = 5;
 79                             barV.Width = SystemInformation.VerticalScrollBarWidth + 1;
 80                             barV.Scroll += barV_Scroll;
 81                             m_lstVCache[control] = barV;
 82                             if (blnHasHScrollbar)
 83                             {
 84                                 barV.Height = control.Height - barV.Width;
 85                             }
 86                             else
 87                             {
 88                                 barV.Height = control.Height;
 89                             }
 90                             SetVMaxNum(control);
 91                             barV.Location = new System.Drawing.Point(control.Right - barV.Width, control.Top);
 92                             control.Parent.Controls.Add(barV);
 93                             int intControlIndex = control.Parent.Controls.GetChildIndex(control);
 94                             control.Parent.Controls.SetChildIndex(barV, intControlIndex);
 95                         }
 96                     }
 97                     else
 98                     {
 99                         if (blnHasHScrollbar)
100                         {
101                             m_lstVCache[control].Height = control.Height - m_lstVCache[control].Width;
102                         }
103                         else
104                         {
105                             m_lstVCache[control].Height = control.Height;
106                         }
107                         SetVMaxNum(control);
108                     }
109                 }
110                 else
111                 {
112                     if (m_lstVCache.ContainsKey(control) && m_lstVCache[control].Parent != null)
113                     {
114                         m_lstVCache[control].Visible = false;
115                     }
116                 }
117 
118                 if (blnHasHScrollbar)
119                 {
120                     if (!m_lstHCache.ContainsKey(control))
121                     {
122                         if (control.Parent != null)
123                         {
124                             UCHScrollbar barH = new UCHScrollbar();
125                             barH.Height = SystemInformation.HorizontalScrollBarHeight + 1;
126                             barH.SmallChange = 5;
127 
128                             barH.Scroll += barH_Scroll;
129                             m_lstHCache[control] = barH;
130                             if (blnHasHScrollbar)
131                             {
132                                 barH.Width = control.Width - barH.Height;
133                             }
134                             else
135                             {
136                                 barH.Width = control.Width;
137                             }
138                             SetHMaxNum(control);
139                             barH.Location = new System.Drawing.Point(control.Left, control.Bottom - barH.Height);
140                             control.Parent.Controls.Add(barH);
141                             int intControlIndex = control.Parent.Controls.GetChildIndex(control);
142                             control.Parent.Controls.SetChildIndex(barH, intControlIndex);
143                         }
144                     }
145                     else
146                     {
147                         if (blnHasHScrollbar)
148                         {
149                             m_lstHCache[control].Width = control.Width - m_lstHCache[control].Height;
150                         }
151                         else
152                         {
153                             m_lstHCache[control].Width = control.Width;
154                         }
155                         SetHMaxNum(control);
156                     }
157                 }
158                 else
159                 {
160                     if (m_lstHCache.ContainsKey(control))
161                     {
162                         if (m_lstHCache[control].Visible && m_lstHCache[control].Parent != null)
163                         {
164                             m_lstHCache[control].Visible = false;
165                         }
166                     }
167                 }
168             }
169             ResetScrollLocation(sender);
170         }

修改橫豎滾動條值

  1  private void SetVMaxNum(Control control)
  2         {
  3             if (!m_lstVCache.ContainsKey(control))
  4                 return;
  5             var intoV = ControlHelper.GetVScrollBarInfo(control.Handle);
  6             UCVScrollbar barV = m_lstVCache[control];
  7             if (control is ScrollableControl)
  8             {
  9                 barV.Maximum = intoV.ScrollMax;
 10                 barV.Visible = intoV.ScrollMax > 0 && intoV.nMax > 0 && intoV.nPage > 0;
 11                 barV.Value = intoV.nPos;
 12                 barV.BringToFront();
 13                 // barV.Maximum = (control as ScrollableControl).VerticalScroll.Maximum;
 14                 // barV.Value = (control as ScrollableControl).VerticalScroll.Value;
 15             }
 16             else if (control is TreeView)
 17             {
 18                 var tv = control as TreeView;
 19                 barV.Maximum = intoV.ScrollMax * tv.ItemHeight;
 20                 barV.Visible = intoV.ScrollMax > 0 && intoV.nMax > 0 && intoV.nPage > 0;
 21                 barV.Value = intoV.nPos * tv.ItemHeight;
 22                 barV.BringToFront();
 23 
 24                 //barV.Maximum = GetTreeNodeMaxY(control as TreeView) - control.Height;
 25                 //barV.Value = (control as TreeView).AutoScrollOffset.Y;
 26             }
 27             else if (control is TextBox)
 28             {
 29                 TextBox txt = (TextBox)control;
 30                 barV.Maximum = intoV.ScrollMax * txt.PreferredHeight;
 31                 if (txt.ScrollBars == ScrollBars.Both || txt.ScrollBars == ScrollBars.Vertical)
 32                 {
 33                     barV.Visible = true;
 34                 }
 35                 else
 36                 {
 37                     barV.Visible = false;
 38                 }
 39                 barV.Value = intoV.nPos * txt.PreferredHeight;
 40                 barV.BringToFront();
 41             }
 42             else if (control is RichTextBox)
 43             {
 44                 bool blnHasVScrollbar = control.IsHandleCreated && (ControlHelper.GetWindowLong(control.Handle, STYLE) & VSCROLL) != 0;
 45                 barV.Maximum = intoV.ScrollMax;
 46                 barV.Visible = blnHasVScrollbar;
 47                 barV.Value = intoV.nPos;
 48                 barV.BringToFront();
 49             }
 50             else if (control is ListBox)
 51             {
 52                 var lb = control as ListBox;
 53                 if (intoV.ScrollMax <= 1)
 54                 {
 55                     var v = lb.ItemHeight * lb.Items.Count - lb.Height;
 56                     if (v > 0)
 57                         barV.Maximum = v;
 58                     else
 59                         barV.Maximum = intoV.ScrollMax * lb.ItemHeight;
 60                 }
 61                 else
 62                 {
 63                     barV.Maximum = intoV.ScrollMax * lb.ItemHeight;
 64                 }
 65                 barV.Visible = intoV.ScrollMax > 0 && intoV.nMax > 0 && intoV.nPage > 0;
 66                 barV.Value = intoV.nPos * lb.ItemHeight;
 67                 barV.BringToFront();
 68             }
 69             else if (control is ListView)
 70             {
 71                 barV.Maximum = intoV.ScrollMax;
 72                 barV.Visible = intoV.ScrollMax > 0 && intoV.nMax > 0 && intoV.nPage > 0;
 73                 barV.Value = intoV.nPos;
 74                 barV.BringToFront();
 75             }
 76             else if (control is DataGridView)
 77             {
 78                 bool blnHasVScrollbar = false;
 79                 var dgv = (DataGridView)control;
 80                 if (dgv.ScrollBars == ScrollBars.Both || dgv.ScrollBars == ScrollBars.Vertical)
 81                 {
 82                     int _height = dgv.RowTemplate.Height * dgv.Rows.Count;
 83                     if (dgv.ColumnHeadersVisible)
 84                     {
 85                         _height += dgv.ColumnHeadersHeight;
 86                     }
 87                     blnHasVScrollbar = _height > dgv.Height;
 88                 }
 89                 barV.Maximum = dgv.Rows.Count;
 90                 barV.Visible = blnHasVScrollbar;
 91                 barV.Value = dgv.FirstDisplayedScrollingRowIndex; ;
 92                 barV.BringToFront();
 93             }
 94         }
 95         private void SetHMaxNum(Control control)
 96         {
 97             if (!m_lstHCache.ContainsKey(control))
 98                 return;
 99             var intoH = ControlHelper.GetHScrollBarInfo(control.Handle);
100             UCHScrollbar barH = m_lstHCache[control];
101             if (control is ScrollableControl)
102             {
103                 barH.Maximum = intoH.ScrollMax;
104                 barH.Visible = intoH.ScrollMax > 0 && intoH.nMax > 0 && intoH.nPage > 0;
105                 barH.Value = intoH.nPos;
106                 barH.BringToFront();
107 
108                 //barH.Maximum = (control as ScrollableControl).HorizontalScroll.Maximum;
109                 //barH.Value = (control as ScrollableControl).HorizontalScroll.Value;
110             }
111             else if (control is TreeView)
112             {
113                 var tv = control as TreeView;
114                 barH.Maximum = intoH.ScrollMax;
115                 barH.Visible = intoH.ScrollMax > 0 && intoH.nMax > 0 && intoH.nPage > 0;
116                 barH.Value = intoH.nPos;
117                 barH.BringToFront();
118                 //barH.Maximum = GetTreeNodeMaxX(control as TreeView);
119                 //barH.Value = (control as TreeView).AutoScrollOffset.X;
120             }
121             else if (control is TextBox)
122             {
123                 TextBox txt = (TextBox)control;
124                 barH.Maximum = intoH.ScrollMax;
125 
126                 if (txt.ScrollBars == ScrollBars.Both || txt.ScrollBars == ScrollBars.Horizontal)
127                 {
128                     barH.Visible = true;
129                 }
130                 else
131                 {
132                     barH.Visible = false;
133                 }
134 
135                 barH.Value = intoH.nPos;
136                 barH.BringToFront();
137             }
138             else if (control is RichTextBox)
139             {
140                 bool blnHasHScrollbar = control.IsHandleCreated && (ControlHelper.GetWindowLong(control.Handle, STYLE) & HSCROLL) != 0;
141 
142                 barH.Maximum = intoH.ScrollMax;
143                 barH.Visible = blnHasHScrollbar;
144                 barH.Value = intoH.nPos;
145                 barH.BringToFront();
146             }
147             else if (control is ListBox)
148             {
149                 var lb = control as ListBox;
150                 barH.Maximum = intoH.ScrollMax * lb.ItemHeight;
151                 barH.Visible = intoH.ScrollMax > 0 && intoH.nMax > 0 && intoH.nPage > 0;
152                 barH.Value = intoH.nPos * lb.ItemHeight;
153                 barH.BringToFront();
154             }
155             else if (control is ListView)
156             {
157                 barH.Maximum = intoH.ScrollMax;
158                 barH.Visible = intoH.ScrollMax > 0 && intoH.nMax > 0 && intoH.nPage > 0;
159                 barH.Value = intoH.nPos;
160                 barH.BringToFront();
161             }
162             else if (control is DataGridView)
163             {
164                 bool blnHasHScrollbar = false;
165                 var dgv = (DataGridView)control;
166                 int _width = 0;
167                 if (dgv.ScrollBars == ScrollBars.Both || dgv.ScrollBars == ScrollBars.Horizontal)
168                 {
169                     foreach (DataGridViewColumn com in dgv.Columns)
170                     {
171                         _width += com.Width;
172                     }
173                     if (dgv.RowHeadersVisible)
174                     {
175                         _width += dgv.RowHeadersWidth;
176                     }
177                     blnHasHScrollbar = _width > dgv.Width;
178                 }
179                 if (blnHasHScrollbar)
180                     barH.Maximum = _width - dgv.Width;
181                 barH.Visible = blnHasHScrollbar;
182                 barH.Value = dgv.FirstDisplayedScrollingColumnHiddenWidth;
183                 barH.BringToFront();
184             }
185         }

位置改變時候修改滾動條

 1   private void ResetScrollLocation(object sender)
 2         {
 3             Control control = (Control)sender;
 4             bool blnHasVScrollbar = control.IsHandleCreated && (ControlHelper.GetWindowLong(control.Handle, STYLE) & VSCROLL) != 0;
 5             bool blnHasHScrollbar = control.IsHandleCreated && (ControlHelper.GetWindowLong(control.Handle, STYLE) & HSCROLL) != 0;
 6             if (control is TextBox)
 7             {
 8                 var txt = (TextBox)control;
 9                 if (txt.ScrollBars == ScrollBars.Both)
10                 {
11                     blnHasVScrollbar = true;
12                     blnHasHScrollbar = true;
13                 }
14                 else if (txt.ScrollBars == ScrollBars.Vertical)
15                 {
16                     blnHasVScrollbar = true;
17                     blnHasHScrollbar = false;
18                 }
19                 else if (txt.ScrollBars == ScrollBars.Horizontal)
20                 {
21                     blnHasVScrollbar = false;
22                     blnHasHScrollbar = true;
23                 }
24                 else
25                 {
26                     blnHasVScrollbar = false;
27                     blnHasHScrollbar = false;
28                 }
29             }
30             else if (control is DataGridView)
31             {
32                 var dgv = (DataGridView)control;
33                 if (dgv.ScrollBars == ScrollBars.Both || dgv.ScrollBars == ScrollBars.Vertical)
34                 {
35                     int _height = dgv.RowTemplate.Height * dgv.Rows.Count;
36                     if (dgv.ColumnHeadersVisible)
37                     {
38                         _height += dgv.ColumnHeadersHeight;
39                     }
40                     blnHasVScrollbar = _height > dgv.Height;
41                 }
42                 if (dgv.ScrollBars == ScrollBars.Both || dgv.ScrollBars == ScrollBars.Horizontal)
43                 {
44                     int _width = 0;
45                     foreach (DataGridViewColumn com in dgv.Columns)
46                     {
47                         _width += com.Width;
48                     }
49                     if (dgv.RowHeadersVisible)
50                     {
51                         _width += dgv.RowHeadersWidth;
52                     }
53                     blnHasHScrollbar = _width > dgv.Width;
54                 }
55             }
56             else if (control is ListView)
57             {
58                 if (!((ListView)control).Scrollable)
59                 {
60                     blnHasVScrollbar = false;
61                     blnHasHScrollbar = false;
62                 }
63             }
64             if (control.Visible)
65             {
66                 if (m_lstVCache.ContainsKey(control))
67                 {
68                     m_lstVCache[control].Location = new System.Drawing.Point(control.Right - m_lstVCache[control].Width, control.Top);
69                     if (blnHasHScrollbar)
70                     {
71                         m_lstVCache[control].Height = control.Height - m_lstHCache[control].Height;
72                     }
73                     else
74                     {
75                         m_lstVCache[control].Height = control.Height;
76                     }
77                     SetVMaxNum(control);
78                 }
79 
80                 if (m_lstHCache.ContainsKey(control))
81                 {
82                     m_lstHCache[control].Location = new System.Drawing.Point(control.Left, control.Bottom - m_lstHCache[control].Height);
83                     //if (blnHasVScrollbar)
84                     //{
85                     //    m_lstHCache[control].Width = control.Width - m_lstVCache[control].Width;
86                     //}
87                     //else
88                     //{
89                     m_lstHCache[control].Width = control.Width;
90                     //}
91                     SetHMaxNum(control);
92                 }
93             }
94         }

滑動滾動條的處理滾動

 1  void barV_Scroll(object sender, EventArgs e)
 2         {
 3             UCVScrollbar bar = (UCVScrollbar)sender;
 4             if (m_lstVCache.ContainsValue(bar))
 5             {
 6                 Control c = m_lstVCache.FirstOrDefault(p => p.Value == bar).Key;
 7 
 8                 //ControlHelper.SetVScrollValue(c.Handle, bar.Value);
 9                 if (c is ScrollableControl)
10                 {
11                     (c as ScrollableControl).AutoScrollPosition = new Point((c as ScrollableControl).AutoScrollPosition.X, bar.Value);
12                 }
13                 else if (c is TreeView)
14                 {
15                     ControlHelper.SetVScrollValue(c.Handle, bar.Value / ((c as TreeView).ItemHeight));
16                 }
17                 else if (c is TextBox)
18                 {
19                     ControlHelper.SetVScrollValue(c.Handle, bar.Value / ((c as TextBox).PreferredHeight));
20                 }
21                 else if (c is RichTextBox)
22                 {
23                     ControlHelper.SetVScrollValue(c.Handle, bar.Value);
24                 }
25                 else if (c is ListBox)
26                 {
27                     ControlHelper.SetVScrollValue(c.Handle, bar.Value / ((c as ListBox).ItemHeight));
28                 }
29                 else if (c is ListView)
30                 {
31                     ControlHelper.SetVScrollValue(c.Handle, bar.Value);
32                 }
33                 else if (c is DataGridView)
34                 {
35                     var dgv = (DataGridView)c;
36                     if(bar.Value>0)
37                     dgv.FirstDisplayedScrollingRowIndex = bar.Value - 1;
38                 }
39             }
40         }
41 
42         void barH_Scroll(object sender, EventArgs e)
43         {
44             UCHScrollbar bar = (UCHScrollbar)sender;
45             if (m_lstHCache.ContainsValue(bar))
46             {
47                 Control c = m_lstHCache.FirstOrDefault(p => p.Value == bar).Key;
48                 if (c is ScrollableControl)
49                 {
50                     (c as ScrollableControl).AutoScrollPosition = new Point(bar.Value, (c as ScrollableControl).AutoScrollPosition.Y);
51                 }
52                 else if (c is TreeView)
53                 {
54                     ControlHelper.SetHScrollValue(c.Handle, bar.Value);
55                     //TreeView tv = (c as TreeView);
56                     //SetTreeViewVScrollLocation(tv, tv.Nodes, bar.Value);
57                 }
58                 else if (c is TextBox)
59                 {
60                     ControlHelper.SetHScrollValue(c.Handle, bar.Value);
61                     //TextBox txt = (c as TextBox);
62                     //SetTextBoxVScrollLocation(txt, bar.Value);
63                 }
64                 else if (c is RichTextBox)
65                 {
66                     ControlHelper.SetHScrollValue(c.Handle, bar.Value);
67                 }
68                 else if (c is ListBox)
69                 {
70                     ControlHelper.SetHScrollValue(c.Handle, bar.Value);
71                 }
72                 else if (c is ListView)
73                 {
74                     ControlHelper.SetHScrollValue(c.Handle, bar.Value);
75                 }
76                 else if (c is DataGridView)
77                 {
78                     var dgv = (DataGridView)c;
79                     dgv.HorizontalScrollingOffset = bar.Value;
80                 }
81             }
82         }

以下是全部程式碼

  1 using System;
  2 using System.Collections.Generic;
  3 using System.ComponentModel;
  4 using System.Drawing;
  5 using System.Linq;
  6 using System.Text;
  7 using System.Windows.Forms;
  8 
  9 namespace HZH_Controls.Controls
 10 {
 11     [ProvideProperty("UserCustomScrollbar", typeof(Control))]
 12     public class ScrollbarComponent : Component, IExtenderProvider
 13     {
 14         public ScrollbarComponent()
 15         {
 16 
 17         }
 18 
 19         public ScrollbarComponent(IContainer container)
 20         {
 21             container.Add(this);
 22         }
 23 
 24         Dictionary<Control, bool> m_controlCache = new Dictionary<Control, bool>();
 25         public bool CanExtend(object extendee)
 26         {
 27             if (extendee is ScrollableControl)
 28             {
 29                 ScrollableControl control = (ScrollableControl)extendee;
 30                 if (control.AutoScroll == true)
 31                 {
 32                     return true;
 33                 }
 34             }
 35             else if (extendee is TreeView)
 36             {
 37                 TreeView control = (TreeView)extendee;
 38                 if (control.Scrollable)
 39                 {
 40                     return true;
 41                 }
 42             }
 43             else if (extendee is TextBox)
 44             {
 45                 TextBox control = (TextBox)extendee;
 46                 if (control.Multiline && control.ScrollBars != ScrollBars.None)
 47                 {
 48                     return true;
 49                 }
 50             }
 51             else if (extendee is RichTextBox)
 52             {
 53                 return true;
 54             }
 55             else if (extendee is ListBox)
 56             {
 57                 return true;
 58             }
 59             //else if (extendee is ListView)
 60             //{
 61             //    return true;
 62             //}
 63             else if (extendee is DataGridView)
 64             {
 65                 return true;
 66             }
 67             return false;
 68         }
 69 
 70         [Browsable(true), Category("自定義屬性"), Description("是否使用自定義滾動條"), DisplayName("UserCustomScrollbar"), Localizable(true)]
 71         public bool GetUserCustomScrollbar(Control control)
 72         {
 73             if (m_controlCache.ContainsKey(control))
 74                 return m_controlCache[control];
 75             return true;
 76         }
 77 
 78         public void SetUserCustomScrollbar(Control control, bool blnUserCustomScrollbar)
 79         {
 80             m_controlCache[control] = blnUserCustomScrollbar;
 81             if (!blnUserCustomScrollbar)
 82                 return;
 83 
 84             if (control is TreeView)
 85             {
 86                 TreeView tv = (TreeView)control;
 87                 //tv.MouseWheel += tv_MouseWheel;
 88                 //tv.AfterSelect += tv_AfterSelect;
 89                 tv.AfterExpand += tv_AfterExpand;
 90                 tv.AfterCollapse += tv_AfterCollapse;
 91             }
 92             else if (control is TextBox)
 93             {
 94                 TextBox txt = (TextBox)control;
 95                 //txt.MouseWheel += txt_MouseWheel;
 96                 txt.TextChanged += txt_TextChanged;
 97                 txt.KeyDown += txt_KeyDown;
 98             }
 99             else if (control is RichTextBox)
100             {
101                 RichTextBox txt = (RichTextBox)control;
102                 //txt.MouseWheel += txt_MouseWheel;
103                 txt.TextChanged += txt_TextChanged;
104                 txt.KeyDown += txt_KeyDown;
105             }
106             else if (control is ListBox)
107             {
108                 ListBox lb = (ListBox)control;
109                 lb.DataSourceChanged += Lb_DataSourceChanged;
110                 lb.SelectedIndexChanged += Lb_SelectedIndexChanged;                
111             }
112             //else if (control is ListView)
113             //{
114             //    ListView lv = (ListView)control;
115 
116             //}
117             else if (control is DataGridView)
118             {
119                 DataGridView dgv = (DataGridView)control;
120                 dgv.DataSourceChanged += dgv_DataSourceChanged;
121                 dgv.RowsAdded += dgv_RowsAdded;
122                 dgv.RowsRemoved += dgv_RowsRemoved;
123                 dgv.Scroll += dgv_Scroll;
124             }
125 
126             control.VisibleChanged += control_VisibleChanged;
127             control.SizeChanged += control_SizeChanged;
128             control.LocationChanged += control_LocationChanged;
129             control.Disposed += control_Disposed;
130             control.MouseWheel += Control_MouseWheel;
131             control_SizeChanged(control, null);
132         }
133 
134         void dgv_Scroll(object sender, ScrollEventArgs e)
135         {
136             if (e.NewValue == e.OldValue)
137                 return;
138             DataGridView dgv = (DataGridView)sender;
139             if (e.ScrollOrientation == ScrollOrientation.HorizontalScroll)
140             {
141                 if (m_lstHCache.ContainsKey(dgv))
142                 {
143                     m_lstHCache[dgv].Value = e.NewValue;
144                 }
145             }
146             else if (e.ScrollOrientation == ScrollOrientation.VerticalScroll)
147             {
148                 if (m_lstVCache.ContainsKey(dgv))
149                 {
150                     m_lstVCache[dgv].Value = e.NewValue;
151                 }
152             }
153         }
154 
155         void dgv_RowsRemoved(object sender, DataGridViewRowsRemovedEventArgs e)
156         {
157             Control control = sender as Control;
158             control_SizeChanged(control, null);
159         }
160 
161         void dgv_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
162         {
163             Control control = sender as Control;
164             control_SizeChanged(control, null);
165         }
166 
167         void dgv_DataSourceChanged(object sender, EventArgs e)
168         {
169             Control control = sender as Control;
170             control_SizeChanged(control, null);
171         }
172 
173         private void Lb_SelectedIndexChanged(object sender, EventArgs e)
174         {
175             Control c = sender as Control;
176             SetVMaxNum(c);
177         }
178 
179         private void Lb_DataSourceChanged(object sender, EventArgs e)
180         {
181             Control c = sender as Control;
182             control_SizeChanged(c, null);
183         }
184 
185         private void Control_MouseWheel(object sender, MouseEventArgs e)
186         {
187             Control c = (Control)sender;
188             if (m_lstVCache.ContainsKey(c))
189             {
190                 if (e.Delta > 5)
191                 {
192                     ControlHelper.ScrollUp(c.Handle);
193                 }
194                 else if (e.Delta < -5)
195                 {
196                     ControlHelper.ScrollDown(c.Handle);
197                 }
198 
199                 SetVMaxNum(c);
200             }
201             else if (m_lstHCache.ContainsKey(c))
202             {
203                 if (e.Delta > 5)
204                 {
205                     ControlHelper.ScrollLeft(c.Handle);
206                 }
207                 else if (e.Delta < -5)
208                 {
209                     ControlHelper.ScrollRight(c.Handle);
210                 }
211 
212                 SetHMaxNum(c);
213             }
214         }
215 
216         void control_Disposed(object sender, EventArgs e)
217         {
218             Control control = (Control)sender;
219             if (m_lstVCache.ContainsKey(control) && m_lstVCache[control].Parent != null)
220             {
221                 m_lstVCache[control].Parent.Controls.Remove(m_lstVCache[control]);
222                 m_lstVCache.Remove(control);
223             }
224         }
225 
226         void control_LocationChanged(object sender, EventArgs e)
227         {
228             ResetScrollLocation(sender);
229         }
230 
231         void control_SizeChanged(object sender, EventArgs e)
232         {
233             if (ControlHelper.IsDesignMode())
234             {
235                 return;
236             }
237             else
238             {
239                 var control = sender as Control;
240 
241                 bool blnHasVScrollbar = control.IsHandleCreated && (ControlHelper.GetWindowLong(control.Handle, STYLE) & VSCROLL) != 0;
242                 bool blnHasHScrollbar = control.IsHandleCreated && (ControlHelper.GetWindowLong(control.Handle, STYLE) & HSCROLL) != 0;
243                 if (control is TextBox)
244                 {
245                     var txt = (TextBox)control;
246                     if (txt.ScrollBars == ScrollBars.Both)
247                     {
248                         blnHasVScrollbar = true;
249                         blnHasHScrollbar = true;
250                     }
251                     else if (txt.ScrollBars == ScrollBars.Vertical)
252                     {
253                         blnHasVScrollbar = true;
254                         blnHasHScrollbar = false;
255                     }
256                     else if (txt.ScrollBars == ScrollBars.Horizontal)
257                     {
258                         blnHasVScrollbar = false;
259                         blnHasHScrollbar = true;
260                     }
261                     else
262                     {
263                         blnHasVScrollbar = false;
264                         blnHasHScrollbar = false;
265                     }
266                 }
267                 else if (control is DataGridView)
268                 {
269                     var dgv = (DataGridView)control;
270                     if (dgv.ScrollBars == ScrollBars.Both || dgv.ScrollBars == ScrollBars.Vertical)
271                     {
272                         int _height = dgv.RowTemplate.Height * dgv.Rows.Count;
273                         if (dgv.ColumnHeadersVisible)
274                         {
275                             _height += dgv.ColumnHeadersHeight;
276                         }
277                         blnHasVScrollbar = _height > dgv.Height;
278                     }
279                     if (dgv.ScrollBars == ScrollBars.Both || dgv.ScrollBars == ScrollBars.Horizontal)
280                     {
281                         int _width = 0;
282                         foreach (DataGridViewColumn com in dgv.Columns)
283                         {
284                             _width += com.Width;
285                         }
286                         if (dgv.RowHeadersVisible)
287                         {
288                             _width += dgv.RowHeadersWidth;
289                         }
290                         blnHasHScrollbar = _width > dgv.Width;
291                     }
292                 }
293                 else if (control is ListView)
294                 {
295                     if (!((ListView)control).Scrollable)
296                     {
297                         blnHasVScrollbar = false;
298                         blnHasHScrollbar = false;
299                     }
300                 }
301                 if (blnHasVScrollbar)
302                 {
303                     if (!m_lstVCache.ContainsKey(control))
304                     {
305                         if (control.Parent != null)
306                         {
307                             UCVScrollbar barV = new UCVScrollbar();
308                             barV.SmallChange = 5;
309                             barV.Width = SystemInformation.VerticalScrollBarWidth + 1;
310                             barV.Scroll += barV_Scroll;
311                             m_lstVCache[control] = barV;
312                             if (blnHasHScrollbar)
313                             {
314                                 barV.Height = control.Height - barV.Width;
315                             }
316                             else
317                             {
318                                 barV.Height = control.Height;
319                             }
320                             SetVMaxNum(control);
321                             barV.Location = new System.Drawing.Point(control.Right - barV.Width, control.Top);
322                             control.Parent.Controls.Add(barV);
323                             int intControlIndex = control.Parent.Controls.GetChildIndex(control);
324                             control.Parent.Controls.SetChildIndex(barV, intControlIndex);
325                         }
326                     }
327                     else
328                     {
329                         if (blnHasHScrollbar)
330                         {
331                             m_lstVCache[control].Height = control.Height - m_lstVCache[control].Width;
332                         }
333                         else
334                         {
335                             m_lstVCache[control].Height = control.Height;
336                         }
337                         SetVMaxNum(control);
338                     }
339                 }
340                 else
341                 {
342                     if (m_lstVCache.ContainsKey(control) && m_lstVCache[control].Parent != null)
343                     {
344                         m_lstVCache[control].Visible = false;
345                     }
346                 }
347 
348                 if (blnHasHScrollbar)
349                 {
350                     if (!m_lstHCache.ContainsKey(control))
351                     {
352                         if (control.Parent != null)
353                         {
354                             UCHScrollbar barH = new UCHScrollbar();
355                             barH.Height = SystemInformation.HorizontalScrollBarHeight + 1;
356                             barH.SmallChange = 5;
357 
358                             barH.Scroll += barH_Scroll;
359                             m_lstHCache[control] = barH;
360                             if (blnHasHScrollbar)
361                             {
362                                 barH.Width = control.Width - barH.Height;
363                             }
364                             else
365                             {
366                                 barH.Width = control.Width;
367                             }
368                             SetHMaxNum(control);
369                             barH.Location = new System.Drawing.Point(control.Left, control.Bottom - barH.Height);
370                             control.Parent.Controls.Add(barH);
371                             int intControlIndex = control.Parent.Controls.GetChildIndex(control);
372                             control.Parent.Controls.SetChildIndex(barH, intControlIndex);
373                         }
374                     }
375                     else
376                     {
377                         if (blnHasHScrollbar)
378                         {
379                             m_lstHCache[control].Width = control.Width - m_lstHCache[control].Height;
380                         }
381                         else
382                         {
383                             m_lstHCache[control].Width = control.Width;
384                         }
385                         SetHMaxNum(control);
386                     }
387                 }
388                 else
389                 {
390                     if (m_lstHCache.ContainsKey(control))
391                     {
392                         if (m_lstHCache[control].Visible && m_lstHCache[control].Parent != null)
393                         {
394                             m_lstHCache[control].Visible = false;
395                         }
396                     }
397                 }
398             }
399             ResetScrollLocation(sender);
400         }
401 
402 
403         private void SetVMaxNum(Control control)
404         {
405             if (!m_lstVCache.ContainsKey(control))
406                 return;
407             var intoV = ControlHelper.GetVScrollBarInfo(control.Handle);
408             UCVScrollbar barV = m_lstVCache[control];
409             if (control is ScrollableControl)
410             {
411                 barV.Maximum = intoV.ScrollMax;
412                 barV.Visible = intoV.ScrollMax > 0 && intoV.nMax > 0 && intoV.nPage > 0;
413                 barV.Value = intoV.nPos;
414                 barV.BringToFront();
415                 // barV.Maximum = (control as ScrollableControl).VerticalScroll.Maximum;
416                 // barV.Value = (control as ScrollableControl).VerticalScroll.Value;
417             }
418             else if (control is TreeView)
419             {
420                 var tv = control as TreeView;
421                 barV.Maximum = intoV.ScrollMax * tv.ItemHeight;
422                 barV.Visible = intoV.ScrollMax > 0 && intoV.nMax > 0 && intoV.nPage > 0;
423                 barV.Value = intoV.nPos * tv.ItemHeight;
424                 barV.BringToFront();
425 
426                 //barV.Maximum = GetTreeNodeMaxY(control as TreeView) - control.Height;
427                 //barV.Value = (control as TreeView).AutoScrollOffset.Y;
428             }
429             else if (control is TextBox)
430             {
431                 TextBox txt = (TextBox)control;
432                 barV.Maximum = intoV.ScrollMax * txt.PreferredHeight;
433                 if (txt.ScrollBars == ScrollBars.Both || txt.ScrollBars == ScrollBars.Vertical)
434                 {
435                     barV.Visible = true;
436                 }
437                 else
438                 {
439                     barV.Visible = false;
440                 }
441                 barV.Value = intoV.nPos * txt.PreferredHeight;
442                 barV.BringToFront();
443             }
444             else if (control is RichTextBox)
445             {
446                 bool blnHasVScrollbar = control.IsHandleCreated && (ControlHelper.GetWindowLong(control.Handle, STYLE) & VSCROLL) != 0;
447                 barV.Maximum = intoV.ScrollMax;
448                 barV.Visible = blnHasVScrollbar;
449                 barV.Value = intoV.nPos;
450                 barV.BringToFront();
451             }
452             else if (control is ListBox)
453             {
454                 var lb = control as ListBox;
455                 if (intoV.ScrollMax <= 1)
456                 {
457                     var v = lb.ItemHeight * lb.Items.Count - lb.Height;
458                     if (v > 0)
459                         barV.Maximum = v;
460                     else
461                         barV.Maximum = intoV.ScrollMax * lb.ItemHeight;
462                 }
463                 else
464                 {
465                     barV.Maximum = intoV.ScrollMax * lb.ItemHeight;
466                 }
467                 barV.Visible = intoV.ScrollMax > 0 && intoV.nMax > 0 && intoV.nPage > 0;
468                 barV.Value = intoV.nPos * lb.ItemHeight;
469                 barV.BringToFront();
470             }
471             else if (control is ListView)
472             {
473                 barV.Maximum = intoV.ScrollMax;
474                 barV.Visible = intoV.ScrollMax > 0 && intoV.nMax > 0 && intoV.nPage > 0;
475                 barV.Value = intoV.nPos;
476                 barV.BringToFront();
477             }
478             else if (control is DataGridView)
479             {
480                 bool blnHasVScrollbar = false;
481                 var dgv = (DataGridView)control;
482                 if (dgv.ScrollBars == ScrollBars.Both || dgv.ScrollBars == ScrollBars.Vertical)
483                 {
484                     int _height = dgv.RowTemplate.Height * dgv.Rows.Count;
485                     if (dgv.ColumnHeadersVisible)
486                     {
487                         _height += dgv.ColumnHeadersHeight;
488                     }
489                     blnHasVScrollbar = _height > dgv.Height;
490                 }
491                 barV.Maximum = dgv.Rows.Count;
492                 barV.Visible = blnHasVScrollbar;
493                 barV.Value = dgv.FirstDisplayedScrollingRowIndex; ;
494                 barV.BringToFront();
495             }
496         }
497         private void SetHMaxNum(Control control)
498         {
499             if (!m_lstHCache.ContainsKey(control))
500                 return;
501             var intoH = ControlHelper.GetHScrollBarInfo(control.Handle);
502             UCHScrollbar barH = m_lstHCache[control];
503             if (control is ScrollableControl)
504             {
505                 barH.Maximum = intoH.ScrollMax;
506                 barH.Visible = intoH.ScrollMax > 0 && intoH.nMax > 0 && intoH.nPage > 0;
507                 barH.Value = intoH.nPos;
508                 barH.BringToFront();
509 
510                 //barH.Maximum = (control as ScrollableControl).HorizontalScroll.Maximum;
511                 //barH.Value = (control as ScrollableControl).HorizontalScroll.Value;
512             }
513             else if (control is TreeView)
514             {
515                 var tv = control as TreeView;
516                 barH.Maximum = intoH.ScrollMax;
517                 barH.Visible = intoH.ScrollMax > 0 && intoH.nMax > 0 && intoH.nPage > 0;
518                 barH.Value = intoH.nPos;
519                 barH.BringToFront();
520                 //barH.Maximum = GetTreeNodeMaxX(control as TreeView);
521                 //barH.Value = (control as TreeView).AutoScrollOffset.X;
522             }
523             else if (control is TextBox)
524             {
525                 TextBox txt = (TextBox)control;
526                 barH.Maximum = intoH.ScrollMax;
527 
528                 if (txt.ScrollBars == ScrollBars.Both || txt.ScrollBars == ScrollBars.Horizontal)
529                 {
530                     barH.Visible = true;
531                 }
532                 else
533                 {
534                     barH.Visible = false;
535                 }
536 
537                 barH.Value = intoH.nPos;
538                 barH.BringToFront();
539             }
540             else if (control is RichTextBox)
541             {
542                 bool blnHasHScrollbar = control.IsHandleCreated && (ControlHelper.GetWindowLong(control.Handle, STYLE) & HSCROLL) != 0;
543 
544                 barH.Maximum = intoH.ScrollMax;
545                 barH.Visible = blnHasHScrollbar;
546                 barH.Value = intoH.nPos;
547                 barH.BringToFront();
548             }
549             else if (control is ListBox)
550             {
551                 var lb = control as ListBox;
552                 barH.Maximum = intoH.ScrollMax * lb.ItemHeight;
553                 barH.Visible = intoH.ScrollMax > 0 && intoH.nMax > 0 && intoH.nPage > 0;
554                 barH.Value = intoH.nPos * lb.ItemHeight;
555                 barH.BringToFront();
556             }
557             else if (control is ListView)
558             {
559                 barH.Maximum = intoH.ScrollMax;
560                 barH.Visible = intoH.ScrollMax > 0 && intoH.nMax > 0 && intoH.nPage > 0;
561                 barH.Value = intoH.nPos;
562                 barH.BringToFront();
563             }
564             else if (control is DataGridView)
565             {
566                 bool blnHasHScrollbar = false;
567                 var dgv = (DataGridView)control;
568                 int _width = 0;
569                 if (dgv.ScrollBars == ScrollBars.Both || dgv.ScrollBars == ScrollBars.Horizontal)
570                 {
571                     foreach (DataGridViewColumn com in dgv.Columns)
572                     {
573                         _width += com.Width;
574                     }
575                     if (dgv.RowHeadersVisible)
576                     {
577                         _width += dgv.RowHeadersWidth;
578                     }
579                     blnHasHScrollbar = _width > dgv.Width;
580                 }
581                 if (blnHasHScrollbar)
582                     barH.Maximum = _width - dgv.Width;
583                 barH.Visible = blnHasHScrollbar;
584                 barH.Value = dgv.FirstDisplayedScrollingColumnHiddenWidth;
585                 barH.BringToFront();
586             }
587         }
588         /// <summary>
589         /// Resets the v scroll location.
590         /// </summary>
591         /// <param name="sender">The sender.</param>
592         private void ResetScrollLocation(object sender)
593         {
594             Control control = (Control)sender;
595             bool blnHasVScrollbar = control.IsHandleCreated && (ControlHelper.GetWindowLong(control.Handle, STYLE) & VSCROLL) != 0;
596             bool blnHasHScrollbar = control.IsHandleCreated && (ControlHelper.GetWindowLong(control.Handle, STYLE) & HSCROLL) != 0;
597             if (control is TextBox)
598             {
599                 var txt = (TextBox)control;
600                 if (txt.ScrollBars == ScrollBars.Both)
601                 {
602                     blnHasVScrollbar = true;
603                     blnHasHScrollbar = true;
604                 }
605                 else if (txt.ScrollBars == ScrollBars.Vertical)
606                 {
607                     blnHasVScrollbar = true;
608                     blnHasHScrollbar = false;
609                 }
610                 else if (txt.ScrollBars == ScrollBars.Horizontal)
611                 {
612                     blnHasVScrollbar = false;
613                     blnHasHScrollbar = true;
614                 }
615                 else
616                 {
617                     blnHasVScrollbar = false;
618                     blnHasHScrollbar = false;
619                 }
620             }
621             else if (control is DataGridView)
622             {
623                 var dgv = (DataGridView)control;
624                 if (dgv.ScrollBars == ScrollBars.Both || dgv.ScrollBars == ScrollBars.Vertical)
625                 {
626                     int _height = dgv.RowTemplate.Height * dgv.Rows.Count;
627                     if (dgv.ColumnHeadersVisible)
628                     {
629                         _height += dgv.ColumnHeadersHeight;
630                     }
631                     blnHasVScrollbar = _height > dgv.Height;
632                 }
633                 if (dgv.ScrollBars == ScrollBars.Both || dgv.ScrollBars == ScrollBars.Horizontal)
634                 {
635                     int _width = 0;
636                     foreach (DataGridViewColumn com in dgv.Columns)
637                     {
638                         _width += com.Width;
639                     }
640                     if (dgv.RowHeadersVisible)
641                     {
642                         _width += dgv.RowHeadersWidth;
643                     }
644                     blnHasHScrollbar = _width > dgv.Width;
645                 }
646             }
647             else if (control is ListView)
648             {
649                 if (!((ListView)control).Scrollable)
650                 {
651                     blnHasVScrollbar = false;
652                     blnHasHScrollbar = false;
653                 }
654             }
655             if (control.Visible)
656             {
657                 if (m_lstVCache.ContainsKey(control))
658                 {
659                     m_lstVCache[control].Location = new System.Drawing.Point(control.Right - m_lstVCache[control].Width, control.Top);
660                     if (blnHasHScrollbar)
661                     {
662                         m_lstVCache[control].Height = control.Height - m_lstHCache[control].Height;
663                     }
664                     else
665                     {
666                         m_lstVCache[control].Height = control.Height;
667                     }
668                     SetVMaxNum(control);
669                 }
670 
671                 if (m_lstHCache.ContainsKey(control))
672                 {
673                     m_lstHCache[control].Location = new System.Drawing.Point(control.Left, control.Bottom - m_lstHCache[control].Height);
674                     //if (blnHasVScrollbar)
675                     //{
676                     //    m_lstHCache[control].Width = control.Width - m_lstVCache[control].Width;
677                     //}
678                     //else
679                     //{
680                     m_lstHCache[control].Width = control.Width;
681                     //}
682                     SetHMaxNum(control);
683                 }
684             }
685         }
686 
687         /// <summary>
688         /// Handles the VisibleChanged event of the control control.
689         /// </summary>
690         /// <param name="sender">The source of the event.</param>
691         /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
692         void control_VisibleChanged(object sender, EventArgs e)
693         {
694             Control control = (Control)sender;
695 
696             if (m_lstVCache.ContainsKey(control) && m_lstVCache[control].Parent != null)
697             {
698                 m_lstVCache[control].Visible = control.Visible;
699             }
700 
701             if (m_lstHCache.ContainsKey(control) && m_lstHCache[control].Parent != null)
702             {
703                 m_lstHCache[control].Visible = control.Visible;
704             }
705             if (control.Visible)
706             {
707                 control_SizeChanged(control, null);
708             }
709         }
710 
711         private const int HSCROLL = 0x100000;
712         private const int VSCROLL = 0x200000;
713         private const int STYLE = -16;
714 
715         private Dictionary<Control, UCVScrollbar> m_lstVCache = new Dictionary<Control, UCVScrollbar>();
716         private Dictionary<Control, UCHScrollbar> m_lstHCache = new Dictionary<Control, UCHScrollbar>();
717 
718         void barV_Scroll(object sender, EventArgs e)
719         {
720             UCVScrollbar bar = (UCVScrollbar)sender;
721             if (m_lstVCache.ContainsValue(bar))
722             {
723                 Control c = m_lstVCache.FirstOrDefault(p => p.Value == bar).Key;
724 
725                 //ControlHelper.SetVScrollValue(c.Handle, bar.Value);
726                 if (c is ScrollableControl)
727                 {
728                     (c as ScrollableControl).AutoScrollPosition = new Point((c as ScrollableControl).AutoScrollPosition.X, bar.Value);
729                 }
730                 else if (c is TreeView)
731                 {
732                     ControlHelper.SetVScrollValue(c.Handle, bar.Value / ((c as TreeView).ItemHeight));
733                 }
734                 else if (c is TextBox)
735                 {
736                     ControlHelper.SetVScrollValue(c.Handle, bar.Value / ((c as TextBox).PreferredHeight));
737                 }
738                 else if (c is RichTextBox)
739                 {
740                     ControlHelper.SetVScrollValue(c.Handle, bar.Value);
741                 }
742                 else if (c is ListBox)
743                 {
744                     ControlHelper.SetVScrollValue(c.Handle, bar.Value / ((c as ListBox).ItemHeight));
745                 }
746                 else if (c is ListView)
747                 {
748                     ControlHelper.SetVScrollValue(c.Handle, bar.Value);
749                 }
750                 else if (c is DataGridView)
751                 {
752                     var dgv = (DataGridView)c;
753                     if(bar.Value>0)
754                     dgv.FirstDisplayedScrollingRowIndex = bar.Value - 1;
755                 }
756             }
757         }
758 
759         void barH_Scroll(object sender, EventArgs e)
760         {
761             UCHScrollbar bar = (UCHScrollbar)sender;
762             if (m_lstHCache.ContainsValue(bar))
763             {
764                 Control c = m_lstHCache.FirstOrDefault(p => p.Value == bar).Key;
765                 if (c is ScrollableControl)
766                 {
767                     (c as ScrollableControl).AutoScrollPosition = new Point(bar.Value, (c as ScrollableControl).AutoScrollPosition.Y);
768                 }
769                 else if (c is TreeView)
770                 {
771                     ControlHelper.SetHScrollValue(c.Handle, bar.Value);
772                     //TreeView tv = (c as TreeView);
773                     //SetTreeViewVScrollLocation(tv, tv.Nodes, bar.Value);
774                 }
775                 else if (c is TextBox)
776                 {
777                     ControlHelper.SetHScrollValue(c.Handle, bar.Value);
778                     //TextBox txt = (c as TextBox);
779                     //SetTextBoxVScrollLocation(txt, bar.Value);
780                 }
781                 else if (c is RichTextBox)
782                 {
783                     ControlHelper.SetHScrollValue(c.Handle, bar.Value);
784                 }
785                 else if (c is ListBox)
786                 {
787                     ControlHelper.SetHScrollValue(c.Handle, bar.Value);
788                 }
789                 else if (c is ListView)
790                 {
791                     ControlHelper.SetHScrollValue(c.Handle, bar.Value);
792                 }
793                 else if (c is DataGridView)
794                 {
795                     var dgv = (DataGridView)c;
796                     dgv.HorizontalScrollingOffset = bar.Value;
797                 }
798             }
799         }
800 
801         #region Treeview處理    English:Treeview\u5904\u7406
802         void tv_AfterCollapse(object sender, TreeViewEventArgs e)
803         {
804             control_SizeChanged(sender as Control, null);
805         }
806 
807         void tv_AfterExpand(object sender, TreeViewEventArgs e)
808         {
809             control_SizeChanged(sender as Control, null);
810         }
811 
812         #endregion
813 
814         #region TextBox處理    English:TextBox Processing
815 
816         void txt_TextChanged(object sender, EventArgs e)
817         {
818             Control txt = sender as Control;
819             control_SizeChanged(txt, null);
820         }
821 
822         void txt_KeyDown(object sender, KeyEventArgs e)
823