C# winform 動態畫圖


因為自己剛接觸winform一星期,沒有找到動態繪圖的方法,所以自己寫了個,寫的不好,請輕拍,請指教~

自定義一個控件,然后實現:

    public partial class CheckResult : UserControl
    {
        public CheckResult()
        {
            InitializeComponent();
        }
        private void CheckResult_Load(object sender, EventArgs e)
        {

        }
        /// <summary>
        /// 檢測失敗,划叉號
        /// </summary>
        public void DrawFailMark()
        {
            int brushWidth = 14;
            Point startPoint = new Point(50, 3);
            Pen pen = new Pen(Brushes.Red, brushWidth);
            int x = startPoint.X;
            int y = startPoint.Y;
            using (Graphics g1 = this.pictureBox1.CreateGraphics())
            {
                g1.Clear(this.BackColor);
                DrawFailMarkLines(false, g1, pen, x, y, startPoint);
                g1.Save();
            }
            //以上動態畫出的線在窗口縮小或隱藏后重新顯示時會因為form的重繪而消失,所以需要用Bitmap再畫一遍
            this.pictureBox1.Refresh();
            Bitmap bmp = new Bitmap(this.pictureBox1.Width, this.pictureBox1.Height);
            Graphics g2 = Graphics.FromImage(bmp);
            g2.Clear(this.BackColor);
            DrawFailMarkLines(true, g2, pen, x, y, startPoint);
            g2.Dispose();
            this.pictureBox1.Image = bmp;
        }

        private void DrawFailMarkLines(bool isRedraw, Graphics g, Pen pen, int x, int y, Point startPoint)
        {
            for (int i = 0; i < 14; i++)
            {
                g.DrawLine(pen, x, y, x + 10, y + 10);
                x += 10;
                y += 10;
                i++;
                if (!isRedraw)
                {
                    Thread.Sleep(50);//通過線程sleep實現動態繪圖
                }
            }
            y = startPoint.Y;
            for (int i = 0; i < 14; i++)
            {
                g.DrawLine(pen, x, y, x - 10, y + 10);
                x -= 10;
                y += 10;
                i++;
                if (!isRedraw)
                {
                    Thread.Sleep(50);//通過線程sleep實現動態繪圖
                }
            }
            g.Save();
        }

        /// <summary>
        /// 檢測成功,划對號
        /// </summary>
        public void DrawSuccessMark()
        {
            int brushWidth = 14;
            Point startPoint = new Point(50, 50);
            Pen pen = new Pen(Brushes.Green, brushWidth);
            int x = startPoint.X;
            int y = startPoint.Y;
            using (Graphics g1 = this.pictureBox1.CreateGraphics())
            {
                g1.Clear(this.BackColor);
                DrawSuccessMarkLines(false, g1, pen, x, y, startPoint, brushWidth);
                g1.Save();
            }
            //以上動態畫出的線在窗口縮小或隱藏后重新顯示時會因為form的重繪而消失,所以需要用Bitmap再畫一遍
            this.pictureBox1.Refresh();
            Bitmap bmp = new Bitmap(this.pictureBox1.Width, this.pictureBox1.Height);
            Graphics g2 = Graphics.FromImage(bmp);
            g2.Clear(this.BackColor);
            DrawSuccessMarkLines(true, g2, pen, x, y, startPoint, brushWidth);
            g2.Dispose();
            this.pictureBox1.Image = bmp;
        }

        private void DrawSuccessMarkLines(bool isRedraw, Graphics g, Pen pen, int x, int y, Point startPoint, int brushWidth)
        {
            for (int i = 0; i < 7; i++)
            {
                g.DrawLine(pen, x, y, x + 10, y + 10);
                x += 10;
                y += 10;
                i++;
                if (!isRedraw)
                {
                    Thread.Sleep(50);//通過線程sleep實現動態繪圖
                }

            }
            x -= brushWidth / 2;
            for (int i = 0; i < 15; i++)
            {
                g.DrawLine(pen, x, y, x + 10, y - 10);
                x += 10;
                y -= 10;
                i++;
                if (!isRedraw)
                {
                    Thread.Sleep(50);//通過線程sleep實現動態繪圖
                }
            }
        }
    }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM