C#实现动态数据绘图graphic的方法示例

 更新时间:2017年09月05日 12:16:18   作者:5t4rk  
这篇文章主要介绍了C#实现动态数据绘图graphic的方法,结合实例形式分析了C#根据动态数据绘制2D数据表格的相关操作技巧,需要的朋友可以参考下

本文实例讲述了C#实现动态数据绘图graphic的方法。分享给大家供大家参考,具体如下:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
namespace Drawing
{
  public partial class Form1 : Form
  {
    public Form1()
    {
      InitializeComponent();
    }
    private int WidthAdded = 0;
    private int HeightAdded = 0;
    private int Added_Value = 15;
    private int Incremented = 3;
    private double Width_Max = 0.0f;
    private double Height_Max = 0f;
    private string hstrlink;
    private string vstrlink;
    private const int hor_kedu=17;
    private const int ver_kedu = 13;
    private double HelpRdm = 0;
    private void Form1_Load(object sender, EventArgs e)
    {
      Width_Max=Convert.ToDouble(panel.ClientSize.Width);
      Height_Max = Convert.ToDouble(panel.ClientSize.Height);
      BtnStatusInfoForb();
    }
    private void button1_Click(object sender, EventArgs e)
    {
      Graphics myGraphics_framwork;
      Pen myPen = new Pen(Color.Blue, 0.25f);
      myGraphics_framwork = panel.CreateGraphics();
      for (int Increment = 0; Increment < panel.Height; Increment += Incremented)
      {
        myGraphics_framwork.DrawLine(myPen, 0, HeightAdded, panel.ClientSize.Width, HeightAdded);
        HeightAdded = HeightAdded + Added_Value;
      }
      for (int Increment = 0; Increment < panel.Height; Increment += Incremented)
      {
        myGraphics_framwork.DrawLine(myPen, WidthAdded, 0, WidthAdded, panel.ClientSize.Height);
        WidthAdded = WidthAdded + Added_Value;
      }
      myGraphics_framwork.Dispose();
      for (int hstart = 0; hstart < Width_Max; hstart += hor_kedu)
      {
        hstrlink = hstrlink + hstart.ToString() + "-";
      }
      label2.Text = hstrlink;
      for (int start = (int)Height_Max; start > 0; start -= ver_kedu)
      {
        vstrlink = vstrlink + start.ToString() + "-" + "\n";
      }
      label3.Text = vstrlink + "0-";
      BtnStatusInfoActi();
      button1.Enabled = false;
    }
    private void BtnStatusInfoForb()
    {
      button2.Enabled = false;
      button3.Enabled = false;
      button4.Enabled = false;
      trackBar1.Enabled = false;
      Rec_NumBox.Enabled = false;
    }
    private void BtnStatusInfoActi()
    {
      button2.Enabled = true;
      button3.Enabled = true;
      button4.Enabled = true;
      trackBar1.Enabled = true;
      Rec_NumBox.Enabled = true;
    }
    private void myfun()
    {
      Graphics myGraphics = panel.CreateGraphics();
      Font myFont = new Font("Times New Roman", 72, FontStyle.Italic);
      Point startPoint = new Point(0, 0);
      Point endPoint = new Point(30, 30);
      LinearGradientBrush myBrush = new LinearGradientBrush(startPoint, endPoint, Color.Black, Color.Yellow);
      myBrush.WrapMode = WrapMode.TileFlipXY;
      myGraphics.DrawString("String", myFont, myBrush, 0, 0);
    }
    private void button2_Click(object sender, EventArgs e)
    {
      if (ParaBox1.Text == null)
      {
        MessageBox.Show("请输入矩形的高度");
      }
      else
      {
        try
        {
          Graphics myGraphics_Rectangle = panel.CreateGraphics();
          Pen myPen_Rec = new Pen(Color.Red, 3);
          Rectangle rect = new Rectangle();
          rect.X = 20; rect.Y = panel.ClientSize.Height - Convert.ToInt32(ParaBox1.Text);
          rect.Width = 20; rect.Height = panel.ClientSize.Height;
          myGraphics_Rectangle = panel.CreateGraphics();
          myGraphics_Rectangle.DrawRectangle(myPen_Rec, rect);
          SolidBrush MyRectBrush = new SolidBrush(Color.Purple);
          myGraphics_Rectangle.FillRectangle(MyRectBrush, rect);
          myGraphics_Rectangle.Dispose();
        }
        catch (Exception Excp)
        {
          MessageBox.Show(Excp.Message);
        }
      }
    }
    /// <summary>
    ///
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void button3_Click(object sender, EventArgs e)
    {
      Graphics myGraphics_Rectangle = panel.CreateGraphics();
      if (Rec_NumBox == null)
      {
        MessageBox.Show("请输入你要绘制的矩形数量");
      }
      else
      {
        double count = Convert.ToInt32(Rec_NumBox.Text);
        double TableSpace = (1 * Width_Max) / (4 * count);
        double TableWidth = (3 * Width_Max) / (4 * count);
        double RecOffset = TableWidth + TableSpace;//
        Pen myPen_Rec = new Pen(Color.Red, 3);
        for (long RectNum = 0; RectNum < count; RectNum++)
        {
          Random Radom = new Random();
          System.Threading.Thread.Sleep(20);
          double RandomHegiht =Radom.Next(0, Convert.ToInt32(Height_Max));
          Rectangle rect = new Rectangle();
          rect.X =Convert.ToInt32(RectNum*RecOffset);
          rect.Y = Convert.ToInt32(panel.ClientSize.Height - RandomHegiht);
          rect.Width = Convert.ToInt32(TableWidth);
          rect.Height = panel.ClientSize.Height;
          myGraphics_Rectangle = panel.CreateGraphics();
          myGraphics_Rectangle.DrawRectangle(myPen_Rec, rect);
          SolidBrush MyRectBrush = new SolidBrush(Color.Green);
          myGraphics_Rectangle.FillRectangle(MyRectBrush, rect);
        }
      myGraphics_Rectangle.Dispose();
      }
      Rec_NumBox.Text = Added_Value.ToString();
      Added_Value = Added_Value+50;
    }
    private void button4_Click(object sender, EventArgs e)
    {
      StringFormat sf=new StringFormat();
      sf.Alignment = StringAlignment.Near;
      Graphics myGraphics = panel.CreateGraphics();
      RectangleF REf = new RectangleF(12, 30, 8, 38);
      Font myFont = new Font("Times New Roman", 62, FontStyle.Italic);
      Point startPoint = new Point(0, 0);
      Point endPoint = new Point(30, 30);
      LinearGradientBrush myBrush = new LinearGradientBrush(startPoint, endPoint, Color.Green, Color.Blue);
      SolidBrush MyRectBrush = new SolidBrush(Color.Purple);
      myBrush.WrapMode = WrapMode.TileFlipXY;
      myGraphics.DrawString("b2spirit", myFont, myBrush, 0, 0);
      Random r=new Random();
      int temp=r.Next(10,300);
      myGraphics.DrawString(temp.ToString(), myFont, MyRectBrush, REf, sf);
    }
    private void trackBar1_Scroll(object sender, EventArgs e)
    {
      TrackBar myTB=new TrackBar();
      myTB=(TrackBar)sender;
      Rec_NumBox.Text = myTB.Value.ToString();
    }
  }
}

