1

内の線の回転アニメーションを描きたいPictureBox。私pictureBox1.CreateGraphics()は線を引くのに使っていますが、この方法は には向いていないと聞きましたPictureBoxPictureBoxまた、窓のちらつきがひどいのですが、何か提案はありますか? コード セグメントは次のとおりです。

    private void OnTimedEvent(object source, PaintEventArgs e)
    {
        try
        {
            if (comport.BytesToRead > 0)
            {
                X = comport.ReadByte();
                Y = comport.ReadByte();
                Z = comport.ReadByte();
            }

            Graphics g = pictureBox1.CreateGraphics();
            Pen red = new Pen(Color.Red, 2);
            g.TranslateTransform(100, 80 - X);
            g.RotateTransform(120);
            g.DrawLine(red, new Point(100, 0 + X), new Point(100, 80 - X));
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }

    private void timer1_Tick(object sender, EventArgs e)
    {
        try
        {
            timer1.Interval = 1;
            pictureBox1.Invalidate();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }
4

1 に答える 1

1

Try to move your drawing code in the pictureBox1.Paint event handler and just call pictureBox1.Invalidate whenewer you need to redraw your pictureBox1. This is the real way how to draw. At the moment you are having flickering because you redraw your pictureBox1 on every second but you don't have primitives to draw at that moment.

        byte X;
        byte Y;
        byte Z;
        private void OnTimedEvent(object source, PaintEventArgs e)
        {
            try
            {
                if (comport.BytesToRead > 0)
                {
                    X = comport.ReadByte();
                    Y = comport.ReadByte();
                    Z = comport.ReadByte();
                }
                pictureBox1.Invalidate();

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            try
            {
                timer1.Interval = 1;
                pictureBox1.Invalidate();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void pictureBox1_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            Pen red = new Pen(Color.Red, 2);
            g.TranslateTransform(100, 80 - X);
            g.RotateTransform(120);
            g.DrawLine(red, new Point(100, 0 + X), new Point(100, 80 - X));
        }
于 2012-10-21T11:46:52.247 に答える