雁过请留痕...
代码改变世界

颜色分段显示,线性填充的一个Demo

2012-08-02 17:51  xiashengwang  阅读(1751)  评论(0编辑  收藏  举报

1,指定一个开始颜色,一个结束颜色,进行填充。

2,生成指定数量的,这两个颜色中间的过渡色,这个对地图表示等功能需要分段颜色表示的很有用。

效果图:

先看第一个功能,填充渐变的颜色:

主要就是利用LinearGradientBrush这个画刷

            lblStartColor.Click += new EventHandler(lblColor_Click);
            lblEndColor.Click += new EventHandler(lblColor_Click);
        void lblColor_Click(object sender, EventArgs e)
        {
            var control = sender as Control;
            if (control != null)
            {
                ColorDialog colrDlg = new ColorDialog();
                colrDlg.Color = control.BackColor;
                if (colrDlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    control.BackColor = colrDlg.Color;
                    DoRefreshPicture();
                }            
            }
        }
        private void DoRefreshPicture()
        {
            LinearGradientBrush brush = new LinearGradientBrush(this.pictureBox1.ClientRectangle, this.lblStartColor.BackColor,
                this.lblEndColor.BackColor, LinearGradientMode.Horizontal);
            Bitmap bmp = new Bitmap(this.pictureBox1.ClientRectangle.Width, this.pictureBox1.ClientRectangle.Height);
            using (Graphics g = Graphics.FromImage(bmp))
            {
                g.FillRectangle(brush, new Rectangle(0, 0, bmp.Width, bmp.Height));
            }
            brush.Dispose();
            this.pictureBox1.Image = bmp;
        }

下面是颜色分段实现的主要代码。

主要就是算两个颜色的差值,重新生成一个新的颜色,然后在一个Panel里动态生成一些Label。没什么技术含量。

定义一个辅助分段颜色的类:

        private class ColorSplitor
        {
            private Color _startColor;
            private Color _endColor;
            private int _splitCount;
            public ColorSplitor(Color sColor, Color eColor, int splitCount)
            {
                _startColor = sColor;
                _endColor = eColor;
                _splitCount = splitCount;
            }
            public Color  GetItemColor(int index)
            {
                if (index <= 0) return _startColor;
                if (index >= _splitCount) return _endColor;

                int r = _startColor.R + (_endColor.R - _startColor.R) * index / _splitCount;
                int g = _startColor.G + (_endColor.G - _startColor.G) * index / _splitCount;
                int b = _startColor.B + (_endColor.B - _startColor.B) * index / _splitCount;

                return Color.FromArgb(r, g, b);
            }
        }
        //分段显示
        private void button1_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.textBox1.Text)) return;
            int count = 0;
            if (int.TryParse(this.textBox1.Text, out count) && count > 0 && count < 15)
            {
                this.panel1.Controls.Clear();
                this.panel1.AutoSize = true;

                ColorSplitor splitor = new ColorSplitor(this.lblStartColor.BackColor,
                    this.lblEndColor.BackColor, count);
                int verticalPositon = 10;
                for (var i = 0; i < count; i++)
                {
                    Label lable = new Label();
                    lable.AutoSize = false;
                    lable.BorderStyle = BorderStyle.FixedSingle;
                    lable.Name = "lable" + i;
                    lable.Size = new Size(100, 20);
                    lable.BackColor = splitor.GetItemColor(i);
                    lable.Location = new Point(10, verticalPositon);

                    verticalPositon += 30;
                    this.panel1.Controls.Add(lable);
                }
            } 
        }