C#工作線程創建窗體及繪製星圖實例

   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));
            }
        }

    }

發佈了185 篇原創文章 · 獲贊 274 · 訪問量 110萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章