基本信息
源码名称:C# 仪表盘 例子源码
源码大小:0.76M
文件格式:.rar
开发语言:C#
更新时间:2014-11-04
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300

本次赞助数额为: 2 元 
   源码介绍

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.Runtime.InteropServices;

namespace CNPOPSOFT.Controls
{
    public partial class VistaCPUInfo : UserControl
    {
        PerformanceCounter pc = null;
        float percentOfCPU = 0, percentOfMemory = 0;  //CPU和内存分别所占百分比,0~99
        bool firstRun = true;

        Timer timer = new Timer();
        Timer timerCPU = new Timer();
        Timer timerMem = new Timer();

        float cpuAimAngle = 0, memAimAngle = 0, cpuCurAngle = 0, memCurAngle = 0;
        StringFormat format = new StringFormat();
        Font textFont = new Font("Arial", 8, FontStyle.Bold);
        SolidBrush textBrush = new SolidBrush(Color.White);

        public enum VistaCPUInfoStyle
        {
            经典, 酷黑
        }

        private VistaCPUInfoStyle style = VistaCPUInfoStyle.经典;
        /// <summary>
        /// 风格
        /// </summary>
        public VistaCPUInfoStyle Style
        {
            get { return style; }
            set { style = value; }
        }

        private CustomRectangle positionRect = new CustomRectangle();
        /// <summary>
        /// 时钟的位置矩形
        /// </summary>
        public CustomRectangle PositionRect
        {
            get { return positionRect; }
            set { positionRect = value; }
        }

        #region 只读属性

        private Image Image
        {
            get
            {
                if (style == VistaCPUInfoStyle.酷黑)
                    return global::CNPOPSOFT.Controls.Properties.Resources.back_cool_lrg;
                else
                    return global::CNPOPSOFT.Controls.Properties.Resources.back_lrg;
            }
        }

        private Image ImageDial
        {
            get
            {
                if (style == VistaCPUInfoStyle.酷黑)
                    return global::CNPOPSOFT.Controls.Properties.Resources.dial_cool_lrg;
                else
                    return global::CNPOPSOFT.Controls.Properties.Resources.dial_lrg;
            }
        }

        private Image ImageDialDot
        {
            get
            {
                if (style == VistaCPUInfoStyle.酷黑)
                    return global::CNPOPSOFT.Controls.Properties.Resources.dialdot_cool_lrg;
                else
                    return global::CNPOPSOFT.Controls.Properties.Resources.dialdot_lrg;
            }
        }

        private Image ImageDialSmall
        {
            get
            {
                if (style == VistaCPUInfoStyle.酷黑)
                    return global::CNPOPSOFT.Controls.Properties.Resources.dial_cool_lrg_sml;
                else
                    return global::CNPOPSOFT.Controls.Properties.Resources.dial_lrg_sml;
            }
        }

        private Image ImageGlass
        {
            get
            {
                if (style == VistaCPUInfoStyle.酷黑)
                    return global::CNPOPSOFT.Controls.Properties.Resources.glass_cool_lrg;
                else
                    return global::CNPOPSOFT.Controls.Properties.Resources.glass_lrg;
            }
        }

        #endregion

        [DllImport("kernel32")]
        public static extern void GetWindowsDirectory(StringBuilder WinDir, int count);
        [DllImport("kernel32")]
        public static extern void GetSystemDirectory(StringBuilder SysDir, int count);
        [DllImport("kernel32")]
        public static extern void GetSystemInfo(ref  CPU_INFO cpuinfo);
        [DllImport("kernel32")]
        public static extern void GlobalMemoryStatus(ref  MEMORY_INFO meminfo);
        [DllImport("kernel32")]
        public static extern void GetSystemTime(ref  SYSTEMTIME_INFO stinfo);
        //定义CPU的信息结构  
        [StructLayout(LayoutKind.Sequential)]
        public struct CPU_INFO
        {
            public uint dwOemId;
            public uint dwPageSize;
            public uint lpMinimumApplicationAddress;
            public uint lpMaximumApplicationAddress;
            public uint dwActiveProcessorMask;
            public uint dwNumberOfProcessors;
            public uint dwProcessorType;
            public uint dwAllocationGranularity;
            public uint dwProcessorLevel;
            public uint dwProcessorRevision;
        }
        //定义内存的信息结构  
        [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;
        }
        //定义系统时间的信息结构  
        [StructLayout(LayoutKind.Sequential)]
        public struct SYSTEMTIME_INFO
        {
            public ushort wYear;
            public ushort wMonth;
            public ushort wDayOfWeek;
            public ushort wDay;
            public ushort wHour;
            public ushort wMinute;
            public ushort wSecond;
            public ushort wMilliseconds;
        }

        public VistaCPUInfo()
        {
            InitializeComponent();

            format.LineAlignment = StringAlignment.Center;
            format.Alignment = StringAlignment.Center;

            this.timer.Interval = 1000;
            this.timer.Tick  = new EventHandler(timer_Tick);

            timerCPU.Interval = 100;
            timerMem.Interval = 100;
            timerCPU.Tick  = new EventHandler(timerCPU_Tick);
            timerMem.Tick  = new EventHandler(timerMem_Tick);

            this.DoubleBuffered = true;
            this.BackColor = Color.Transparent;

            this.Paint  = new PaintEventHandler(VistaCPUInfo_Paint);
            this.Resize  = new EventHandler(VistaCPUInfo_Resize);
            this.MouseUp  = new MouseEventHandler(VistaCPUInfo_MouseUp);
        }

