pictiuebox效率低,这里使用高效重绘方法,vs2017,代码很短,鼠标判断等就200行,只使用了panel 实现。可添加多个图像元素,移动元素并调整元素图层。 using System;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 abstract class DraggableObject { public abstract string Id { get; set; } public abstract int prim_X { get; set; } public abstract int prim_Y { get; set; } public abstract Rectangle Region { get; set; } public abstract Bitmap Setimage { get; set; } public abstract void OnPaint(PaintEventArgs e); } public class Draggable : DraggableObject { private string m_Id; private int m_primX; private int m_primY; private Rectangle m_Region; private Bitmap m_SetImage; public Draggable(string id, Rectangle regin, Bitmap image) { m_Id = id; m_Region = regin; m_SetImage = image; } public override string Id { get => m_Id; set => m_Id = value; } public override int prim_X { get => m_primX; set => m_primX = value; } public override int prim_Y { get => m_primY; set => m_primY = value; } public override Rectangle Region { get => m_Region; set => m_Region = value; } public override Bitmap Setimage { get => m_SetImage; set => m_SetImage = value; } public override void OnPaint(PaintEventArgs e) { e.Graphics.DrawImage(m_SetImage, m_Region); } } public List<DraggableObject> DesignObjects = new List<DraggableObject>(); public PanelEx panelContent; public Label info; private bool Is_Dragging = false; private int index = -1,record = -1; public Form1() { InitializeComponent(); panelContent = new PanelEx() { Dock = DockStyle.Fill, BackgroundImageLayout = ImageLayout.Stretch, BackgroundImage = Image.FromFile("back.jpg") }; panelContent.Paint = PanelContent_Paint; panelContent.MouseMove = PanelContent_MouseMove; panelContent.MouseDown = PanelContent_MouseDown; panelContent.MouseUp = PanelContent_MouseUp; this.Controls.Add(panelContent); PanelEx panelTop = new PanelEx() { Height = 30, Dock = DockStyle.Top }; this.Controls.Add(panelTop); Button[] buttons = new Button[4]; string[] btnText = new string[] { "添加元素","删除元素","元素前移","元素后移"}; for (int i=0;i<4;i ) { buttons[i] = new Button() { Size = new Size(70, 25), Location = new Point(10 i*80,5), Font = new Font("微软雅黑",9,FontStyle.Regular), Text = btnText[i], Tag = i }; buttons[i].Click =Button_Click; panelTop.Controls.Add(buttons[i]); } info = new Label() { AutoSize = true, Location = new Point(350,5), Font = new Font("微软雅黑", 9, FontStyle.Regular), }; panelTop.Controls.Add(info); } private void PanelContent_Paint(object sender, PaintEventArgs e) { foreach (DraggableObject item in DesignObjects) { item.OnPaint(e); } } private void Button_Click(object sender, EventArgs e) { Control pL = (Control)sender; if ((int)pL.Tag == 0) { OpenFileDialog openImgDialog = new OpenFileDialog(); openImgDialog.Filter = "图像文件(*.png;*.jpg;*.gif;*.bmp)|*.png;*.jpg;*.gif;*.bmp"; openImgDialog.RestoreDirectory = true; openImgDialog.FilterIndex = 1; if (openImgDialog.ShowDialog() == DialogResult.OK) { Bitmap bmp = Image.FromFile(openImgDialog.FileName) as Bitmap; Draggable draggableBlock = new Draggable(DateTime.Now.ToString(), new Rectangle(50, 50, bmp.Width, bmp.Height), bmp); DesignObjects.Add(draggableBlock); record = DesignObjects.Count - 1; info.Text = "当前元素:" (record 1) " ID: " DesignObjects[record].Id; } } else if ((int)pL.Tag == 1) { if (record != -1) { DesignObjects.RemoveAt(record); record = DesignObjects.Count == 0 ? -1 : DesignObjects.Count - 1; } } else if ((int)pL.Tag == 2) { if (record != -1) { var swap = DesignObjects[record]; DesignObjects.RemoveAt(record); DesignObjects.Insert(0, swap); record = 0; } } if ((int)pL.Tag == 3) { if (record != -1) { var swap = DesignObjects[record]; DesignObjects.RemoveAt(record); DesignObjects.Add(swap); record = DesignObjects.Count - 1; } } if (record != -1) info.Text = "当前元素:" (record 1) " ID: " DesignObjects[record].Id; panelContent.Invalidate(); } private void PanelContent_MouseUp(object sender, MouseEventArgs e) { Is_Dragging = false; } private void PanelContent_MouseDown(object sender, MouseEventArgs e) { index = DesignObjects.FindIndex(d => d.Region.Contains(e.Location)); if (index != -1) { record = index; Is_Dragging = true; DesignObjects[index].prim_X = e.Location.X - DesignObjects[index].Region.Left; DesignObjects[index].prim_Y = e.Location.Y - DesignObjects[index].Region.Top; info.Text = "当前元素:" (record 1) " ID: " DesignObjects[record].Id; } else { record = -1; Is_Dragging = false; } } private void PanelContent_MouseMove(object sender, MouseEventArgs e) { Control pL = (Control)sender; if (Is_Dragging && index != -1) { int set_x = e.Location.X - DesignObjects[index].prim_X; int set_y = e.Location.Y - DesignObjects[index].prim_Y; set_x = set_x < 0 ? 0 : set_x = set_x > panelContent.Width - DesignObjects[index].Region.Width ? panelContent.Width - DesignObjects[index].Region.Width : set_x; set_y = set_y < 0 ? 0 : set_y = set_y > panelContent.Height - DesignObjects[index].Region.Height ? panelContent.Height - DesignObjects[index].Region.Height : set_y; DesignObjects[index].Region = new Rectangle(set_x, set_y, DesignObjects[index].Region.Width, DesignObjects[index].Region.Height); panelContent.Invalidate(); } else { index = DesignObjects.FindIndex(d => d.Region.Contains(e.Location)); if (index != -1) pL.Cursor = Cursors.SizeAll; else pL.Cursor = Cursors.Default; } } }}
下载多图像平滑无闪烁移动基础代码用户还喜欢
- 18480 文章数
- 500万+ 热度
作者专栏
编辑推荐
- 淡抹u2引擎,修复内容较多,物有所值
- 界域传说·经典巨作=传世单机(一键安装)
- 丸子版本(175个传世版本大集合)
- GS版本:神话公益服务端+客户端
- 图片放大工具(放大图片不模糊)
- 剪映无限制VIP版
- 传奇世界客户端下载器,史上最全传世客户端
- 传世GS20220920商业引擎注册+登录配置器 解压密码是1
- U2官方排行榜游戏网关 支持元神,支持传家宝
- GS开战传世客户端+服务端
- (淡漠夕阳)u2引擎合区工具
- 传世GS引擎消除“你的游戏客户端版本号过旧,请及时更新”提示
- 传世一机多区双线路配置器--免密码版本
- 传世凤凰登陆器劫持修复软件
- SQLite3 for Navicat
- 传奇世界npc对话框编辑工具
- 传世GS落霞铭文服务器端
- gs_20210409引擎包+注册机(无限制)
- 传奇世界NPC对话封包查看器[支持时长版和极速版]
- 彩虹引擎传世脚本编辑工具1.7版来了,支持函数脚本翻译
评论