注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

信息 灵感 创新

I? =Information,Inspiration,Innovation

 
 
 

日志

 
 
关于我

we are 5. Mathematics, Computation, Programming, Engineering, and Making fun of life.

网易考拉推荐

Graphics Programming with GDI+ 学习笔记(四)  

2011-10-26 21:42:26|  分类: C# & .NET |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

Graphics Programming with GDI+ 学习笔记(一) - Castor - 趁年轻,多折腾

 第三章 Graphics对象(2)

绘制一个Pie

第二个例子是绘制饼图,用到了一个自定义的类,方便了颜色的管理和提取,这一点很值得学习,不过原文中是用的结构体,不喜欢我就直接用类了。程序比较简单,直接上代码:

using System;

using System.Windows.Forms;

using System.Drawing;

using System.Drawing.Drawing2D;

 

namespace LearnWindows

{

    public partial class Form1 : Form

    {

        double total=0;

        public Form1()

        {

            InitializeComponent();

            button3.BackColor = Color.Blue;

        }

 

        private void button1_Click(object sender, EventArgs e)

        {

            if (textBox1.Text == "") return;

            try

            {

                double d = double.Parse(textBox1.Text);

                if (d <= 0)

                    throw new System.Exception();

                else

                {

                    total += d;

                    DataWithColor dc = new DataWithColor(d,button3.BackColor);

                    listBox1.Items.Add(dc);

                }

            }

            catch

            {

                textBox1.Text = "";

                MessageBox.Show("请输入正数!");

            }

 

            int r, g, b;

            Random rdm = new Random();

            r = rdm.Next(255);

            g = rdm.Next(255);

            b = rdm.Next(255);

            button3.BackColor = Color.FromArgb(255, r, g, b);

        }

 

        private void button2_Click(object sender, EventArgs e)

        {

            int cnt = listBox1.Items.Count;

            if (cnt == 0)

                return;

            double[] piece = new double[cnt];

            Graphics gr = pictureBox1.CreateGraphics();

            gr.Clear(pictureBox1.BackColor);

            Rectangle rect = new Rectangle(0,0,pictureBox1.Width,pictureBox1.Height);

            float angle = 0;

            float sweep = 0;

            foreach (object obj in listBox1.Items)

            {

                DataWithColor dc = obj as DataWithColor;

                sweep = (float)(360*dc.value / total);

                gr.FillPie(new SolidBrush(dc.color), rect, angle, sweep);

                angle += sweep;

            }

            gr.Dispose();

        }

 

        private void button4_Click(object sender, EventArgs e)

        {

            listBox1.Items.Clear();

            total = 0;

            Graphics gr = pictureBox1.CreateGraphics();

            gr.Clear(pictureBox1.BackColor);

            gr.Dispose();

        }

 

        private void button3_Click(object sender, EventArgs e)

        {

            ColorDialog cd=new ColorDialog();

            if (cd.ShowDialog(this) == DialogResult.OK)

                button3.BackColor = cd.Color;

        }

    }

 

    public class DataWithColor

    {

        public double value;

        public Color color;

        public DataWithColor(double d, Color c)

        {

            this.value = d;

            this.color = c;

        }

        public override string ToString()

        {

            return string.Format("{0}:{1}", value, color.ToArgb());

        }

    }

}

下面是运行效果:

Graphics Programming with GDI+ 学习笔记(四) - Castor - 趁年轻,多折腾

  重写了ToString方法,这样在列表框中显示出来的效果会好点。颜色可以随机生成,也可以手动修改颜色。

  评论这张
 
阅读(869)| 评论(1)
推荐 转载

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2016