注册 登录
编程论坛 C# 论坛

如何实现在窗体内自由拖动图标

lovesky126 发布于 2021-03-30 19:10, 1277 次点击
小白一枚,请教如何实现在窗体内自由拖动图标,思路是什么呢
2 回复
#2
apull2021-03-30 21:21
程序代码:

        Point mouse;
        bool isDown = false;
        private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
        {
            mouse.X = e.X;
            mouse.Y = e.Y;
            isDown = true;
        }

        private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
        {
            if(isDown)
            {
                pictureBox1.Left += (e.X-mouse.X);
                pictureBox1.Top += (e.Y- mouse.Y);
            }
        }

        private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
        {
            isDown = false;
        }
#3
qq28895779662021-04-15 11:17
正确方法
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
 
namespace Demo_1
{
    public partial class Form1 : Form
    {
        public class PanelEx : Panel
        {
            public PanelEx()
            {
                SetStyle(ControlStyles.AllPaintingInWmPaint |
                    ControlStyles.OptimizedDoubleBuffer |
                    ControlStyles.ResizeRedraw |
                    ControlStyles.SupportsTransparentBackColor |
                    ControlStyles.UserPaint, true
                    );
                UpdateStyles();
            }
        }
 
        public class DraggableObject
        {
            public int prim_X { get; set; }
            public int prim_Y { get; set; }
            public Rectangle Region { get; set; }
            public Bitmap SetImage { get; set; }
            public void OnPaint(PaintEventArgs e)
            {
                e.Graphics.DrawImage(SetImage, Region);
            }
        }
        public List<DraggableObject> DesignObjects = new List<DraggableObject>();
 
        private PanelEx panel;
        private bool Press = false;
        private int num = -1;
 
        public Form1()
        {
            InitializeComponent();
 
            this.DoubleBuffered = true;
            this.Size = new Size(800,600);
            panel = new PanelEx() { Size = new Size(800, 600), };
            this.Controls.Add(panel);
            panel.Paint += (s, e) => { foreach (DraggableObject item in DesignObjects) item.OnPaint(e); };
            panel.MouseDown += Panel_MouseDown;
            panel.MouseUp += (s, e) => { Press = false; };
            panel.MouseMove += Panel_MouseMove;
 
            int x = 0, y = 0;
            for (int i=0;i<40;i++) // 写入40个图像
            {
                if (i != 0 && i % 9 == 0)
                {
                    y += 1;
                    x = 0;
                }
                DraggableObject obj = new DraggableObject();
                obj.Region = new Rectangle(x*40,5+y*40,30,30);
                obj.SetImage = Draw_Rect(); // 这里改为写入图标图像Draw_Image
                DesignObjects.Add(obj);
                x += 1;
            }
            panel.Invalidate();
        }
 
        public Bitmap Draw_Rect()
        {
            Bitmap block = new Bitmap(30, 30);
            using (Graphics g = Graphics.FromImage(block))
            {
                Pen mypen = new Pen(Color.Red, 1);
                g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
                g.DrawRectangle(mypen, 0, 0, 29, 29);
            }
            return block;
        }
 
        private void Panel_MouseDown(object sender, MouseEventArgs e)
        {
            num = DesignObjects.FindIndex(d => d.Region.Contains(e.Location));
            if (num != -1)
            {
                Press = true;
                DesignObjects[num].prim_X = e.Location.X - DesignObjects[num].Region.Left;
                DesignObjects[num].prim_Y = e.Location.Y - DesignObjects[num].Region.Top;
            }
            else
            {
                Press = false;
            }
        }
 
        private void Panel_MouseMove(object sender, MouseEventArgs e)
        {
            if (Press)
            {
                int set_x = e.Location.X - DesignObjects[num].prim_X;
                int set_y = e.Location.Y - DesignObjects[num].prim_Y;
 
                DesignObjects[num].Region = new Rectangle(
                    set_x, set_y,
                    DesignObjects[num].Region.Width,
                    DesignObjects[num].Region.Height
                    );
                panel.Invalidate();
            }
        }
    }
}
1