| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1396 人关注过本帖
标题:如何实现在窗体内自由拖动图标
只看楼主 加入收藏
lovesky126
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2021-3-30
结帖率:0
收藏
已结贴  问题点数:20 回复次数:2 
如何实现在窗体内自由拖动图标
小白一枚,请教如何实现在窗体内自由拖动图标,思路是什么呢
搜索更多相关主题的帖子: 思路 图标 拖动 窗体 
2021-03-30 19:10
apull
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
来 自:三体星系
等 级:版主
威 望:216
帖 子:1509
专家分:9241
注 册:2010-3-16
收藏
得分:20 
程序代码:
        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;
        }
2021-03-30 21:21
qq2889577966
Rank: 4
等 级:业余侠客
威 望:5
帖 子:68
专家分:287
注 册:2021-4-14
收藏
得分:0 
正确方法
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();
            }
        }
    }
}
2021-04-15 11:17
快速回复:如何实现在窗体内自由拖动图标
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.030065 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved