需要帮助C#中的绘图程序 [英] Need help with paint program in C#

查看:75
本文介绍了需要帮助C#中的绘图程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用c #Windows Form开发一个简单的绘图程序,这是我的问题。



问题:

每次新的前一行被删除。我知道这是因为

I am developing a simple paint program in c# Windows Form and here is my problem.

Problem:
Each time a new line is drawn previous one gets removed. I know it is because of

this.Refresh();



但是我该怎么办呢?



我的代码:








But how can i fix it ?

My code:



namespace mero_paint
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            g = this.CreateGraphics();

        }

        // Variables
        bool mouse_down = false;
        bool drawn = false;

        Graphics g;
        string tool = "";


        int mX;
        int mY;
        int currentX;
        int currentY;
        private void Form1_MouseDown(object sender, MouseEventArgs e)
        {
            mouse_down = true;
            mX = e.X;
            mY = e.Y;
        }
        private void button1_Click(object sender, EventArgs e)
        {
            tool = "line";
        }

        private void button2_Click(object sender, EventArgs e)
        {
            tool = "rectangle";
        }

        private void Form1_MouseMove(object sender, MouseEventArgs e)
        {
            currentX = e.X;
            currentY = e.Y;

            Pen p = new Pen(Color.Black);

            if (mouse_down)
            {
                if (tool == "line")
                {
                    this.Refresh();
                    g.DrawLine(p, new Point(mX, mY), new Point(currentX, currentY));

                }
            }
        }

        private void Form1_MouseUp(object sender, MouseEventArgs e)
        {
            mouse_down = false;
            mX = 0;
            mY = 0;
        }
    }
}









任何形式的帮助将不胜感激

推荐答案

不,这不是因为刷新。你只是不知道基本的想法。现在,我会告诉你这个想法,你会很容易解决它。原则上, Graphics 实例上的绘图不存储在任何地方。您必须在请求时进行渲染,这在事件的处理程序中完成 System.Windows.Forms.Control.Paint 或重写虚拟方法 System.Windows.Forms.Control.OnPaint 。在这些处理程序中,使用通过 CreateGraphics 创建的 Graphics 的实例是没用的(即使你可以在某些中使用它)例如,在使用手写笔绘图时,但稍后您需要将绘图结果放入数据中,以便能够重新渲染它。相反,你应该使用在参数参数中传递给你的处理程序的那个。



请看我过去对这个主题的回答:

什么样的俏皮方法是Paint? (DataGridViewImageCell.Paint(...)) [ ^ ],

在面板上捕获绘图 [ ^ ],

mdi子表单之间的绘制线 [ ^ ]。



祝你好运,

-SA
No, this is not "because of Refresh". You just don't know the basic idea. Now, I'll tell you the idea and you will easily fix it. The drawing on a Graphics instance is not stored anywhere in principle. You have to do rendering whenever it is requested, which is done in a handler of the event System.Windows.Forms.Control.Paint or overridden virtual method System.Windows.Forms.Control.OnPaint. In these handler, it's useless to use the instance of Graphics created via CreateGraphics (even though you can use it in some cases, for example at the moment of drawing with the stylus, but later you need to put the result of drawing in data, to be able to re-render it). Instead, you should use the one passed to your handler in the event arguments parameter.

Please see my past answers on this topic:
What kind of playful method is Paint? (DataGridViewImageCell.Paint(...))[^],
capture the drawing on a panel[^],
Drawing Lines between mdi child forms[^].

Good luck,
—SA


这篇关于需要帮助C#中的绘图程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