生产设备图形模拟,以下为全部代码:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Drawing.Imaging;
namespace 曲线图画法
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.ComponentModel.IContainer components;
private Rectangle[] curveArea,BkArea;
private Graphics onScreenGraphics,Parent1;
private Graphics[] curveGraphics,BkGraphics;
private Bitmap[] ScreenBitmap,Bkmap;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Timer timer1;
private System.Windows.Forms.Panel panel2;
private System.Windows.Forms.Panel panel1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.HScrollBar hScrollBar1;
private System.Drawing.Pen curvePen;
private int count=30;
public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
// InitcurveData();
init();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.button1 = new System.Windows.Forms.Button();
this.timer1 = new System.Windows.Forms.Timer(this.components);
this.panel2 = new System.Windows.Forms.Panel();
this.panel1 = new System.Windows.Forms.Panel();
this.hScrollBar1 = new System.Windows.Forms.HScrollBar();
this.button2 = new System.Windows.Forms.Button();
this.panel2.SuspendLayout();
this.SuspendLayout();
//
// button1
//
this.button1.BackColor = System.Drawing.Color.LightSteelBlue;
this.button1.Location = new System.Drawing.Point(827, 8);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(77, 25);
this.button1.TabIndex = 0;
this.button1.Text = "button1";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// timer1
//
this.timer1.Interval = 300;
//
// panel2
//
this.panel2.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.panel2.Controls.Add(this.panel1);
this.panel2.Controls.Add(this.hScrollBar1);
this.panel2.Controls.Add(this.button2);
this.panel2.Location = new System.Drawing.Point(29, 226);
this.panel2.Name = "panel2";
this.panel2.Size = new System.Drawing.Size(858, 296);
this.panel2.TabIndex = 2;
//
// panel1
//
this.panel1.Location = new System.Drawing.Point(5, 30);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(2996, 229);
this.panel1.TabIndex = 2;
//
// hScrollBar1
//
this.hScrollBar1.Location = new System.Drawing.Point(4, 272);
this.hScrollBar1.Name = "hScrollBar1";
this.hScrollBar1.Size = new System.Drawing.Size(754, 18);
this.hScrollBar1.TabIndex = 3;
this.hScrollBar1.Scroll += new System.Windows.Forms.ScrollEventHandler(this.hScrollBar1_Scroll);
//
// button2
//
this.button2.Location = new System.Drawing.Point(5, 9);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(382, 15);
this.button2.TabIndex = 0;
this.button2.Visible = false;
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(911, 565);
this.Controls.Add(this.panel2);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
this.panel2.ResumeLayout(false);
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void Form1_Load(object sender, System.EventArgs e)
{
}
// private void InitcurveData()
// {
// curveArea = new Rectangle(100, 100, 400, 210);
// onScreenGraphics= this.CreateGraphics();
// ScreenBitmap = new Bitmap(curveArea.Width, curveArea.Height);
// curveGraphics = Graphics.FromImage(ScreenBitmap);
// curveGraphics.Clear(Color.Ivory);
// curvePen=new Pen(Color.Blue,1);
// Point p1=new Point(10,10);Point p1l=new Point(7,13);Point p1R=new Point(13,13);
// Point p2=new Point(10,200);
// Point p4=new Point(390,200);Point p4l=new Point(387,197);Point p4R=new Point(387,203);
Point[] points=new Point[]{new Point(10,100),new Point(30,106),new Point(50,120),new Point(100,150),new Point(120,120),new Point(150,100),new Point(170,90),new Point(200,70)};
// curveGraphics.DrawLine(curvePen,p1,p2);curveGraphics.DrawLine(curvePen,p1,p1l);curveGraphics.DrawLine(curvePen,p1,p1R);
// curveGraphics.DrawLine(curvePen,p2,p4);curveGraphics.DrawLine(curvePen,p4l,p4);curveGraphics.DrawLine(curvePen,p4R,p4);
curveGraphics.DrawCurve(curvePen,points);
// }
protected override void OnPaint(PaintEventArgs e)
{
// for(int i=0;i<10;i++)
// {
// onScreenGraphics.DrawImage(ScreenBitmap[i], curveArea[i]);
//
// }
// base.OnPaint (e);
// onScreenGraphics.Clear(Color.Linen);
for(int i=0;i<count;i++)
{
onScreenGraphics.DrawImage(ScreenBitmap[i], curveArea[i]);
onScreenGraphics.DrawImage(Bkmap[i], BkArea[i]);
Font ft=new Font(FontFamily.GenericSerif,12);
onScreenGraphics.DrawString((i+1).ToString()+"#",ft,new SolidBrush(Color.Red),7+i*50,185);
}
}
private void init()
{
try
{
panel1.Width=80*count+20;
curvePen=new Pen(Color.Orange,2);
button2.Width=panel1.Width;
hScrollBar1.Width=panel2.Width-10;
curveGraphics=new Graphics[count];
BkGraphics=new Graphics[count];
ScreenBitmap=new Bitmap[count];
BkArea=new Rectangle[count];
Bkmap=new Bitmap[count];
curveArea=new Rectangle[count];
onScreenGraphics= panel1.CreateGraphics();
Parent1=panel2.CreateGraphics();
for(int i=0;i<count;i++)
{
//------------------------
curveArea[i] = new Rectangle(5+i*50, 100+i*2, 30, 80-i*2);
ScreenBitmap[i] = new Bitmap(curveArea[i].Width, curveArea[i].Height);
curveGraphics[i] = Graphics.FromImage(ScreenBitmap[i]);
curveGraphics[i].Clear(Color.MidnightBlue);
//-------------
BkArea[i] = new Rectangle(5+i*50, 100, 30, 80);
Bkmap[i] = new Bitmap(BkArea[i].Width, BkArea[i].Height);
BkGraphics[i] = Graphics.FromImage(Bkmap[i]);
BkGraphics[i].DrawRectangle(curvePen,1, 1, 28, 78);
}
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
// }
}
int k=1;
private void button1_Click(object sender, System.EventArgs e)
{
}
int i=1;
private Point[] GetPoints()
{
Point[] temp=new Point[8];
Point[] points1=new Point[]{new Point(10,100),new Point(30,106),new Point(50,120),new Point(100,150),new Point(120,120),new Point(150,100),new Point(170,60),new Point(200,70)};
Point[] points2=new Point[]{new Point(10,100),new Point(30,116),new Point(50,119),new Point(100,140),new Point(120,125),new Point(150,90),new Point(170,70),new Point(200,80)};
Point[] points3=new Point[]{new Point(10,100),new Point(30,126),new Point(50,118),new Point(100,130),new Point(120,130),new Point(150,80),new Point(170,80),new Point(200,90)};
Point[] points4=new Point[]{new Point(10,100),new Point(30,136),new Point(50,117),new Point(100,120),new Point(120,135),new Point(150,70),new Point(170,90),new Point(200,100)};
Point[] points5=new Point[]{new Point(10,100),new Point(30,146),new Point(50,116),new Point(100,110),new Point(120,140),new Point(150,60),new Point(170,100),new Point(200,110)};
Point[] points6=new Point[]{new Point(10,100),new Point(30,156),new Point(50,115),new Point(100,100),new Point(120,145),new Point(150,50),new Point(170,110),new Point(200,130)};
if(i==1)
{
temp= points1;
}
if(i==2)
{
temp= points2;
}
if(i==3)
{
temp= points3;
}
if(i==4)
{
temp= points4;
}
if(i==5)
{
temp= points5;
}
if(i==6)
{
temp= points6;
}
i++;
if(i>6)
{
i=1;
temp= points1;
}
return temp;
}
// private void timer1_Tick(object sender, System.EventArgs e)
// {
// Point[] temp=new Point[8];
// temp=GetPoints();
// InitcurveData();
// curveGraphics.DrawCurve(curvePen,temp);
// onScreenGraphics.DrawImage(ScreenBitmap, curveArea);//不可用this.refresh(),闪烁太厉害,用这个重绘就好了
//
//
// }
private void hScrollBar1_Scroll(object sender, System.Windows.Forms.ScrollEventArgs e)
{
panel1.Left=13-e.NewValue*panel1.Width/panel2.Width;
// onScreenGraphics.Clear(Color.Linen);
for(int i=0;i<count;i++)
{
onScreenGraphics.DrawImage(ScreenBitmap[i], curveArea[i]);
onScreenGraphics.DrawImage(Bkmap[i], BkArea[i]);
Font ft=new Font(FontFamily.GenericSerif,12);
onScreenGraphics.DrawString((i+1).ToString()+"#",ft,new SolidBrush(Color.Red),7+i*50,185);
}
}
}
}