• GDI绘制柱状图


    实现效果:

    1,添加三个按钮

    2,添加两个TextBox控件,利用KeyPress事件来控制不让有其他字母输进来。

    3,添加两个Lable标签用来跟随图形移动,位置提前测量出来。

    4.添加Timer组件,设置Enabled为True,然后再利用Timer的事件。

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    
    namespace GDI柱状图
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
            //定义1号柱状图参数
            int RECX1 = 215;
            int RECY1 = 325;
            int RECWidth1 = 20;
            int RECHeight1 = 0;
    
            //定义2号柱状图参数
            int RECX2 = 340;
            int RECY2 = 325;
            int RECWidth2 = 20;
            int RECHeight2 = 0;
    
            //定义启动
            bool run = false;
            //利用集合来清空画板
            public List<Graphics> g = new List<Graphics>();
            private void timer1_Tick(object sender, EventArgs e)
            {
                if (run == true)
                {
                    //第一条柱状图
                    if (Convert.ToInt32( textBox1.Text) !=0)
                    {
                        //填充高度
                        RECHeight1 = RECHeight1 + Convert.ToInt32(this.textBox1.Text);
                        //每次填充Y点位置都要向上增加
                        RECY1 = RECY1 - Convert.ToInt32(this.textBox1.Text);
                        this.label3.Location = new Point(215, this.label3.Location.Y - Convert.ToInt32(this.textBox1.Text));
                        Graphics g1 = this.CreateGraphics();
                        this.label3.Text = (Convert.ToInt32(this.label3.Text) + 1).ToString();
                        g1.FillRectangle(Brushes.Blue, RECX1, RECY1, RECWidth1, RECHeight1);
                        g.Add(g1);
                    }
                    //第二条柱状图
                    if (Convert.ToInt32(textBox2.Text) != 0)
                    {
                        RECHeight2 = RECHeight2 + Convert.ToInt32(this.textBox2.Text);
                        RECY2 = RECY2 - Convert.ToInt32(this.textBox2.Text);
                        this.label4.Location = new Point(340, this.label4.Location.Y - Convert.ToInt32(this.textBox2.Text));
                        Graphics g2 = this.CreateGraphics();
                        this.label4.Text = (Convert.ToInt32(this.label4.Text) + 1).ToString();
                        g2.FillRectangle(Brushes.Blue, RECX2, RECY2, RECWidth2, RECHeight2);
                        g.Add(g2);
                    }
                }
            }
            //启动按钮
            private void button1_Click(object sender, EventArgs e)
            {
                this.run = true;
            }
            //停止按钮
            private void button2_Click(object sender, EventArgs e)
            {
                this.run = false;
            }
            //清除按钮
            private void button3_Click(object sender, EventArgs e)
            {
                foreach (var item in g)
                {
                    item.Clear(this.BackColor);
                    this.label3.Location = new Point(215, 295);
                    this.label3.Text = "00";
                    this.label4.Location = new Point(340, 295);
                    this.label4.Text = "00";
                    RECX1 = 215;
                    RECY1 = 325;
                    RECWidth1 = 20;
                    RECHeight1 = 0;
                    RECX2 = 340;
                    RECY2 = 325;
                    RECWidth2 = 20;
                    RECHeight2 = 0;
                }
            }
    
            private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
            {
                if ((e.KeyChar != 8 && !char.IsDigit(e.KeyChar)) && e.KeyChar != 13)
                {
                    MessageBox.Show("请输入数字", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
                    e.Handled = true;
                }
            }
    
            private void textBox2_KeyPress(object sender, KeyPressEventArgs e)
            {
                if ((e.KeyChar != 8 && !char.IsDigit(e.KeyChar)) && e.KeyChar != 13)
                {
                    MessageBox.Show("请输入数字", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
                    e.Handled = true;
                }
            }
        }
    }
    GDI柱状图
  • 相关阅读:
    Flex动画
    八大排序算法
    Android switch语句“case expressions must be constant expressions”
    MySQL修改root密码的多种方法
    Android中ListView控件onItemClick事件中获取listView传递的数据
    超详细Android接入支付宝支付实现,有图有真相
    Android蓝牙开发---与蓝牙模块进行通信
    Leecode no.19 删除链表的倒数第 N 个结点
    玩转java静态/动态代理
    Leecode no.198. 打家劫舍
  • 原文地址:https://www.cnblogs.com/Luck1996/p/11979944.html
Copyright © 2020-2023  润新知