【问题标题】:C# - WInForms - Dispose of old graphics at redrawn eventC# - WInForms - 在重绘事件中处理旧图形
【发布时间】:2016-05-27 14:33:54
【问题描述】:

我正在学习使用 Windows 窗体处理绘画事件的基础知识。

到目前为止,该程序类型的工作,但图形的任何更新都不会删除以前绘制的线(图形没有被处置)。

最初的教程使用了Refresh,但似乎不起作用,我将其替换为Invalidate+Update

另外,将图形控件设置为 this.CreateGraphics() 不起作用,我将其切换为 panel2.CreateGraphics()(我也尝试了 e.Graphics 没有结果)。

namespace GraphicsTutorialV1
{
    public partial class Form1 : Form
    {
        Pen myPen = new Pen(Color.Black);
        Graphics g = null;

        static int start_x, start_y;
        static int end_x, end_y;

        static int my_angle = 0;
        static int my_length = 0;
        static int my_increment = 0;
        static int num_lines = 0;

        public Form1()
        {
            InitializeComponent();

            Int32.TryParse(textBox1.Text, out num_lines);
            Int32.TryParse(textBox2.Text, out my_angle);
            Int32.TryParse(textBox3.Text, out my_length);
            Int32.TryParse(textBox4.Text, out my_increment);

            start_x = (panel2.Width / 2);
            start_y = (panel2.Height / 2);
        }

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            myPen.Width = 1;
            g = panel2.CreateGraphics();
            //g = e.Graphics;

            for(int i = 0; i < num_lines; i++)
            {
                drawLine();
            }
        }

        private void drawLine()
        {
            int temp;
            Int32.TryParse(textBox2.Text, out temp);
            my_angle = my_angle + temp;
            Int32.TryParse(textBox4.Text, out temp);
            my_length = my_length + temp;

            end_x = (int)(start_x + Math.Cos(my_angle * Math.PI / 180) * my_length);
            end_y = (int)(start_y + Math.Sin(my_angle * Math.PI / 180) * my_length);

            Point[] points =
            {
                new Point(start_x, start_y),
                new Point(end_x, end_y)
            };

            start_x = end_x;
            start_y = end_y;

            g.DrawLines(myPen, points);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Int32.TryParse(textBox1.Text, out num_lines);
            Int32.TryParse(textBox2.Text, out my_angle);
            Int32.TryParse(textBox3.Text, out my_length);
            Int32.TryParse(textBox4.Text, out my_increment);

            this.Invalidate();
            this.Update();
        }
    }
}

【问题讨论】:

  • 不要存储 Graphic 对象。只需使用绘制事件中提供的e.Graphics。如果要让面板绘制,则必须使用面板的绘制事件。
  • 使用 CreateGraphics() 绘制在 99.9% 的情况下都是错误的。如果该教程建议您立即停止使用它。请改用面板的 Paint 事件。以及它的 Invalidate() 方法来触发重绘。您现在陷入了成功的陷阱,面板的 BackColor 属性可以完成工作。
  • 有一个来自 Micrsoft 的教程,它开始使用 FillEllipse 作为画笔..Arrgh.
  • 我已经发布了太多 CreateGraphics 答案来支持另一个答案。只需自己发布并将其标记为答案。
  • @HansPassant 是什么?自我评价?将他指向您的数千个答案中的一个不是更好吗?☺

标签: c# winforms graphics gdi+


【解决方案1】:

我的代码的问题是绘图指令包含在表单的绘制事件中。通过在面板的绘制事件中设置绘图,然后将图形设置为标准绘制事件,一切都解决了。此外,刷新开始工作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-23
    • 1970-01-01
    相关资源
    最近更新 更多