Determining the scrollbars visibility
The controls with the auto-scroll functionality provide HorizontalScroll and VerticalScroll properties which enable you to control the automatically shown scrollbars. Use their Visible members to determine whether the scrollbars are visible to the user.
[C#]ScrollableControl;
To demonstrate the properties usage, we will implement a GetVisibleScrollbars method that returns a ScrollBars value specifying which scrollbars are visible.
[C#]using System.Windows.Forms;
protected static ScrollBars GetVisibleScrollbars(ScrollableControl ctl)
{
if (ctl.HorizontalScroll.Visible)
return ctl.VerticalScroll.Visible ? ScrollBars.Both : ScrollBars.Horizontal;
else
return ctl.VerticalScroll.Visible ? ScrollBars.Vertical : ScrollBars.None;
}
An alternative technique
There is an alternative way how to get the scrollbars visibility based on use of the GetWindowLong API function. This technique unlike the foregoing works also on .NET Framework 1.1.
First, create a Win32 class that imports the GetWindowLong function from the user32.dll library and defines some constants.
[C#]
Next, implement the GetVisibleScrollbars method that calls GetWindowLong to get a control's window style and examines the returned value to get the visible scrollbars. Use the control's Handle property as the first argument of the GetWindowLong method.
[C#]using System.Windows.Forms;
protected static ScrollBars GetVisibleScrollbars(Control ctl)
{
int wndStyle = Win32.GetWindowLong(ctl.Handle, Win32.GWL_STYLE);
bool hsVisible = (wndStyle & Win32.WS_HSCROLL) != 0;
bool vsVisible = (wndStyle & Win32.WS_VSCROLL) != 0;
if (hsVisible)
return vsVisible ? ScrollBars.Both : ScrollBars.Horizontal;
else
return vsVisible ? ScrollBars.Vertical : ScrollBars.None;
}
Using the GetVisibleScrollbars method
You can add a VisibleScrollbars property to your control that indicates which scrollbars are displayed in the control window.
A change of scrollbars visibility causes the cotrol's client size change, and thus, firing the Resize event. Handle this event (e.g. by overriding the OnResize method) to detect scrollbars visibility changes.
[C#]
No comments:
Post a Comment