在好例子网,分享、交流、成长!
您当前所在位置:首页C# 开发实例C#语言基础 → C# 仪表盘 例子源码

C# 仪表盘 例子源码

C#语言基础

下载此实例
  • 开发语言:C#
  • 实例大小:0.76M
  • 下载次数:112
  • 浏览次数:2018
  • 发布时间:2014-11-04
  • 实例类别:C#语言基础
  • 发 布 人:abweixx
  • 文件格式:.rar
  • 所需积分: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();
        }
    }
}

标签: 仪表盘

实例下载地址

C# 仪表盘 例子源码

不能下载?内容有错? 点击这里报错 + 投诉 + 提问

好例子网口号:伸出你的我的手 — 分享

网友评论

发表评论

(您的评论需要经过审核才能显示)

查看所有0条评论>>

小贴士

感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。

  • 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
  • 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
  • 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
  • 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。

关于好例子网

本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明

;
报警