我使用Graphics 在一张2000*2000的图片上面画了10个50*50的小图,我想做的是
1)如何给这10个小图增加鼠标事件,比如鼠标的移动,单击等事件。
2)我更改这10个小图的内容,如何将它们先擦除在重绘。开发环境:Visual Studio 2008、C#

解决方案 »

  1.   

    将小图做成控件,然后重写OnPaint。这样小图都有单独的事件。
      

  2.   

    那你重绘时要注意使用ClipRectangle
      

  3.   

    把小图装pictureBox里,用pictureBox的事件,可动态添加(+=)
      

  4.   

    Did you tried the DoubleBuffered property?
      

  5.   

    给个思路:首先对于每个小的image,我们定义一个类SmallImageItem,它拥有如下属性:Region,Image,IsDragging(表示是否处于被拖动中),DraggingPoint(鼠标落下点,用于计算位移),并拥有一个方法Draw(用于自绘),在Form中我们可以创造一个该类的List,以替代控件的功能示例代码如下(有很多方面没有考虑,仅做参考,已实现了small image的拖动)using System;
    using System.Windows.Forms;
    using System.Drawing;
    using System.Collections.Generic;
    namespace WindowsFormsApplication1
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
                // 背景图是Windows自带的,在我的图片文件夹中
                m_BackgroundImage = Image.FromFile("Blue hills.jpg");
                this.DoubleBuffered = true;            m_ImageItems = new List<SmallImageItem>();
                SmallImageItem item0 = new SmallImageItem();
                item0.Region = new Rectangle(10, 10, 20, 20);
                item0.Image = Image.FromFile("1.jpg");            SmallImageItem item1 = new SmallImageItem();
                item1.Region = new Rectangle(30, 10, 20, 20);
                item1.Image = Image.FromFile("2.jpg");            SmallImageItem item2 = new SmallImageItem();
                item2.Region = new Rectangle(10, 40, 20, 20);
                item2.Image = Image.FromFile("3.jpg");            m_ImageItems.Add(item0);
                m_ImageItems.Add(item1);
                m_ImageItems.Add(item2);
            }        protected override void OnPaint(PaintEventArgs e)
            {
                base.OnPaint(e);
                e.Graphics.DrawImage(m_BackgroundImage, this.ClientRectangle);            foreach (SmallImageItem item in m_ImageItems)
                {
                    item.Draw(e.Graphics);
                }
            }        protected override void OnMouseDown(MouseEventArgs e)
            {
                base.OnMouseDown(e);
                foreach (SmallImageItem item in m_ImageItems)
                {
                    // 确定拖动哪个image
                    if (item.Region.Contains(e.Location))
                    {
                        item.IsDragging = true;
                        item.DraggingPoint = e.Location;
                    }
                }
            }        protected override void OnMouseMove(MouseEventArgs e)
            {
                base.OnMouseMove(e);
                foreach (SmallImageItem item in m_ImageItems)
                {
                    // 计算被drag的图片的新位置,并重绘
                    if (item.IsDragging)
                    {
                        item.Region = new Rectangle(
                            item.Region.Left + e.X - item.DraggingPoint.X,
                            item.Region.Top + e.Y - item.DraggingPoint.Y,
                            item.Region.Width,
                            item.Region.Height);
                        item.DraggingPoint = e.Location;
                        Invalidate();
                    }
                }
                
            }        protected override void OnMouseUp(MouseEventArgs e)
            {
                base.OnMouseUp(e);            foreach (SmallImageItem item in m_ImageItems)
                {
                    if (item.IsDragging)
                    {
                        item.IsDragging = false;
                        item.DraggingPoint = Point.Empty;
                    }
                }
            }        private Image m_BackgroundImage;
            private List<SmallImageItem> m_ImageItems;
        }    public class SmallImageItem
        {
            public SmallImageItem()
            {
                m_IsDragging = false;
            }        public Rectangle Region
            {
                get { return m_Region; }
                set { m_Region = value; }
            }        public Image Image
            {
                get { return m_Image; }
                set { m_Image = value; }
            }        public virtual void Draw(Graphics g)
            {
                g.DrawImage(m_Image, m_Region);
            }        public bool IsDragging
            {
                get { return m_IsDragging; }
                set { m_IsDragging = value; }
            }        public Point DraggingPoint
            {
                get { return m_DraggingPoint; }
                set { m_DraggingPoint = value; }
            }        private Rectangle m_Region;
            private Image m_Image;
            private bool m_IsDragging;
            private Point m_DraggingPoint;
        }
    }