7.C#编程学习——属性
代码如下:
usingSystem;
usingSystem.Drawing;
usingSystem.Windows.Forms;
classSysInfoList : Form
{
readonlyfloat cxCol;
readonlyint cySpace;
publicstaticvoid Main()
{
Application.Run(newSysInfoList());
}
public SysInfoList()
{
Text = "System Information: List";
BackColor = SystemColors.Window;
ForeColor = SystemColors.WindowText;
Graphics grfx = CreateGraphics();
SizeF sizef = grfx.MeasureString(" ", Font);
cxCol = sizef.Width + SysInfoStrings.MaxLabelWidth(grfx, Font);
grfx.Dispose();
cySpace = Font.Height;
}
protectedoverridevoid OnPaint(PaintEventArgs pea)
{
Graphics grfx = pea.Graphics;
Brush brush = newSolidBrush(ForeColor);
int iCount = SysInfoStrings.Count;
string[] astrLabels = SysInfoStrings.Labels;
string[] astrValues = SysInfoStrings.Values;
for (int i = 0; i < iCount;i++)
{
grfx.DrawString(astrLabels[i],Font, brush,
0, i * cySpace);
grfx.DrawString(astrValues[i],Font, brush,
cxCol, i *cySpace);
}
}
}
SysInfoStrings.cs如下
//---------------------------------------------
// SysInfoStrings.cs ?2001 byCharles Petzold
//---------------------------------------------
usingSystem;
usingSystem.Drawing;
usingSystem.Windows.Forms;
classSysInfoStrings
{
publicstaticstring[] Labels
{
get
{
returnnewstring[]
{
"ArrangeDirection",
"ArrangeStartingPosition",
"BootMode",
"Border3DSize",
"BorderSize",
"CaptionButtonSize",
"CaptionHeight",
"ComputerName",
"CursorSize",
"DbcsEnabled",
"DebugOS",
"DoubleClickSize",
"DoubleClickTime",
"DragFullWindows",
"DragSize",
"FixedFrameBorderSize",
"FrameBorderSize",
"HighContrast",
"HorizontalScrollBarArrowWidth",
"HorizontalScrollBarHeight",
"HorizontalScrollBarThumbWidth",
"IconSize",
"IconSpacingSize",
"KanjiWindowHeight",
"MaxWindowTrackSize",
"MenuButtonSize",
"MenuCheckSize",
"MenuFont",
"MenuHeight",
"MidEastEnabled",
"MinimizedWindowSize",
"MinimizedWindowSpacingSize",
"MinimumWindowSize",
"MinWindowTrackSize",
"MonitorCount",
"MonitorsSameDisplayFormat",
"MouseButtons",
"MouseButtonsSwapped",
"MousePresent",
"MouseWheelPresent",
"MouseWheelScrollLines",
"NativeMouseWheelSupport",
"Network",
"PenWindows",
"PrimaryMonitorMaximizedWindowSize",
"PrimaryMonitorSize",
"RightAlignedMenus",
"Secure",
"ShowSounds",
"SmallIconSize",
"ToolWindowCaptionButtonSize",
"ToolWindowCaptionHeight",
"UserDomainName",
"UserInteractive",
"UserName",
"VerticalScrollBarArrowHeight",
"VerticalScrollBarThumbHeight",
"VerticalScrollBarWidth",
"VirtualScreen",
"WorkingArea",
};
}
}
publicstaticstring[] Values
{
get
{
returnnewstring[]
{
SystemInformation.ArrangeDirection.ToString(),
SystemInformation.ArrangeStartingPosition.ToString(),
SystemInformation.BootMode.ToString(),
SystemInformation.Border3DSize.ToString(),
SystemInformation.BorderSize.ToString(),
SystemInformation.CaptionButtonSize.ToString(),
SystemInformation.CaptionHeight.ToString(),
SystemInformation.ComputerName,
SystemInformation.CursorSize.ToString(),
SystemInformation.DbcsEnabled.ToString(),
SystemInformation.DebugOS.ToString(),
SystemInformation.DoubleClickSize.ToString(),
SystemInformation.DoubleClickTime.ToString(),
SystemInformation.DragFullWindows.ToString(),
SystemInformation.DragSize.ToString(),
SystemInformation.FixedFrameBorderSize.ToString(),
SystemInformation.FrameBorderSize.ToString(),
SystemInformation.HighContrast.ToString(),
SystemInformation.HorizontalScrollBarArrowWidth.ToString(),
SystemInformation.HorizontalScrollBarHeight.ToString(),
SystemInformation.HorizontalScrollBarThumbWidth.ToString(),
SystemInformation.IconSize.ToString(),
SystemInformation.IconSpacingSize.ToString(),
SystemInformation.KanjiWindowHeight.ToString(),
SystemInformation.MaxWindowTrackSize.ToString(),
SystemInformation.MenuButtonSize.ToString(),
SystemInformation.MenuCheckSize.ToString(),
SystemInformation.MenuFont.ToString(),
SystemInformation.MenuHeight.ToString(),
SystemInformation.MidEastEnabled.ToString(),
SystemInformation.MinimizedWindowSize.ToString(),
SystemInformation.MinimizedWindowSpacingSize.ToString(),
SystemInformation.MinimumWindowSize.ToString(),
SystemInformation.MinWindowTrackSize.ToString(),
SystemInformation.MonitorCount.ToString(),
SystemInformation.MonitorsSameDisplayFormat.ToString(),
SystemInformation.MouseButtons.ToString(),
SystemInformation.MouseButtonsSwapped.ToString(),
SystemInformation.MousePresent.ToString(),
SystemInformation.MouseWheelPresent.ToString(),
SystemInformation.MouseWheelScrollLines.ToString(),
SystemInformation.NativeMouseWheelSupport.ToString(),
SystemInformation.Network.ToString(),
SystemInformation.PenWindows.ToString(),
SystemInformation.PrimaryMonitorMaximizedWindowSize.ToString(),
SystemInformation.PrimaryMonitorSize.ToString(),
SystemInformation.RightAlignedMenus.ToString(),
SystemInformation.Secure.ToString(),
SystemInformation.ShowSounds.ToString(),
SystemInformation.SmallIconSize.ToString(),
SystemInformation.ToolWindowCaptionButtonSize.ToString(),
SystemInformation.ToolWindowCaptionHeight.ToString(),
SystemInformation.UserDomainName,
SystemInformation.UserInteractive.ToString(),
SystemInformation.UserName,
SystemInformation.VerticalScrollBarArrowHeight.ToString(),
SystemInformation.VerticalScrollBarThumbHeight.ToString(),
SystemInformation.VerticalScrollBarWidth.ToString(),
SystemInformation.VirtualScreen.ToString(),
SystemInformation.WorkingArea.ToString(),
};
}
}
publicstaticint Count
{
get
{
return Labels.Length;
}
}
publicstaticfloat MaxLabelWidth(Graphics grfx, Font font)
{
return MaxWidth(Labels, grfx, font);
}
publicstaticfloat MaxValueWidth(Graphics grfx, Font font)
{
return MaxWidth(Values, grfx, font);
}
staticfloat MaxWidth(string[] astr, Graphics grfx, Font font)
{
float fMax = 0;
foreach (string str in astr)
fMax = Math.Max(fMax, grfx.MeasureString(str, font).Width);
return fMax;
}
}