简介
本文介绍了如何使一个Vista风格与C#酷CPUInfo控制。这种控制可以得到当前的Windows系统的CPU和内存的百分比。背景
微软在Vista系统提供了一个漂亮的侧栏,所以我想用C#相同的控制。到GDI,你可以发现,这是很容易做到。使用代码
的主类是VistaCPUInfo类,它是从UserControl类继承://
// Main Class
//
public partial class VistaCPUInfo : UserControl
{
...
}
要得到CPU的百分比,使用下面的代码:{C}
要获得物理内存的百分比,使用下面的代码://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方法得出的结果:
兴趣点GDI和C#开发是一个非常有趣的事情!PNG格式的图片绘制阿尔法PICS是非常好!对于更多的代码示例,请访问{A}。历史
2008年1月8日:第一,关于2008年5月9日:修改Davidwu:禁用designtime模式的动画。 (感谢约翰尼研究!)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);
}