COM+Win64GDIDirectXOpenGLWindows MobileGDI+COMWin32设计/图形高级C# 2.0初学者C# 3.0WPF中级开发Visual StudioWindows.NETVisual BasicC#
炫酷的 Vista 侧边栏小工具风格 CPU 信息动画控件!(已修复)
炫酷的 Vista 侧边栏小工具风格 CPU 信息动画控件!(已修复)

引言
本文介绍了如何使用 C# 创建一款 Vista 风格的炫酷 CPU 信息控件。该控件可以获取当前 Windows 系统的 CPU 和内存使用百分比。
背景
微软在 Vista 系统中提供了一个漂亮的侧边栏,所以我希望使用 C# 创建相同的控件。感谢 GDI+,你会发现这非常容易实现。
Using the Code
主类是 VistaCPUInfo
类;它继承自 Usercontrol
类
//
// Main Class
//
public partial class VistaCPUInfo : UserControl
{
...
}
要获取 CPU 使用百分比,请使用以下代码
if (pc == null) pc = new PerformanceCounter("Processor", "% Processor Time", "_Total");
cpu = (float)pc.NextValue(); //This is the aim value!
要获取物理内存使用百分比,请使用以下代码
//Memory Structure
[StructLayout(LayoutKind.Sequential)]
public struct MEMORY_INFO
{
public uint dwLength;
public uint dwMemoryLoad;
public uint dwTotalPhys;
public uint dwAvailPhys;
public uint dwTotalPageFile;
public uint dwAvailPageFile;
public uint dwTotalVirtual;
public uint dwAvailVirtual;
}
...
MEMORY_INFO MemInfo;
MemInfo = new MEMORY_INFO();
GlobalMemoryStatus(ref MemInfo);
mem = (float)MemInfo.dwMemoryLoad; //This is the aim value!
然后,使用 GDI+ 方法绘制结果
void VistaCPUInfo_Paint(object sender, PaintEventArgs e)
{
e.Graphics.TextRenderingHint =
System.Drawing.Text.TextRenderingHint.ClearTypeGridFit;
//Background
e.Graphics.DrawImage
(Image, (int)positionRect.X, (int)positionRect.Y, 198, 159);
//Point
e.Graphics.ResetTransform();
e.Graphics.TranslateTransform
(positionRect.X + 68f, positionRect.Y + 82f);
e.Graphics.RotateTransform(cpuCurAngle);
e.Graphics.DrawImage(ImageDial, -5, -49, 10, 98);
e.Graphics.ResetTransform();
e.Graphics.TranslateTransform
(positionRect.X + 143f, positionRect.Y + 50f);
e.Graphics.RotateTransform(memCurAngle);
e.Graphics.DrawImage(ImageDialSmall, -5, -35, 10, 70);
e.Graphics.ResetTransform();
//Cover
e.Graphics.DrawImage
(ImageDialDot, (int)positionRect.X, (int)positionRect.Y, 198, 150);
//Text
RectangleF rect = new RectangleF((int)positionRect.X + 53,
(int)positionRect.Y + 107, 35, 15);
e.Graphics.DrawString(((int)percentOfCPU).ToString() +
"%", textFont, textBrush, rect, format);
rect = new RectangleF((int)positionRect.X + 127,
(int)positionRect.Y + 66, 35, 13);
e.Graphics.DrawString(((int)percentOfMemory).ToString() +
"%", textFont, textBrush, rect, format);
//GlassEffect
e.Graphics.DrawImage
(ImageGlass, (int)positionRect.X, (int)positionRect.Y, 198, 159);
}
关注点
- 使用 GDI+ 和 C# 进行开发是一件非常有趣的事情!
- PNG 格式的图片非常适合绘制 alpha 图片!
- 更多代码示例,请访问 这个网站。
历史
- 2008/1/8:首次发布于 cnpopsoft.com
- 2008/5/9:Davidwu 修改:禁用了设计时模式下的动画。(感谢 Johnny J!)