截图

更多关于C#相关内容感兴趣的读者可查看本站专题:《C#图片操作技巧汇总》、《C#常见控件用法教程》、《WinForm控件用法总结》、《C#数据结构与算法教程》、《C#面向对象程序设计入门教程》及《C#程序设计之线程使用技巧总结

希望本文所述对大家C#程序设计有所帮助。

相关文章

  • C#实现用户自定义控件中嵌入自己的图标

    C#实现用户自定义控件中嵌入自己的图标

    这篇文章主要介绍了C#实现用户自定义控件中嵌入自己的图标,较为详细的分析了C#实现自定义控件中嵌入图标的具体步骤与相关实现技巧,需要的朋友可以参考下
    2016-03-03
  • Unity 读取文件 TextAsset读取配置文件方式

    Unity 读取文件 TextAsset读取配置文件方式

    这篇文章主要介绍了Unity 读取文件 TextAsset读取配置文件的实例讲解,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2021-04-04
  • C#开发答题赢钱游戏(自动答题器)

    C#开发答题赢钱游戏(自动答题器)

    现在最火的直播游戏,那就是答题赢钱直播了,如百万英雄、芝士超人、花椒直播、冲顶大会等等,这些游戏的玩法都很简单,答对12题即可瓜分奖金了。玩法虽简单但是完全答对12题难度就挺高了,下面小编给大家带来了C#开发答题赢钱游戏,需要的朋友参考下吧
    2018-01-01
  • c#代码自动修改解决方案下任意文件实例

    c#代码自动修改解决方案下任意文件实例

    这篇文章主要介绍了c#代码自动修改解决方案下任意文件实例,有需要的朋友可以参考一下
    2013-11-11
  • C#连接Mysql实现增删改查的操作

    C#连接Mysql实现增删改查的操作

    在IT行业中,数据库连接是应用程序开发中的重要环节,尤其是在使用C#进行Windows或者Web应用开发时,经常需要与各种数据库进行交互,其中就包括广泛使用的MySQL,本篇将详细讲解如何使用C#语言来连接MySQL数据库,以实现数据的读取、写入和其他操作
    2024-09-09
  • C#中Foreach循环遍历的本质与枚举器详解

    C#中Foreach循环遍历的本质与枚举器详解

    这篇文章主要给大家介绍了关于C#中Foreach循环遍历本质与枚举器的相关资料,foreach循环用于列举出集合中所有的元素,foreach语句中的表达式由关键字in隔开的两个项组成,本文通过示例代码介绍的非常详细,需要的朋友可以参考下
    2021-08-08
  • c# 爬取优酷电影信息(1)

    c# 爬取优酷电影信息(1)

    这篇文章主要介绍了c# 如何爬取优酷电影信息,帮助大家更好的理解和学习使用c#,感兴趣的朋友可以了解下
    2021-02-02
  • Unity3D使用GL实现图案解锁功能

    Unity3D使用GL实现图案解锁功能

    这篇文章主要为大家详细介绍了Unity3D使用GL实现图案解锁功能,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-03-03
  • C#文件路径Path类介绍

    C#文件路径Path类介绍

    这篇文章介绍了C#中的文件路径Path类,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-05-05
  • C#中括号强转、as、is区别详解

    C#中括号强转、as、is区别详解

    本文主要介绍了C#中括号强转、as、is区别详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-02-02

最新评论