        void VistaCPUInfo_MouseUp(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right) contextMenuStrip1.Show(this, e.Location);
        }

        void timer_Tick(object sender, EventArgs e)
        {
            if (pc == null) pc = new PerformanceCounter("Processor", "% Processor Time", "_Total");
            float cpu = 30f, mem = 0f;
            if (!firstRun) cpu = (float)pc.NextValue();

            MEMORY_INFO MemInfo;
            MemInfo = new MEMORY_INFO();
            GlobalMemoryStatus(ref  MemInfo);
            mem = (float)MemInfo.dwMemoryLoad;

            if (firstRun || percentOfCPU != cpu)
            {
                percentOfCPU = cpu;
                float i = (float)Math.Round(percentOfCPU * 2.5) - 125f;

                timerCPU.Enabled = false;
                if ((i >= -360) && (i <= 360))
                    cpuAimAngle = i;
                else
                    cpuAimAngle = 0;
                timerCPU.Enabled = true;
            }
            if (firstRun || percentOfMemory != mem)
            {
                percentOfMemory = mem;
                timerMem.Enabled = true;

                float i = (float)Math.Round(percentOfMemory * 2.5) - 125f;

                timerMem.Enabled = false;
                memAimAngle = i;
                timerMem.Enabled = true;
            }

            if (firstRun) firstRun = false;
        }

        void timerMem_Tick(object sender, EventArgs e)
        {
            float i;
            if (memCurAngle > memAimAngle)
            {
                float intRotateStep = (float)Math.Max(1, Math.Round(Math.Abs(Math.Abs(memAimAngle) - Math.Abs(memCurAngle)) / 10d));
                i = memCurAngle - intRotateStep;
                if (i <= memAimAngle)
                {
                    timerMem.Enabled = false;
                    memCurAngle = memAimAngle;
                }
                else
                    memCurAngle = i;
            }
            else
            {
                float intRotateStep = (float)Math.Max(1, Math.Round(Math.Abs(Math.Abs(memAimAngle) - Math.Abs(memCurAngle)) / 10d));
                i = memCurAngle   intRotateStep;
                if (i >= memAimAngle)
                {
                    timerMem.Enabled = false;
                    memCurAngle = memAimAngle;
                }
                else
                {
                    if ((i >= -360) && (i <= 360)) memCurAngle = i;
                }
            }
            //this.Invalidate();
        }

        void timerCPU_Tick(object sender, EventArgs e)
        {
            float i;
            if (cpuCurAngle > cpuAimAngle)
            {
                float intRotateStep = (float)Math.Max(1, Math.Round(Math.Abs(Math.Abs(cpuAimAngle) - Math.Abs(cpuCurAngle)) / 10d));
                i = cpuCurAngle - intRotateStep;
                if (i <= cpuAimAngle)
                {
                    timerCPU.Enabled = false;
                    cpuCurAngle = cpuAimAngle;
                }
                else
                    cpuCurAngle = i;
            }
            else
            {
                float intRotateStep = (float)Math.Max(1, Math.Round(Math.Abs(Math.Abs(cpuAimAngle) - Math.Abs(cpuCurAngle)) / 10d));
                i = cpuCurAngle   intRotateStep;
                if (i >= cpuAimAngle)
                {
                    timerCPU.Enabled = false;
                    cpuCurAngle = cpuAimAngle;
                }
                else
                {
                    if ((i >= -360) && (i <= 360)) cpuCurAngle = i;
                }
            }
            this.Invalidate(positionRect.ToRectangle());
        }

        void VistaCPUInfo_Resize(object sender, EventArgs e)
        {
            positionRect.Width = 202;
            positionRect.Height = 159;
            positionRect.Top = (int)(this.ClientSize.Height < 159 ? 0 : (this.ClientSize.Height - 159) / 2f);
            positionRect.Left = (int)(this.ClientSize.Width < 202 ? 0 : (this.ClientSize.Width - 202) / 2f);

            if (!this.timer.Enabled) this.timer.Enabled = true;

            this.Invalidate();
        }

        void VistaCPUInfo_Paint(object sender, PaintEventArgs e)
        {
            e.Graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit;

            //背景
            e.Graphics.DrawImage(Image, (int)positionRect.X, (int)positionRect.Y, 198, 159);

            //指针
            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();

            //盖板
            e.Graphics.DrawImage(ImageDialDot, (int)positionRect.X, (int)positionRect.Y, 198, 150);

            //文字
            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);

            //玻璃
            e.Graphics.DrawImage(ImageGlass, (int)positionRect.X, (int)positionRect.Y, 198, 159);
        }

        private void mnu经典_Click(object sender, EventArgs e)
        {
            this.style = VistaCPUInfoStyle.经典;
            this.Invalidate();
        }

        private void mnu酷黑_Click(object sender, EventArgs e)
        {
            this.style = VistaCPUInfoStyle.酷黑;
            this.Invalidate();
        }
    }
}