SystemInformation Properties
Strictly speaking, it is not necessary to have a mouse or other pointing device installed on a Windows system, although as a practical matter, it is difficult to work with Windows without one, and virtually every modern Windows computer has a mouse or pointer installed. The SystemInformation class provides several read-only static (Shared in VB.NET) properties, listed in Table 8-1, which allow a program to determine if a mouse is connected to the system, and if so, the configuration and capabilities of the mouse. Accessing these properties is demonstrated in the programs listed in Example 8-1 (in C#) and in Example 8-2 (in VB.NET).
Most mice in use today on Windows systems have two buttons: the left button is typically the primary button and the right button is typically the secondary button. The user can swap these preferences in the Control Panel/Mouse applet to allow left-handed users to reverse the buttons. If the primary button is clicked twice in rapid succession without moving more than a certain number of pixels, the action is interpreted as a double-click. Many mice today also have a mouse wheel, which can also be seen by the system as a third button. The last three properties listed in Table 8-1 relate to the wheel.
Property |
Type |
Description |
---|---|---|
DoubleClickSize |
Size |
Returns dimensions, in pixels, of rectangular area around the first mouse click within which two mouse clicks can be considered a double-click. |
DoubleClickTime |
Integer |
Returns maximum number of milliseconds allowed between two mouse clicks for them to be considered a double-click. Can be set by the user in Control Panel. |
MouseButtons |
Integer |
Returns number of buttons on the mouse. Zero if no mouse is installed. |
MouseButtonsSwapped |
Boolean |
Returns true if the function of the left and right mouse buttons are swapped. Can be set by the user in Control Panel. |
MousePresent |
Boolean |
Returns true if mouse is installed. |
MouseWheelPresent |
Boolean |
Returns true if mouse has a mouse wheel. |
MouseWheelScrollLines |
Integer |
Returns number of lines to scroll when the mouse wheel is rotated one notch, or detent. Used by controls with a scrollbar. |
NativeMouseWheelSupport |
Boolean |
Returns true if the OS supports a mouse wheel natively. |
Example 8-1. Accessing SystemInformation in C# (SystemInfo.cs)
using System; using System.Drawing; using System.Windows.Forms; namespace ProgrammingWinApps { public class SystemInfo : Form { string[ ] SysInfoLabels = { "DoubleClickSize", "DoubleClickTime", "MouseButtons", "MouseButtonsSwapped", "MousePresent", "MouseWheelPresent", "MouseWheelScrollLines", "NativeMouseWheelSupport" }; string[ ] SysInfoValues = { SystemInformation.DoubleClickSize.ToString( ), SystemInformation.DoubleClickTime.ToString( ), SystemInformation.MouseButtons.ToString( ), SystemInformation.MouseButtonsSwapped.ToString( ), SystemInformation.MousePresent.ToString( ), SystemInformation.MouseWheelPresent.ToString( ), SystemInformation.MouseWheelScrollLines.ToString( ), SystemInformation.NativeMouseWheelSupport.ToString( ) }; public SystemInfo( ) { Text = "System Information"; Size = new Size(400,400); } static void Main( ) { Application.Run(new SystemInfo( )); } protected override void OnPaint(PaintEventArgs e) { base.OnPaint(e); Graphics g = e.Graphics; int y = 0; int yDelta = Font.Height; for(int i = 0; i < SysInfoLabels.Length; i++) { string str = SysInfoLabels[i]; str += ": " + SysInfoValues[i]; g.DrawString(str, Font, Brushes.Black, 0, y += yDelta); } } } }
Example 8-2. Accessing SystemInformation in VB.NET (SystemInfo.vb)
Option Strict On imports System imports System.Drawing imports System.Windows.Forms namespace ProgrammingWinApps public class SystemInfo : inherits Form dim SysInfoLabels( ) as string = _ {"DoubleClickSize", _ "DoubleClickTime", _ "MouseButtons", _ "MouseButtonsSwapped", _ "MousePresent", _ "MouseWheelPresent", _ "MouseWheelScrollLines", _ "NativeMouseWheelSupport"} dim SysInfoValues as string( ) = _ {SystemInformation.DoubleClickSize.ToString( ), _ SystemInformation.DoubleClickTime.ToString( ), _ SystemInformation.MouseButtons.ToString( ), _ SystemInformation.MouseButtonsSwapped.ToString( ), _ SystemInformation.MousePresent.ToString( ), _ SystemInformation.MouseWheelPresent.ToString( ), _ SystemInformation.MouseWheelScrollLines.ToString( ), _ SystemInformation.NativeMouseWheelSupport.ToString( ) } public sub New( ) Text = "System Information" Size = new Size(400,400) end sub public shared sub Main( ) Application.Run(new SystemInfo( )) end sub protected overrides sub OnPaint(ByVal e as PaintEventArgs) myBase.OnPaint(e) dim g as Graphics = e.Graphics dim y as integer = 0 dim yDelta as integer = Font.Height dim i as integer for i = 0 to SysInfoLabels.Length - 1 dim str as string = SysInfoLabels(i) str = str + ": " + SysInfoValues(i) y = y + yDelta g.DrawString(str, Font, Brushes.Black, 0, y) next end sub end class end namespace
In the programs listed in Example 8-1 and Example 8-2, two string arrays are declared as member variables: one array contains a set of labels to display on the form and the other contains a matching set of SystemInformation property values. Nothing happens in the Form's constructor other than the setting of Text and Size properties of the form.
The real action occurs in the override of the OnPaint method. This method is called every time the form is redrawn. You begin by chaining up to the base class's OnPaint method so that other methods registered with the OnPaint event delegate are notified, and also to insure that no base-class functionality is missed:
base.OnPaint(e);
myBase.OnPaint(e)
The contents of the arrays of labels and SystemInformation property values are drawn on the surface of the form using a Graphics object. (Chapter 10 covers graphics in detail.) A Graphics object is instantiated, representing the surface of the form:
Graphics g = e.Graphics;
dim g as Graphics = e.Graphics
Then the array of labels, SysInfoLabels, is iterated. For each entry, a string is built up consisting of the label and the corresponding property value. This string is drawn using the DrawString method:
g.DrawString(str, Font, Brushes.Black, 0, y += yDelta);
y = y + yDelta
g.DrawString(str, Font, Brushes.Black, 0, y)
Категории