cpu使用率实时曲线图_.NET_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > .NET > cpu使用率实时曲线图

cpu使用率实时曲线图

 2013/7/10 22:14:52  Diose  博客园  我要评论(0)
  • 摘要:闲着没事搞2个panel叠加在内部panel上面绘制及宽度增加2像素往左移动2像素设计器代码#regionWindows窗体设计器生成的代码///<summary>///设计器支持所需的方法-不要///使用代码编辑器修改此方法的内容。///</summary>privatevoidInitializeComponent(){this.button1=newSystem.Windows.Forms.Button();this.label1=newSystem
  • 标签:使用

闲着没事搞

2个panel叠加 在内部panel上面绘制 及宽度增加2像素 往左移动2像素

 

 

设计器代码

        #region Windows 窗体设计器生成的代码

        /// <summary>
        /// 设计器支持所需的方法 - 不要
        /// 使用代码编辑器修改此方法的内容。
        /// </summary>
        private void InitializeComponent() {
            this.button1 = new System.Windows.Forms.Button();
            this.label1 = new System.Windows.Forms.Panel();
            this.panel1 = new System.Windows.Forms.Panel();
            this.panel1.SuspendLayout();
            this.SuspendLayout();
            // 
            // button1
            // 
            this.button1.Location = new System.Drawing.Point(12, 249);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(75, 23);
            this.button1.TabIndex = 1;
            this.button1.Text = "start";
            this.button1.UseVisualStyleBackColor = true;
            this.button1.Click += new System.EventHandler(this.button1_Click);
            // 
            // label1
            // 
            this.label1.BackColor = System.Drawing.Color.Black;
            this.label1.Location = new System.Drawing.Point(-3, -2);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(480, 130);
            this.label1.TabIndex = 2;
            this.label1.Paint += new System.Windows.Forms.PaintEventHandler(this.label1_Paint);
            // 
            // panel1
            // 
            this.panel1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
            this.panel1.Controls.Add(this.label1);
            this.panel1.Location = new System.Drawing.Point(10, 76);
            this.panel1.Name = "panel1";
            this.panel1.Size = new System.Drawing.Size(480, 130);
            this.panel1.TabIndex = 3;
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(499, 297);
            this.Controls.Add(this.panel1);
            this.Controls.Add(this.button1);
            this.Name = "Form1";
            this.Text = "Form1";
            this.panel1.ResumeLayout(false);
            this.ResumeLayout(false);

        }

        #endregion

        private System.Windows.Forms.Button button1;
        private System.Windows.Forms.Panel label1;
        private System.Windows.Forms.Panel panel1;

 

代码

     private const string CategoryName = "Processor";
        private const string CounterName = "% Processor Time";
        private const string InstanceName = "_Total";
        private List<Point> allpoint = new List<Point>();
        PerformanceCounter pc = new PerformanceCounter(CategoryName, CounterName, InstanceName);
        private delegate void MyDelegate();

        public Form1() {
            InitializeComponent();
        }

        private void label1_Paint(object sender, PaintEventArgs e) {

            Graphics g = e.Graphics;
            #region 网格部分
            Pen p = new Pen(Color.FromArgb(0, 128, 64));
            int xlines = Convert.ToInt32(label1.Height / 12);
            int ylines = Convert.ToInt32(label1.Width / 12);

            for (int i = 1; i <= ylines; i++) {
                g.DrawLine(p, i * 12, 0, i * 12, label1.Height);
            }

            for (int i = 1; i <= xlines; i++) {
                g.DrawLine(p, 0, i * 12, label1.Width, i * 12);
            }
            #endregion

            int total = Convert.ToInt32(Math.Round(pc.NextValue(), 0, MidpointRounding.ToEven));

            if (allpoint.Count == 0) {
                allpoint.Add(new Point(label1.Width - 3, label1.Height));
            }

            Point temp = new Point(label1.Width, label1.Height - Convert.ToInt32((total / 100M) * label1.Height));
            allpoint.Add(temp);

            if (allpoint.Count > 1) {
                g.DrawLines(new Pen(Color.FromArgb(0, 255, 0)), allpoint.ToArray());
            }

            StringBuilder sb = new StringBuilder();
            sb.AppendLine("width:" + label1.Width);
            sb.AppendLine("yHeight:" + Convert.ToInt32((total / 100M) * label1.Height));
            sb.AppendLine("ylines:" + ylines.ToString());
            sb.AppendLine("xlines:" + xlines.ToString());
            sb.AppendLine("cpu total:" + total.ToString() + "%");
            Graphics g2 = this.CreateGraphics();
            g2.DrawString(sb.ToString(), new Font("宋体", 9), new SolidBrush(Color.Red), 0, 0);
            g2.Dispose();
            g.Dispose();
        }

        private void Setlbl() {
            label1.Width += 2;
            label1.Location = new Point(label1.Location.X - 2, label1.Location.Y);
        }

        private void InvokeColor() {
            while (true) {
                Thread.Sleep(2000);
                if (this.InvokeRequired) {
                    this.BeginInvoke(new MyDelegate(Setlbl));
                }
                else {
                    Setlbl();
                }
                this.Invalidate();
            }
        }

        private void button1_Click(object sender, EventArgs e) {
            Thread th = new Thread(new ThreadStart(InvokeColor));
            th.IsBackground = true;
            th.Start();
        }

 

发表评论
用户名: 匿名