在C#中绘制多个对象 [英] Drawing multiple objects in C#

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

问题描述

专家们好!
我正在用C#进行绘图.我这个程序有问题;当我在鼠标事件和绘画事件上绘制新对象时,先前绘制的对象消失了.我的代码如下.

Hi Experts!
I am working on drawing in C#. I have a problem in this program; when I draw a new object on mouse event and paint event the previous drawn object disappears. My code is as follows.

private void picMain_MouseDown(object sender, MouseEventArgs e)
        {
            MouseDownPoint = e.Location;
            mouseMovePoint = e.Location;
        }

private void picMain_MouseMove(object sender, MouseEventArgs e)
        {
            mouseMovePoint = e.Location;
        }
private void picMain_Paint(object sender, PaintEventArgs e)
        {
            G.DrawLine(new Pen(Color.Green, 10),MouseDownPoint, mouseMovePoint);
        }


请帮助我绘制无限数量的对象而不删除前一个对象.


Please help me to draw an unlimited number of objects without erasing the previous one.

推荐答案

kachura写道:
kachura wrote:

当我在鼠标事件上绘制新对象并绘制事件时,先前绘制的对象消失了.

when I draw a new object on mouse event and paint event the previous drawn object disappears.



这就是Windows的工作方式.每次发生绘画事件时,您都必须从头开始重新绘画.最好的选择是将点存储在容器中(附加鼠标事件),然后在绘制处理程序中绘制所有点.
:)



This is how Windows works. Each time the paint event occurs you have to re-paint from scratch. Your best bet would be storing the points in a container (appending on mouse events) and then drawing all of them in the paint handler.
:)


这是因为您已经钩住了Paint事件.它只会在您的绘画事件上画一条线.做到这一点的困难方法是创建一个数组来存储所有行,然后在Paint事件上遍历该数组.

更简单的方法是钩住MouseUp并在MouseUp上画一条新线.这样,您也无需钩住MouseMove,因为您已经拥有终点位置.
It''s because you''ve hooked the Paint event. It will only draw the one line on your paint event. The hard way to do this would be to create an array to store all of the lines and then iterate through the array on the Paint event.

The simpler way is to hook the MouseUp instead and just draw the new line on the MouseUp. Doing that, you also don''t need to hook the MouseMove as you''ll have the end position already.




这是一个简单的应用程序示例,它大致上可以完成您想要的操作(请注意,这只是入门,有更好的C#绘制方法);

Hi,

This is a simple example of an application doing roughly what you want (note that this is just to get you started, there are better ways of drawing in C#);

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;

namespace WindowsFormsApplication2
{
    public partial class Form1 : Form
    {
        IList<Point> points = new List<Point>();

        public Form1()
        {
            this.MouseClick += new MouseEventHandler(Form1_MouseClick);
        }

        void Form1_MouseClick(object sender, MouseEventArgs e)
        {
            points.Add(e.Location);
            this.Invalidate(); // This causes the form to repaint
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            Point previousPoint = Point.Empty;
            foreach (Point point in points)
            {
                if (previousPoint != Point.Empty)
                {
                    e.Graphics.DrawLine(Pens.Black, previousPoint, point);
                }
                previousPoint = point;
            }
        }

        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }
    }
}


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

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