سؤال

Let's say for example my OnPaint draws a line which is as long as a variable called length. Also there is a bool called color. If the bool is true, the line will be red, if it is false, the line will be black. Would be possible to have two different colored and different long lines on screen (My question is: OnPaint redraws the complete surface, but is it possible to have some drawings stay) ?

I know the question might be a bit confusing.

هل كانت مفيدة؟

المحلول

As far as i know there is no such function.

But you can create your own OnPaint method (without calling base.OnPaint()) and integrate this logic (by drawing only element what you need - and something may stay) but this is probably bad approach and may lead you to lots of mistakes.

نصائح أخرى

This will draw two lines at different lengths and colors depending on if isValid == true or false. The lines will stay on the screen. For the other part of your question, Alex provided the correct method.

using System.Drawing;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            bool isTrue = false;

            if (isTrue == true)
            {
                g.DrawLine(Pens.Red, new Point(0, 50), new Point(150, 50));
            }
            else
            {
                g.DrawLine(Pens.Blue, new Point(0, 50), new Point(300, 50));
            }

            g.Dispose();
        }
    }
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top