本文共 5820 字,大约阅读时间需要 19 分钟。
public partial class Form2 : Form
{ public Form2() { InitializeComponent(); }private void button1_Click(object sender, EventArgs e)
{ Task.Factory.StartNew(() => { Form form = new Form(); form.StartPosition = FormStartPosition.CenterScreen; form.Width = 500; form.Height = 500; Button button = new Button(); form.Controls.Add(button); button.Location = new Point(0, 0); button.Width = 50; button.Height = 20; button.Click += new EventHandler(button_Test_Click); form.ShowDialog(); //或 //Application.Run(form) // Application.DoEvents();唤醒窗体 //form.Show(); });} private void button_Test_Click(object sender, EventArgs e) { Thread.Sleep(10000); }
Bitmap bit=new Bitmap(800,500);
private void button2_Click(object sender, EventArgs e) { SetParameter(this.pictureBox1, bit, 20, 400, 250); DrawPosition(-1, -1); }public void DrawPosition(int x,int y)
{ BufferedGraphicsContext current=BufferedGraphicsManager.Current; BufferedGraphics bg; bg=current.Allocate(_PictureBOx.CreateGraphics(),_PictureBOx.DisplayRectangle); Graphics g=bg.Graphics; g.FillRectangle(Brushes.Black, new Rectangle(0, 0, _PictureBOx.Width, _PictureBOx.Height)); g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias; //标准坐标图 Pen pen=new Pen(Color.OrangeRed,1); g.DrawLine(pen, new Point(GetX(0), GetY(_CenterY)), new Point(GetX(_bitMap.Width), GetY(_CenterY))); int countHorizon = 10; for (int i = 0; i < countHorizon; i++)//横线 { g.DrawLine(pen, new Point(GetX(i * _CenterX / countHorizon + _CenterX), GetY(_CenterY)), new Point(GetX(i * _CenterX / countHorizon + _CenterX), GetY(_CenterY) - 5)); g.DrawLine(pen, new Point(GetX(_CenterX - i * _CenterX / countHorizon), GetY(_CenterY)), new Point(GetX(_CenterX - i * _CenterX / countHorizon), GetY(_CenterY) - 5)); }g.DrawLine(pen, new Point(GetX(_CenterX), GetY(0)), new Point(GetX(_CenterX), GetY(_bitMap.Height)));
int countVertical = 6;
for (int i = 0; i < countVertical; i++)//纵线 { g.DrawLine(pen, new Point(GetX(_CenterX), GetY(_CenterY + i * _CenterY / countVertical)), new Point(GetX(_CenterX) + 5, GetY(_CenterY + i * _CenterY / countVertical))); g.DrawLine(pen, new Point(GetX(_CenterX), GetY(_CenterY - i * _CenterY / countVertical)), new Point(GetX(_CenterX) + 5, GetY(_CenterY - i * _CenterY / countVertical))); }Pen pen3 = new Pen(Color.Yellow, 1);
pen3.DashStyle = System.Drawing.Drawing2D.DashStyle.Dash; //虚线 pen3.DashPattern = new float[] {5, 10 }; //斜线 g.DrawLine(pen3, new Point(GetX(_CenterX), GetY(_CenterY)), new Point(GetX(_CenterX) - GetY(_CenterY), GetY(0))); g.DrawLine(pen3, new Point(GetX(_CenterX), GetY(_CenterY)), new Point(GetX(_CenterX) - GetY(_CenterY), GetY(_CenterY * 2))); g.DrawLine(pen3, new Point(GetX(_CenterX), GetY(_CenterY)), new Point(GetX(_CenterX) + GetY(_CenterY), GetY(0))); g.DrawLine(pen3, new Point(GetX(_CenterX), GetY(_CenterY)), new Point(GetX(_CenterX) + GetY(_CenterY), GetY(_CenterY * 2))); int elliValue = _CenterX / 4; int pad = 60; int coefficient = 0; for(int i=0;i<3;i++)//圆 { if (i == 0) continue; coefficient = -20;//Convert.ToInt32(pad*i); g.DrawEllipse(pen, new Rectangle(GetX(_CenterX - elliValue * i) + coefficient, GetY(_CenterY - elliValue * i) + coefficient,GetX( elliValue * i*2) - coefficient, GetY(elliValue * i*2) - coefficient)); }int tempWidth = 3;
int tempOffset = 25; Pen pen4= new Pen(Color.Red, 1); g.DrawLine(pen4, new Point(GetX(_CenterX) - tempWidth, GetY(_CenterY) - tempWidth), new Point(GetX(_CenterX) - tempWidth, GetY(_CenterY) - tempOffset));//纵上左 g.DrawLine(pen4, new Point(GetX(_CenterX) - tempWidth, GetY(_CenterY) + tempWidth), new Point(GetX(_CenterX) - tempWidth, GetY(_CenterY) + tempOffset));//纵下左 g.DrawLine(pen4, new Point(GetX(_CenterX) + tempWidth, GetY(_CenterY) - tempWidth), new Point(GetX(_CenterX) + tempWidth, GetY(_CenterY) - tempOffset));//纵上右 g.DrawLine(pen4, new Point(GetX(_CenterX) + tempWidth, GetY(_CenterY) + tempWidth), new Point(GetX(_CenterX) + tempWidth, GetY(_CenterY) + tempOffset));//纵下右g.DrawLine(pen4, new Point(GetX(_CenterX) - tempWidth, GetY(_CenterY) - tempWidth), new Point(GetX(_CenterX) - tempOffset, GetY(_CenterY) - tempWidth));//横上左
g.DrawLine(pen4, new Point(GetX(_CenterX) - tempWidth, GetY(_CenterY) + tempWidth), new Point(GetX(_CenterX) - tempOffset, GetY(_CenterY) + tempWidth));//横下左 g.DrawLine(pen4, new Point(GetX(_CenterX) + tempWidth, GetY(_CenterY) - tempWidth), new Point(GetX(_CenterX) + tempOffset, GetY(_CenterY) - tempWidth));//横上右 g.DrawLine(pen4, new Point(GetX(_CenterX) + tempWidth, GetY(_CenterY) + tempWidth), new Point(GetX(_CenterX) + tempOffset, GetY(_CenterY) + tempWidth));//横下右 //实时中心 if (x >= 0&&y>=0) { Pen pen2 = new Pen(Color.Green, 1); g.DrawEllipse(pen2, new Rectangle(GetX(x) - 5, GetY(y) - 5, 10, 10));//画中心点 g.FillEllipse(Brushes.Green, new Rectangle(GetX(x) - 5, GetY(y) - 5, 10, 10)); g.DrawLine(pen2, new Point(GetX(x), GetY(y) - 20), new Point(GetX(x), GetY(y) + 20)); g.DrawLine(pen2, new Point(GetX(x) - 20, GetY(y)), new Point(GetX(x) + 20, GetY(y))); } bg.Render(); bg.Dispose(); } int _Margin=20; int _CenterX=400; int _CenterY=250; PictureBox _PictureBOx; Bitmap _bitMap; public void SetParameter(PictureBox pictureBOx,Bitmap bitmap,int margin, int centerX, int centerY) { _Margin = margin; _CenterX = centerX; _CenterY = centerY; _PictureBOx = pictureBOx; _bitMap = bitmap; } public int GetX(double x) { x = (x / _bitMap.Width) * (_PictureBOx.Width - 2 * _Margin) + _Margin; return Convert.ToInt32(x);}
public int GetY(double y) { y = (y / _bitMap.Height) * (_PictureBOx.Height - 2 * _Margin) + _Margin; return Convert.ToInt32(y);}
private void button3_Click(object sender, EventArgs e)
{ // DrawPosition(Convert.ToInt32(textBox1.Text), Convert.ToInt32(textBox2.Text)); Random ran = new Random(); for(int i=0;i<1000;i++) { Thread.Sleep(100); DrawPosition(ran.Next(0, 800),ran.Next(0,500)); } }}
转载地址:http://srows.baihongyu.com/