private void SetHMaxNum(Control control) { if (!m_lstHCache.ContainsKey(control)) { return; } UCHScrollbar barH = m_lstHCache[control]; if (control is ScrollableControl) { barH.Maximum = (control as ScrollableControl).HorizontalScroll.Maximum; barH.Value = (control as ScrollableControl).HorizontalScroll.Value; } else if (control is TreeView) { barH.Maximum = GetTreeNodeMaxX(control as TreeView); barH.Value = (control as TreeView).AutoScrollOffset.X; } else if (control is TextBox) { TextBox txt = (TextBox)control; int intTxtMaxWidth = 0; int intTextWidth = 0; using (var g = txt.CreateGraphics()) { intTxtMaxWidth = (int)g.MeasureString(txt.Text, txt.Font).Width; intTextWidth = (int)g.MeasureString(txt.Text.Substring(0, txt.SelectionStart), txt.Font).Width; } barH.Maximum = intTxtMaxWidth; barH.Value = (control as TextBox).AutoScrollOffset.Y; } }
void barH_Scroll(object sender, EventArgs e) { UCHScrollbar bar = (UCHScrollbar)sender; if (m_lstHCache.ContainsValue(bar)) { Control c = m_lstHCache.FirstOrDefault(p => p.Value == bar).Key; if (c is ScrollableControl) { (c as ScrollableControl).AutoScrollPosition = new Point(bar.Value, (c as ScrollableControl).AutoScrollPosition.Y); } else if (c is TreeView) { //TreeView tv = (c as TreeView); //SetTreeViewVScrollLocation(tv, tv.Nodes, bar.Value); } else if (c is TextBox) { //TextBox txt = (c as TextBox); //SetTextBoxVScrollLocation(txt, bar.Value); } } }
void control_SizeChanged(object sender, EventArgs e) { if (ControlHelper.IsDesignMode()) { return; } else { var control = sender as Control; bool blnHasVScrollbar = control.IsHandleCreated && (ControlHelper.GetWindowLong(control.Handle, STYLE) & VSCROLL) != 0; bool blnHasHScrollbar = control.IsHandleCreated && (ControlHelper.GetWindowLong(control.Handle, STYLE) & HSCROLL) != 0; if (blnHasVScrollbar) { if (!m_lstVCache.ContainsKey(control)) { if (control.Parent != null) { UCVScrollbar barV = new UCVScrollbar(); barV.Width = SystemInformation.VerticalScrollBarWidth; barV.Scroll += barV_Scroll; m_lstVCache[control] = barV; if (blnHasHScrollbar) { barV.Height = control.Height - barV.Width - 2; } else { barV.Height = control.Height - 2; } SetVMaxNum(control); barV.Location = new System.Drawing.Point(control.Right - barV.Width - 1, control.Top + 1); control.Parent.Controls.Add(barV); int intControlIndex = control.Parent.Controls.GetChildIndex(control); control.Parent.Controls.SetChildIndex(barV, intControlIndex); } } else { SetVMaxNum(control); } } else { if (m_lstVCache.ContainsKey(control) && m_lstVCache[control].Parent != null) { m_lstVCache[control].Parent.Controls.Remove(m_lstVCache[control]); m_lstVCache.Remove(control); } } if (blnHasHScrollbar) { if (!m_lstHCache.ContainsKey(control)) { if (control.Parent != null) { UCHScrollbar barH = new UCHScrollbar(); barH.Height = SystemInformation.HorizontalScrollBarHeight; barH.Scroll += barH_Scroll; m_lstHCache[control] = barH; if (blnHasHScrollbar) { barH.Width = control.Width - barH.Height - 2; } else { barH.Width = control.Width - 2; } SetHMaxNum(control); barH.Location = new System.Drawing.Point(control.Left + 1, control.Bottom - barH.Height - 1); control.Parent.Controls.Add(barH); int intControlIndex = control.Parent.Controls.GetChildIndex(control); control.Parent.Controls.SetChildIndex(barH, intControlIndex); } } else { SetHMaxNum(control); } } else { if (m_lstHCache.ContainsKey(control)) { if (m_lstHCache[control].Visible && m_lstHCache[control].Parent != null) { m_lstHCache[control].Parent.Controls.Remove(m_lstHCache[control]); } } } } ResetScrollLocation(sender); }