实例介绍
【实例简介】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;
}
}
}
}
网友评论
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明


支持(0) 盖楼(回复)
支持(0) 盖楼(回复)
支持(0) 盖楼(回复)
支持(0) 盖楼(回复)