void barH_Scroll(object sender, EventArgs e) { ScrollbarHEx bar = (ScrollbarHEx)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) { ScrollBarHelper.SetHScrollValue(c.Handle, bar.Value); //TreeView tv = (c as TreeView); //SetTreeViewVScrollLocation(tv, tv.Nodes, bar.Value); } else if (c is TextBox) { ScrollBarHelper.SetHScrollValue(c.Handle, bar.Value); //TextBox txt = (c as TextBox); //SetTextBoxVScrollLocation(txt, bar.Value); } else if (c is RichTextBox) { ScrollBarHelper.SetHScrollValue(c.Handle, bar.Value); } else if (c is ListBox) { ScrollBarHelper.SetHScrollValue(c.Handle, bar.Value); } else if (c is ComboBox) { ScrollBarHelper.SetHScrollValue(c.Handle, bar.Value); } else if (c is ListView) { ScrollBarHelper.SetHScrollValue(c.Handle, bar.Value); } else if (c is DataGridView) { var dgv = (DataGridView)c; dgv.HorizontalScrollingOffset = bar.Value; } } }
private void SetHMaxNum(Control control) { if (!m_lstHCache.ContainsKey(control)) { return; } var intoH = ScrollBarHelper.GetHScrollBarInfo(control.Handle); ScrollbarHEx barH = m_lstHCache[control]; if (control is ScrollableControl) { barH.Maximum = intoH.ScrollMax; barH.Visible = intoH.ScrollMax > 0 && intoH.nMax > 0 && intoH.nPage > 0; barH.Value = intoH.nPos; barH.BringToFront(); //barH.Maximum = (control as ScrollableControl).HorizontalScroll.Maximum; //barH.Value = (control as ScrollableControl).HorizontalScroll.Value; } else if (control is TreeView) { var tv = control as TreeView; barH.Maximum = intoH.ScrollMax; barH.Visible = intoH.ScrollMax > 0 && intoH.nMax > 0 && intoH.nPage > 0; barH.Value = intoH.nPos; barH.BringToFront(); //barH.Maximum = GetTreeNodeMaxX(control as TreeView); //barH.Value = (control as TreeView).AutoScrollOffset.X; } else if (control is TextBox) { TextBox txt = (TextBox)control; barH.Maximum = intoH.ScrollMax; if (txt.ScrollBars == ScrollBars.Both || txt.ScrollBars == ScrollBars.Horizontal) { barH.Visible = true; } else { barH.Visible = false; } barH.Value = intoH.nPos; barH.BringToFront(); } else if (control is RichTextBox) { bool blnHasHScrollbar = control.IsHandleCreated && (ScrollBarHelper.GetWindowLong(control.Handle, STYLE) & HSCROLL) != 0; barH.Maximum = intoH.ScrollMax; barH.Visible = blnHasHScrollbar; barH.Value = intoH.nPos; barH.BringToFront(); } else if (control is ListBox) { var lb = control as ListBox; barH.Maximum = intoH.ScrollMax * lb.ItemHeight; barH.Visible = intoH.ScrollMax > 0 && intoH.nMax > 0 && intoH.nPage > 0; barH.Value = intoH.nPos * lb.ItemHeight; barH.BringToFront(); } else if (control is ComboBox) { var lb = control as ComboBox; barH.Maximum = intoH.ScrollMax * lb.ItemHeight; barH.Visible = intoH.ScrollMax > 0 && intoH.nMax > 0 && intoH.nPage > 0; barH.Value = intoH.nPos * lb.ItemHeight; barH.BringToFront(); } else if (control is ListView) { barH.Maximum = intoH.ScrollMax; barH.Visible = intoH.ScrollMax > 0 && intoH.nMax > 0 && intoH.nPage > 0; barH.Value = intoH.nPos; barH.BringToFront(); } else if (control is DataGridView) { bool blnHasHScrollbar = false; var dgv = (DataGridView)control; int _width = 0; if (dgv.ScrollBars == ScrollBars.Both || dgv.ScrollBars == ScrollBars.Horizontal) { foreach (DataGridViewColumn com in dgv.Columns) { _width += com.Width; } if (dgv.RowHeadersVisible) { _width += dgv.RowHeadersWidth; } blnHasHScrollbar = _width > dgv.Width; } if (blnHasHScrollbar) { barH.Maximum = _width - dgv.Width; } barH.Visible = blnHasHScrollbar; barH.Value = dgv.FirstDisplayedScrollingColumnHiddenWidth; barH.BringToFront(); } }
void control_SizeChanged(object sender, EventArgs e) { if (this.DesignMode) { return; } else { var control = sender as Control; bool blnHasVScrollbar = control.IsHandleCreated && (ScrollBarHelper.GetWindowLong(control.Handle, STYLE) & VSCROLL) != 0; bool blnHasHScrollbar = control.IsHandleCreated && (ScrollBarHelper.GetWindowLong(control.Handle, STYLE) & HSCROLL) != 0; if (control is TextBox) { var txt = (TextBox)control; if (txt.ScrollBars == ScrollBars.Both) { blnHasVScrollbar = true; blnHasHScrollbar = true; } else if (txt.ScrollBars == ScrollBars.Vertical) { blnHasVScrollbar = true; blnHasHScrollbar = false; } else if (txt.ScrollBars == ScrollBars.Horizontal) { blnHasVScrollbar = false; blnHasHScrollbar = true; } else { blnHasVScrollbar = false; blnHasHScrollbar = false; } } else if (control is DataGridView) { var dgv = (DataGridView)control; if (dgv.ScrollBars == ScrollBars.Both || dgv.ScrollBars == ScrollBars.Vertical) { int _height = dgv.RowTemplate.Height * dgv.Rows.Count; if (dgv.ColumnHeadersVisible) { _height += dgv.ColumnHeadersHeight; } blnHasVScrollbar = _height > dgv.Height; } if (dgv.ScrollBars == ScrollBars.Both || dgv.ScrollBars == ScrollBars.Horizontal) { int _width = 0; foreach (DataGridViewColumn com in dgv.Columns) { _width += com.Width; } if (dgv.RowHeadersVisible) { _width += dgv.RowHeadersWidth; } blnHasHScrollbar = _width > dgv.Width; } } else if (control is ListView) { if (!((ListView)control).Scrollable) { blnHasVScrollbar = false; blnHasHScrollbar = false; } } if (blnHasVScrollbar) { if (!m_lstVCache.ContainsKey(control)) { if (control.Parent != null) { ScrollbarVEx barV = new ScrollbarVEx(); barV.SmallChange = 5; barV.Width = SystemInformation.VerticalScrollBarWidth + 1; barV.Scroll += barV_Scroll; m_lstVCache[control] = barV; if (blnHasHScrollbar) { barV.Height = control.Height - barV.Width; } else { barV.Height = control.Height; } SetVMaxNum(control); barV.Location = new System.Drawing.Point(control.Right - barV.Width, control.Top); control.Parent.Controls.Add(barV); int intControlIndex = control.Parent.Controls.GetChildIndex(control); control.Parent.Controls.SetChildIndex(barV, intControlIndex); } } else { if (blnHasHScrollbar) { m_lstVCache[control].Height = control.Height - m_lstVCache[control].Width; } else { m_lstVCache[control].Height = control.Height; } SetVMaxNum(control); } } else { if (m_lstVCache.ContainsKey(control) && m_lstVCache[control].Parent != null) { m_lstVCache[control].Visible = false; } } if (blnHasHScrollbar) { if (!m_lstHCache.ContainsKey(control)) { if (control.Parent != null) { ScrollbarHEx barH = new ScrollbarHEx(); barH.Height = SystemInformation.HorizontalScrollBarHeight + 1; barH.SmallChange = 5; barH.Scroll += barH_Scroll; m_lstHCache[control] = barH; if (blnHasHScrollbar) { barH.Width = control.Width - barH.Height; } else { barH.Width = control.Width; } SetHMaxNum(control); barH.Location = new System.Drawing.Point(control.Left, control.Bottom - barH.Height); control.Parent.Controls.Add(barH); int intControlIndex = control.Parent.Controls.GetChildIndex(control); control.Parent.Controls.SetChildIndex(barH, intControlIndex); } } else { if (blnHasHScrollbar) { m_lstHCache[control].Width = control.Width - m_lstHCache[control].Height; } else { m_lstHCache[control].Width = control.Width; } SetHMaxNum(control); } } else { if (m_lstHCache.ContainsKey(control)) { if (m_lstHCache[control].Visible && m_lstHCache[control].Parent != null) { m_lstHCache[control].Visible = false; } } } } ResetScrollLocation(sender); }