C# 创建一个Panel并可以通过鼠标拖动并可以拉动改变大小(可生成多个Panel并保存布局到数据库中)
C# 创建一个Panel并可以通过鼠标拖动并可以拉动改变大小(可生成多个Panel并保存布局到数据库中)
一个用于绘制视频布局的控件,可在背景Panel上生成新的Panel并可以拖动改变位置以及调整大小。
工程下载:https://download.****.net/download/qq_40034982/12694987
QQ:2382596602(如有疑问可咨询)
本控件是在https://download.****.net/download/a201220121991/9436984的基础上进行改进而来的,改进点如下:
1.添加功能:
2.限制绘画区域,只能在区域内拖动
3.添加鼠标框,拉动鼠标框选择PANEL
4.深蓝色为目标Panel,浅蓝色为待操作Panel
1.拖拽功能分析
按住鼠标左键拖拽Panel改变位置,将鼠标放至深蓝色布局块右下角按住拉动可改变大小。
2.拉动框选择
按住鼠标左键拉动可选择待执行的Panel
3.功能演示
这里只演示了垂直平分,和水平平分。基本都是以深蓝色为基准,浅蓝色做动作
垂直平分
效果图:
9宫格
分割
用户控件源码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using Newtonsoft.Json;
namespace PRCS_UserControl
{
public partial class ucLayoutDesign : UserControl
{
#region 定义变量
/// <summary>
/// 用于存放选中的panel
/// </summary>
List<PanelRectangle> m_SelectedPanleList = new List<PanelRectangle>();
private const uint WM_SYSCOMMAND = 0x0112;
private const uint SC_MOVE = 0xF010;
private const uint HTCAPTION = 0x0002;
[DllImport("user32.dll", EntryPoint = "SendMessageA")]
private static extern int SendMessage(IntPtr hwnd, uint wMsg, uint wParam, uint lParam);
[DllImport("user32.dll")]
private static extern int ReleaseCapture();
private Point m_currentPoint = new Point(0, 0);//定义一个点
private int m_panelCount = 0;
/// <summary>
/// 用于存放鼠标点击的PANEL的NAME
/// </summary>
private string m_currentPanelname;
/// <summary>
/// 用于存放PICBOX标志位
/// </summary>
private bool m_PicBoxMouseDownState = false;
//定义两个变量
bool MouseIsDown = false;
Rectangle MouseRect = Rectangle.Empty; //矩形(为鼠标画出矩形选区)
Rectangle MouseRect_new = Rectangle.Empty; //矩形(为鼠标画出矩形选区)
int m_width;
int m_heigh;
Point Point = new Point(0, 0);
#endregion
public ucLayoutDesign()
{
InitializeComponent();
}
public string LayoutPanelRectangleString
{
get
{
List<PanelRectangle> panleMessageList = new List<PanelRectangle>();
int containerWidth = backPanel.Width;
int containerHeight = backPanel.Height;
string panelLocationJson = "";
foreach (Control ctrl in backPanel.Controls)
{
if (ctrl is Panel)
{
PanelRectangle panleMessage = new PanelRectangle();
panleMessage.Name = ctrl.Name;
panleMessage.X = ctrl.Location.X * 100.0 / containerWidth;
panleMessage.Y = ctrl.Location.Y * 100.0 / containerHeight;
panleMessage.Width = ctrl.Size.Width * 100.0 / containerWidth;
panleMessage.Height = ctrl.Size.Height * 100.0 / containerHeight;
panleMessageList.Add(panleMessage);
}
}
panelLocationJson = JsonConvert.SerializeObject(panleMessageList);
return panelLocationJson;
}
set
{
List<PanelRectangle> list = new List<PanelRectangle>();
list = JsonConvert.DeserializeObject<List<PanelRectangle>>(value);
foreach (PanelRectangle message in list)
{
CreateControls.ReBuildCreatePanel(backPanel, message.Name, backPanel_MouseMove, backPanel_MouseDown, backPanel_Paint, message.X, message.Y, message.Width, message.Height);
}
}
}
#region 功能
/// <summary>
/// 功能:创建panel
/// 时间:2020/6/7
/// </summary>
public void CreateLayoutPanel()
{
CreateControls.CreatePanel(backPanel, Guid.NewGuid().ToString(), backPanel_MouseMove, backPanel_MouseDown, backPanel_Paint);
}
/// <summary>
/// 功能:删除选中的panel
/// 时间:2020/6/7
/// </summary>
public void RemoveLayoutPanel()
{
if (m_currentPanelname == "" && m_currentPanelname == null && m_SelectedPanleList == null)
{
throw new Exception("未选择Panel");
}
else
{
if (m_SelectedPanleList.Count > 0)
{
foreach (PanelRectangle panelRectangle in m_SelectedPanleList)
{
Panel panel = backPanel.Controls[panelRectangle.Name] as Panel;
backPanel.Controls.Remove(panel);
}
m_SelectedPanleList.Clear();
}
if (m_currentPanelname != "" && m_currentPanelname != null)
{
Panel panel = backPanel.Controls[m_currentPanelname] as Panel;
backPanel.Controls.Remove(panel);
m_currentPanelname = "";
}
}
}
/// <summary>
/// 功能:向左对齐
/// </summary>
/// <param name="panel 目标panel"></param>
/// <param name="rectangles 选中的panel列表"></param>
public void PanelAlignLeft()
{
if (m_currentPanelname != "")
{
double containerWidths = backPanel.Width;
double containerHeights = backPanel.Height;
Panel panels = backPanel.Controls[m_currentPanelname] as Panel;
foreach (PanelRectangle panelRectangle in m_SelectedPanleList)
{//ctrl.Location.X * 100.0 / containerWidth
panelRectangle.X = panels.Location.X * 100.0 / containerWidths;
}
foreach (PanelRectangle panelRectangle in m_SelectedPanleList)
{
foreach (Control ct in backPanel.Controls)
{
if (panelRectangle.Name == ct.Name)
{
//删除原来布局块
Panel panel = backPanel.Controls[ct.Name] as Panel;
backPanel.Controls.Remove(panel);
//重绘更新后的布局块.。
CreateControls.ReBuildCreatePanel(backPanel, panelRectangle.Name, backPanel_MouseMove, backPanel_MouseDown, backPanel_Paint, panelRectangle.X, panelRectangle.Y * 100.0 / containerHeights, panelRectangle.Width * 100.0 / containerWidths, panelRectangle.Height * 100.0 / containerHeights);
}
}
}
}
m_SelectedPanleList.Clear();
}
/// <summary>
/// 功能:向右对齐
/// </summary>
/// <param name="panel 目标panel"></param>
/// <param name="rectangles 选中的panel列表"></param>
public void PanelAlignRight()
{
if (m_currentPanelname != "")
{
double containerWidths = backPanel.Width;
double containerHeights = backPanel.Height;
Panel panels = backPanel.Controls[m_currentPanelname] as Panel;
foreach (PanelRectangle panelRectangle in m_SelectedPanleList)
{
panelRectangle.X = panels.Location.X + panels.Size.Width - panelRectangle.Width;
}
foreach (PanelRectangle panelRectangle in m_SelectedPanleList)
{
foreach (Control ct in backPanel.Controls)
{
if (panelRectangle.Name == ct.Name)
{
//删除原来布局块
Panel panel = backPanel.Controls[ct.Name] as Panel;
backPanel.Controls.Remove(panel);
//重绘更新后的布局块
CreateControls.ReBuildCreatePanel(backPanel, panelRectangle.Name, backPanel_MouseMove, backPanel_MouseDown, backPanel_Paint, panelRectangle.X * 100.0 / containerWidths, panelRectangle.Y * 100.0 / containerHeights, panelRectangle.Width * 100.0 / containerWidths, panelRectangle.Height * 100.0 / containerHeights);
}
}
}
}
m_SelectedPanleList.Clear();
}
/// <summary>
/// 功能:居中对齐
/// </summary>
/// <param name="panel 目标panel"></param>
/// <param name="rectangles 选中的panel列表"></param>
public void PanelAlignMid()
{
if (m_currentPanelname != "")
{
double containerWidths = backPanel.Width;
double containerHeights = backPanel.Height;
Panel panels = backPanel.Controls[m_currentPanelname] as Panel;
foreach (PanelRectangle panelRectangle in m_SelectedPanleList)
{
double w = panelRectangle.Width / 2;
panelRectangle.X = panels.Location.X + panels.Size.Width / 2 - w;
}
foreach (PanelRectangle panelRectangle in m_SelectedPanleList)
{
foreach (Control ct in backPanel.Controls)
{
if (panelRectangle.Name == ct.Name)
{
//删除原来布局块
Panel panel = backPanel.Controls[ct.Name] as Panel;
backPanel.Controls.Remove(panel);
//重绘更新后的布局块
CreateControls.ReBuildCreatePanel(backPanel, panelRectangle.Name, backPanel_MouseMove, backPanel_MouseDown, backPanel_Paint, panelRectangle.X * 100.0 / containerWidths, panelRectangle.Y * 100.0 / containerHeights, panelRectangle.Width * 100.0 / containerWidths, panelRectangle.Height * 100.0 / containerHeights);
}
}
}
}
m_SelectedPanleList.Clear();
}
/// <summary>
/// 功能:顶部对齐
/// </summary>
/// <param name="panel 目标panel"></param>
/// <param name="rectangles 选中的panel列表"></param>
public void PanelAlignTop()
{
if (m_currentPanelname != "")
{
double containerWidths = backPanel.Width;
double containerHeights = backPanel.Height;
Panel panels = backPanel.Controls[m_currentPanelname] as Panel;
foreach (PanelRectangle panelRectangle in m_SelectedPanleList)
{
panelRectangle.Y = panels.Location.Y;
}
foreach (PanelRectangle panelRectangle in m_SelectedPanleList)
{
foreach (Control ct in backPanel.Controls)
{
if (panelRectangle.Name == ct.Name)
{
//删除原来布局块
Panel panel = backPanel.Controls[ct.Name] as Panel;
backPanel.Controls.Remove(panel);
//重绘更新后的布局块
CreateControls.ReBuildCreatePanel(backPanel, panelRectangle.Name, backPanel_MouseMove, backPanel_MouseDown, backPanel_Paint, panelRectangle.X * 100.0 / containerWidths, panelRectangle.Y * 100.0 / containerHeights, panelRectangle.Width * 100.0 / containerWidths, panelRectangle.Height * 100.0 / containerHeights);
}
}
}
}
m_SelectedPanleList.Clear();
}
/// <summary>
/// 功能:底部对齐
/// </summary>
/// <param name="panel 目标panel"></param>
/// <param name="rectangles 选中的panel列表"></param>
public void PanelAlignBottom()
{
if (m_currentPanelname != "")
{
double containerWidths = backPanel.Width;
double containerHeights = backPanel.Height;
Panel panels = backPanel.Controls[m_currentPanelname] as Panel;
foreach (PanelRectangle panelRectangle in m_SelectedPanleList)
{
double h = panelRectangle.Height;
panelRectangle.Y = panels.Location.Y + panels.Height - h;
}
foreach (PanelRectangle panelRectangle in m_SelectedPanleList)
{
foreach (Control ct in backPanel.Controls)
{
if (panelRectangle.Name == ct.Name)
{
//删除原来布局块
Panel panel = backPanel.Controls[ct.Name] as Panel;
backPanel.Controls.Remove(panel);
//重绘更新后的布局块
CreateControls.ReBuildCreatePanel(backPanel, panelRectangle.Name, backPanel_MouseMove, backPanel_MouseDown, backPanel_Paint, panelRectangle.X * 100.0 / containerWidths, panelRectangle.Y * 100.0 / containerHeights, panelRectangle.Width * 100.0 / containerWidths, panelRectangle.Height * 100.0 / containerHeights);
}
}
}
}
m_SelectedPanleList.Clear();
}
/// <summary>
/// 功能:中间对齐
/// </summary>
/// <param name="panel 目标panel"></param>
/// <param name="rectangles 选中的panel列表"></param>
public void PanelAlignCenter()
{
if (m_currentPanelname != "")
{
double containerWidths = backPanel.Width;
double containerHeights = backPanel.Height;
Panel panels = backPanel.Controls[m_currentPanelname] as Panel;
foreach (PanelRectangle panelRectangle in m_SelectedPanleList)
{
double h = panelRectangle.Height;
panelRectangle.Y = panels.Location.Y + panels.Height / 2 - h / 2;
}
foreach (PanelRectangle panelRectangle in m_SelectedPanleList)
{
foreach (Control ct in backPanel.Controls)
{
if (panelRectangle.Name == ct.Name)
{
//删除原来布局块
Panel panel = backPanel.Controls[ct.Name] as Panel;
backPanel.Controls.Remove(panel);
//重绘更新后的布局块
CreateControls.ReBuildCreatePanel(backPanel, panelRectangle.Name, backPanel_MouseMove, backPanel_MouseDown, backPanel_Paint, panelRectangle.X * 100.0 / containerWidths, panelRectangle.Y * 100.0 / containerHeights, panelRectangle.Width * 100.0 / containerWidths, panelRectangle.Height * 100.0 / containerHeights);
}
}
}
}
m_SelectedPanleList.Clear();
}
/// <summary>
/// 功能:宽度一致
/// </summary>
/// <param name="panel 目标panel"></param>
/// <param name="rectangles 选中的panel列表"></param>
public void PanelSameWidth()
{
if (m_currentPanelname != "")
{
double containerWidths = backPanel.Width;
double containerHeights = backPanel.Height;
Panel panels = backPanel.Controls[m_currentPanelname] as Panel;
foreach (PanelRectangle panelRectangle in m_SelectedPanleList)
{
panelRectangle.Width = panels.Width;
}
foreach (PanelRectangle panelRectangle in m_SelectedPanleList)
{
foreach (Control ct in backPanel.Controls)
{
if (panelRectangle.Name == ct.Name)
{
//删除原来布局块
Panel panel = backPanel.Controls[ct.Name] as Panel;
backPanel.Controls.Remove(panel);
//重绘更新后的布局块
CreateControls.ReBuildCreatePanel(backPanel, panelRectangle.Name, backPanel_MouseMove, backPanel_MouseDown, backPanel_Paint, panelRectangle.X * 100.0 / containerWidths, panelRectangle.Y * 100.0 / containerHeights, panelRectangle.Width * 100.0 / containerWidths, panelRectangle.Height * 100.0 / containerHeights);
}
}
}
}
m_SelectedPanleList.Clear();
}
/// <summary>
/// 功能:大小一致
/// </summary>
/// <param name="panel 目标panel"></param>
/// <param name="rectangles 选中的panel列表"></param>
public void PanelSameSize()
{
if (m_currentPanelname != "")
{
double containerWidths = backPanel.Width;
double containerHeights = backPanel.Height;
Panel panels = backPanel.Controls[m_currentPanelname] as Panel;
foreach (PanelRectangle panelRectangle in m_SelectedPanleList)
{
panelRectangle.Width = panels.Width;
panelRectangle.Height = panels.Height;
}
foreach (PanelRectangle panelRectangle in m_SelectedPanleList)
{
foreach (Control ct in backPanel.Controls)
{
if (panelRectangle.Name == ct.Name)
{
//删除原来布局块
Panel panel = backPanel.Controls[ct.Name] as Panel;
backPanel.Controls.Remove(panel);
//重绘更新后的布局块
CreateControls.ReBuildCreatePanel(backPanel, panelRectangle.Name, backPanel_MouseMove, backPanel_MouseDown, backPanel_Paint, panelRectangle.X * 100.0 / containerWidths, panelRectangle.Y * 100.0 / containerHeights, panelRectangle.Width * 100.0 / containerWidths, panelRectangle.Height * 100.0 / containerHeights);
}
}
}
}
m_SelectedPanleList.Clear();
}
/// <summary>
/// 功能:高度一致
/// </summary>
/// <param name="panel 目标panel"></param>
/// <param name="rectangles 选中的panel列表"></param>
public void PanelSameHight()
{
if (m_currentPanelname != "")
{
double containerWidths = backPanel.Width;
double containerHeights = backPanel.Height;
Panel panels = backPanel.Controls[m_currentPanelname] as Panel;
foreach (PanelRectangle panelRectangle in m_SelectedPanleList)
{
panelRectangle.Height = panels.Height;
}
foreach (PanelRectangle panelRectangle in m_SelectedPanleList)
{
foreach (Control ct in backPanel.Controls)
{
if (panelRectangle.Name == ct.Name)
{
//删除原来布局块
Panel panel = backPanel.Controls[ct.Name] as Panel;
backPanel.Controls.Remove(panel);
//重绘更新后的布局块
CreateControls.ReBuildCreatePanel(backPanel, panelRectangle.Name, backPanel_MouseMove, backPanel_MouseDown, backPanel_Paint, panelRectangle.X * 100.0 / containerWidths, panelRectangle.Y * 100.0 / containerHeights, panelRectangle.Width * 100.0 / containerWidths, panelRectangle.Height * 100.0 / containerHeights);
}
}
}
}
m_SelectedPanleList.Clear();
}
/// <summary>
/// 功能:垂直平分
/// </summary>
public void LeftSplit()
{
if (m_SelectedPanleList.Count > 0)
{
m_SelectedPanleList = m_SelectedPanleList.OrderBy(j => j.Y).ToList();
Deduplication();
double containerWidths = backPanel.Width;
double containerHeights = backPanel.Height;
double Y = 634 / m_SelectedPanleList.Count;
double X = 1303 / m_SelectedPanleList.Count;
double locationY = 0;
foreach (PanelRectangle panelRectangle in m_SelectedPanleList)
{
panelRectangle.Height = Y;
panelRectangle.X = panelRectangle.X;
panelRectangle.Y = locationY;
locationY = locationY + Y;
}
foreach (PanelRectangle panelRectangle in m_SelectedPanleList)
{
foreach (Control ct in backPanel.Controls)
{
if (panelRectangle.Name == ct.Name)
{
//删除原来布局块
Panel panel = backPanel.Controls[ct.Name] as Panel;
backPanel.Controls.Remove(panel);
//重绘更新后的布局块
CreateControls.ReBuildCreatePanel(backPanel, panelRectangle.Name, backPanel_MouseMove, backPanel_MouseDown, backPanel_Paint, panelRectangle.X * 100.0 / containerWidths, panelRectangle.Y * 100.0 / containerHeights, panelRectangle.Width * 100.0 / containerWidths, panelRectangle.Height * 100.0 / containerHeights);
}
}
}
}
m_SelectedPanleList.Clear();
}
/// <summary>
/// 水平平分
/// </summary>
public void HorizontalSplit()
{
if (m_SelectedPanleList.Count > 0)
{
m_SelectedPanleList = m_SelectedPanleList.OrderBy(j => j.X).ToList();
Deduplication();
double containerWidths = backPanel.Width;
double containerHeights = backPanel.Height;
Deduplication();
// double Y = 634 / m_SelectedPanleList.Count;
double X = 1303 / m_SelectedPanleList.Count;
double locationY = 0;
double locationX = 0;
foreach (PanelRectangle panelRectangle in m_SelectedPanleList)
{
panelRectangle.Width = X;
panelRectangle.X = locationX;
panelRectangle.Y = panelRectangle.Y;
locationX = locationX + X;
}
foreach (PanelRectangle panelRectangle in m_SelectedPanleList)
{
foreach (Control ct in backPanel.Controls)
{
if (panelRectangle.Name == ct.Name)
{
//删除原来布局块
Panel panel = backPanel.Controls[ct.Name] as Panel;
backPanel.Controls.Remove(panel);
//重绘更新后的布局块
CreateControls.ReBuildCreatePanel(backPanel, panelRectangle.Name, backPanel_MouseMove, backPanel_MouseDown, backPanel_Paint, panelRectangle.X * 100.0 / containerWidths, panelRectangle.Y * 100.0 / containerHeights, panelRectangle.Width * 100.0 / containerWidths, panelRectangle.Height * 100.0 / containerHeights);
}
}
}
}
m_SelectedPanleList.Clear();
}
/// <summary>
/// 垂直居中
/// </summary>
public void VerticalMidSplit()
{
m_SelectedPanleList = m_SelectedPanleList.OrderBy(j => j.X).ToList();
if (m_currentPanelname != "")
{
Panel panels = backPanel.Controls[m_currentPanelname] as Panel;
PanelRectangle panel = new PanelRectangle();
panel.X = panels.Location.X;
panel.Y = panels.Location.Y;
panel.Name = panels.Name;
panel.Width = panels.Width;
panel.Height = panels.Height;
m_SelectedPanleList.Add(panel);
}
if (m_SelectedPanleList.Count > 0)
{
m_SelectedPanleList = m_SelectedPanleList.OrderBy(j => j.X).ToList();
Deduplication();
double containerWidths = backPanel.Width;
double containerHeights = backPanel.Height;
Deduplication();
double Y = 634 / m_SelectedPanleList.Count;
double X = 1303 / m_SelectedPanleList.Count;
double locationY = 0;
double locationX = 0;
foreach (PanelRectangle panelRectangle in m_SelectedPanleList)
{
panelRectangle.Y = 634 / 2 - panelRectangle.Height / 2;
}
foreach (PanelRectangle panelRectangle in m_SelectedPanleList)
{
foreach (Control ct in backPanel.Controls)
{
if (panelRectangle.Name == ct.Name)
{
//删除原来布局块
Panel panel = backPanel.Controls[ct.Name] as Panel;
backPanel.Controls.Remove(panel);
//重绘更新后的布局块
CreateControls.ReBuildCreatePanel(backPanel, panelRectangle.Name, backPanel_MouseMove, backPanel_MouseDown, backPanel_Paint, panelRectangle.X * 100.0 / containerWidths, panelRectangle.Y * 100.0 / containerHeights, panelRectangle.Width * 100.0 / containerWidths, panelRectangle.Height * 100.0 / containerHeights);
}
}
}
}
m_SelectedPanleList.Clear();
}
/// <summary>
/// 功能:水平居中
/// </summary>
public void HorizontalMidSplit()
{
m_SelectedPanleList = m_SelectedPanleList.OrderBy(j => j.Y).ToList();
if (m_currentPanelname != "")
{
Panel panels = backPanel.Controls[m_currentPanelname] as Panel;
PanelRectangle panel = new PanelRectangle();
panel.X = panels.Location.X;
panel.Y = panels.Location.Y;
panel.Name = panels.Name;
panel.Width = panels.Width;
panel.Height = panels.Height;
m_SelectedPanleList.Add(panel);
}
if (m_SelectedPanleList.Count > 0)
{
m_SelectedPanleList = m_SelectedPanleList.OrderBy(j => j.Y).ToList();
Deduplication();
double containerWidths = backPanel.Width;
double containerHeights = backPanel.Height;
Deduplication();
double Y = 634 / m_SelectedPanleList.Count;
double X = 1303 / m_SelectedPanleList.Count;
double locationY = 0;
foreach (PanelRectangle panelRectangle in m_SelectedPanleList)
{
panelRectangle.X = 651 - panelRectangle.Width / 2;
}
foreach (PanelRectangle panelRectangle in m_SelectedPanleList)
{
foreach (Control ct in backPanel.Controls)
{
if (panelRectangle.Name == ct.Name)
{
//删除原来布局块
Panel panel = backPanel.Controls[ct.Name] as Panel;
backPanel.Controls.Remove(panel);
//重绘更新后的布局块
CreateControls.ReBuildCreatePanel(backPanel, panelRectangle.Name, backPanel_MouseMove, backPanel_MouseDown, backPanel_Paint, panelRectangle.X * 100.0 / containerWidths, panelRectangle.Y * 100.0 / containerHeights, panelRectangle.Width * 100.0 / containerWidths, panelRectangle.Height * 100.0 / containerHeights);
}
}
}
}
m_SelectedPanleList.Clear();
}
#endregion
#region 鼠标事件
private void backPanel_Paint(object sender, PaintEventArgs e)
{
Panel panel = sender as Panel;
ControlPaint.DrawBorder(e.Graphics,
panel.ClientRectangle,
Color.Red,
1,
ButtonBorderStyle.Solid,
Color.Red,
1,
ButtonBorderStyle.Solid,
Color.Red,
1,
ButtonBorderStyle.Solid,
Color.Red,
1,
ButtonBorderStyle.Solid);
}
private void backPanel_MouseMove(object sender, MouseEventArgs e)
{//实现panel的缩放和拖动
Panel panel = sender as Panel;//取得当前panel
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{//如果点击的为鼠标左键
//if (ModifierKeys == Keys.Control)
if(m_PicBoxMouseDownState)
{//如果按住ctrl键,则为缩放panel
panel.Width +=e.X - m_currentPoint.X;//取出panel缩放后的实际宽度
panel.Height += e.Y -m_currentPoint.Y;//取出panel缩放后的实际高度
m_currentPoint =e.Location;//给点重新赋值
panel.Refresh();//刷新放置绘制出现延迟
if (panel.Width<100)
{
panel.Width = 100;
}
else if(panel.Height < 100)
{
panel.Height = 100;
}
else if(panel.Width>1303- panel.Location.X)
{
panel.Width = 1303 - panel.Location.X;
}
else if (panel.Height > 634 - panel.Location.Y)
{
panel.Height = 634 - panel.Location.Y;
}
foreach (Panel panels in backPanel.Controls)
{
panel.BackColor = backPanel.BackColor;
foreach (PictureBox pictureBoxs in panel.Controls)
{
panel.Controls.Remove(pictureBoxs);
}
}
panel.BackColor = Color.DarkBlue;
PictureBox pictureBox = new PictureBox();
pictureBox.Image = Image.FromFile("Images\\POINT.png");
pictureBox.Size = new Size(10, 10);
pictureBox.Location = new Point(panel.Width - 10, panel.Height - 10);
pictureBox.Cursor = Cursors.PanSE;
pictureBox.MouseDown += PictureBox_MouseDown;
pictureBox.MouseMove += PictureBox_MouseMove;
pictureBox.MouseUp += PictureBox_MouseUp;
panel.Controls.Add(pictureBox);
}
else
{//否则为拖动panel
panel.Location = new Point(panel.Left + e.X - m_currentPoint.X, panel.Top + e.Y - m_currentPoint.Y);//拖动panel后重新定位位置
}
}
}
private void backPanel_MouseDown(object sender, MouseEventArgs e)
{
Panel panels = sender as Panel;
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
foreach (PictureBox pictureBoxs in panels.Controls)
{
panels.Controls.Remove(pictureBoxs);
}
m_currentPanelname = panels.Name;
PictureBox pictureBox = new PictureBox();
pictureBox.Image = Image.FromFile("Images\\POINT.png");
pictureBox.Size = new Size(10, 10);
pictureBox.Location = new Point(panels.Width - 10, panels.Height - 10);
pictureBox.Cursor = Cursors.PanSE;
pictureBox.MouseDown += PictureBox_MouseDown;
pictureBox.MouseMove += PictureBox_MouseMove;
pictureBox.MouseUp += PictureBox_MouseUp;
panels.Controls.Add(pictureBox);
change_Color(panels);
}
if (e.Button == System.Windows.Forms.MouseButtons.Right)
{
PanelRectangle panleMsg = new PanelRectangle();
panleMsg.Name = panels.Name;
panleMsg.X = panels.Location.X;
panleMsg.Y = panels.Location.Y;
panleMsg.Width = panels.Size.Width;
panleMsg.Height = panels.Size.Height;
m_SelectedPanleList.Add(panleMsg);//将选中的panel对象保存
panels.BackColor = Color.RoyalBlue;
foreach (PictureBox picture in panels.Controls)
{
panels.Controls.Remove(picture);
}
}
if (m_PicBoxMouseDownState)
{//如果按住ctrl键,则更新p点
Panel panel = sender as Panel;
m_currentPoint = new Point(e.X,e.Y);
}
else
{//否则执行移动,使用api函数
ReleaseCapture();
SendMessage((sender as Control).Handle, WM_SYSCOMMAND, SC_MOVE + HTCAPTION, 0);
Panel panel = sender as Panel;
if (panel.Location.X >= 1303 - panel.Width)
{
panel.Location = new Point(1303 - panel.Width, panel.Location.Y);
}
if (panel.Location.Y >= 634 - panel.Height)
{
panel.Location = new Point(panel.Location.X, 634 - panel.Height);
}
if (panel.Location.X < 0)
{
panel.Location = new Point(0, panel.Location.Y);
}
if (panel.Location.Y < 0)
{
panel.Location = new Point(panel.Location.X, 0);
}
}
}
private void PictureBox_MouseMove(object sender, MouseEventArgs e)
{
if (m_PicBoxMouseDownState)
{
Point panLocation = (backPanel.Controls[m_currentPanelname] as Panel).PointToClient((sender as PictureBox).PointToScreen(e.Location));
backPanel_MouseMove(backPanel.Controls[m_currentPanelname] as Panel, new MouseEventArgs(e.Button, e.Clicks, panLocation.X, panLocation.Y, e.Delta));
}
}
private void PictureBox_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
Point panLocation = (backPanel.Controls[m_currentPanelname] as Panel).PointToClient((sender as PictureBox).PointToScreen(e.Location));
m_PicBoxMouseDownState = true;
backPanel_MouseDown(backPanel.Controls[m_currentPanelname] as Panel, new MouseEventArgs(e.Button, e.Clicks, panLocation.X, panLocation.Y, e.Delta));
}
}
private void PictureBox_MouseUp(object sender, MouseEventArgs e)
{
m_PicBoxMouseDownState = false;
}
#endregion
#region 功能方法(颜色清零,修改颜色,List去重)
/// <summary>
/// 功能:通过点击鼠标右键清除颜色
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void color_Clear(object sender, EventArgs e)
{
MouseEventArgs Mouse_e = (MouseEventArgs)e;//click的事件是继承了MOUSE事件,可以强制转换
if (Mouse_e.Button == MouseButtons.Right)
{
foreach (Panel panel in backPanel.Controls)
{
panel.BackColor = backPanel.BackColor;
foreach (PictureBox pictureBox in panel.Controls)
{
panel.Controls.Remove(pictureBox);
}
}
m_SelectedPanleList.Clear();
m_currentPanelname = "";
m_PicBoxMouseDownState = false;
}
}
/// <summary>
/// 功能:list去重
/// </summary>
public void Deduplication()
{
var list = m_SelectedPanleList.GroupBy(c => c.Name).Select(c => c.First()).ToList();
m_SelectedPanleList.Clear();
m_SelectedPanleList = list;
}
/// <summary>
/// 功能:选择根布局颜色
/// </summary>
/// <param name="panels"></param>
private void change_Color(Panel panels)
{
foreach (Panel panel in backPanel.Controls)
{
panel.BackColor = backPanel.BackColor;
foreach (PictureBox pictureBoxs in panel.Controls)
{
panel.Controls.Remove(pictureBoxs);
}
}
panels.BackColor = Color.DarkBlue;
PictureBox pictureBox = new PictureBox();
pictureBox.Image = Image.FromFile("Images\\POINT.png");
pictureBox.Size = new Size(10, 10);
pictureBox.Location = new Point(panels.Width - 10, panels.Height - 10);
pictureBox.Cursor = Cursors.PanSE;
pictureBox.MouseDown += PictureBox_MouseDown;
pictureBox.MouseMove += PictureBox_MouseMove;
pictureBox.MouseUp += PictureBox_MouseUp;
panels.Controls.Add(pictureBox);
}
#endregion
#region 画虚线框
/// <summary>
/// 功能:重画矩形长宽
/// </summary>
/// <param name="sender"></param>
/// <param name="p"></param>
private void ResizeToRectangle(object sender, Point p)
{
DrawRectangle(sender);
MouseRect.Width = p.X - MouseRect.Left;
m_width = p.X - MouseRect.Left;
MouseRect.Height = p.Y - MouseRect.Top;
m_heigh = p.Y - MouseRect.Top;
DrawRectangle(sender);
if (m_width < 0)
{
string sss = "";
}
}
private void DrawRectangle(object sender)
{
Rectangle rect = ((Panel)sender).RectangleToScreen(MouseRect);
ControlPaint.DrawReversibleFrame(rect, Color.White, FrameStyle.Dashed);
}
private void DrawStart(object sender, Point StartPoint)
{
((Panel)sender).Capture = true;
Cursor.Clip = ((Panel)sender).RectangleToScreen(((Panel)sender).Bounds);
MouseRect = new Rectangle(StartPoint.X, StartPoint.Y, 0, 0);
}
private void backPanel_Mouse_Down(object sender, MouseEventArgs e)
{
MouseIsDown = true;
DrawStart(sender, e.Location);
}
private void backPanel_Mouse_Up(object sender, MouseEventArgs e)
{
this.Capture = false;
Cursor.Clip = Rectangle.Empty;
MouseIsDown = false;
DrawRectangle(sender);
MouseRect = Rectangle.Empty;
//松开鼠标时在上面重绘一个透明方块用于选择
}
/// <summary>
/// 功能:将鼠标框选中的panel存到list中
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void backPanel_Mouse_Move(object sender, MouseEventArgs e)
{
if (MouseIsDown)
{
ResizeToRectangle(sender, e.Location);
foreach (Control control in ((Panel)sender).Controls)
{
if (m_width > 0 && m_heigh > 0)
{
if (MouseRect.IntersectsWith(control.Bounds)) //相交( MouseRect.Contains 完全包含)
{
PanelRectangle panleMsg = new PanelRectangle();
panleMsg.Name = control.Name;
panleMsg.X = control.Location.X;
panleMsg.Y = control.Location.Y;
panleMsg.Width = control.Size.Width;
panleMsg.Height = control.Size.Height;
m_SelectedPanleList.Add(panleMsg);//将选中的panel对象保存
control.BackColor = Color.RoyalBlue;
}
}
else if (m_width < 0 && m_heigh < 0)
{
Rectangle rectangle = new Rectangle(e.Location.X, e.Location.Y, Math.Abs(m_width), Math.Abs(m_heigh));
if (rectangle.IntersectsWith(control.Bounds)) //相交( MouseRect.Contains 完全包含)
{
PanelRectangle panleMsg = new PanelRectangle();
panleMsg.Name = control.Name;
panleMsg.X = control.Location.X;
panleMsg.Y = control.Location.Y;
panleMsg.Width = control.Size.Width;
panleMsg.Height = control.Size.Height;
m_SelectedPanleList.Add(panleMsg);//将选中的panel对象保存
control.BackColor = Color.RoyalBlue;
}
}
else if (m_width < 0 && m_heigh > 0)
{
Rectangle rectangle = new Rectangle(e.Location.X, e.Location.Y - Math.Abs(m_heigh), Math.Abs(m_width), Math.Abs(m_heigh));
if (rectangle.IntersectsWith(control.Bounds)) //相交( MouseRect.Contains 完全包含)
{
PanelRectangle panleMsg = new PanelRectangle();
panleMsg.Name = control.Name;
panleMsg.X = control.Location.X;
panleMsg.Y = control.Location.Y;
panleMsg.Width = control.Size.Width;
panleMsg.Height = control.Size.Height;
m_SelectedPanleList.Add(panleMsg);//将选中的panel对象保存
control.BackColor = Color.RoyalBlue;
}
}
else if (m_width > 0 && m_heigh < 0)
{
Rectangle rectangle = new Rectangle(e.Location.X - Math.Abs(m_width), e.Location.Y, Math.Abs(m_width), Math.Abs(m_heigh));
if (rectangle.IntersectsWith(control.Bounds)) //相交( MouseRect.Contains 完全包含)
{
PanelRectangle panleMsg = new PanelRectangle();
panleMsg.Name = control.Name;
panleMsg.X = control.Location.X;
panleMsg.Y = control.Location.Y;
panleMsg.Width = control.Size.Width;
panleMsg.Height = control.Size.Height;
m_SelectedPanleList.Add(panleMsg);//将选中的panel对象保存
control.BackColor = Color.RoyalBlue;
}
}
Deduplication();
}
}
}
#endregion
}
public class CreateControls
{
/// <summary>
/// 功能:重建panel
/// 日期:2020/6/17
/// </summary>
/// <param name="ctl"></param>
/// <param name="name"></param>
/// <param name="panel_MouseMove"></param>
/// <param name="panel_MouseDown"></param>
/// <param name="panel_Paint"></param>
public static void CreatePanel(Control ctl, string name, MouseEventHandler panel_MouseMove, MouseEventHandler panel_MouseDown, PaintEventHandler panel_Paint)
{
Panel panel = new Panel()
{
Name = name,
BackColor = Color.FromArgb(10 * 5, 255, 255, 255),
BorderStyle = BorderStyle.FixedSingle,
Size = new Size(100, 100)
};
panel.MouseMove += new MouseEventHandler(panel_MouseMove);
panel.MouseDown += new MouseEventHandler(panel_MouseDown);
panel.Paint += new PaintEventHandler(panel_Paint);
panel.GetType().GetProperty("DoubleBuffered", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic).SetValue(panel, true, null);//设定让panel不闪烁
ctl.Controls.Add(panel);
panel.BringToFront();
}
/// <summary>
/// 功能:重建panel
/// 日期:2020/6/17
/// </summary>
/// <param name="ctl"></param panel >
/// <param name="name"></param panel name>
/// <param name="panel_MouseMove"></param>
/// <param name="panel_MouseDown"></param>
/// <param name="panel_Paint"></param>
/// <param name="x"></param >
/// <param name="y"></param>
/// <param name="width"></param>
/// <param name="height"></param>
public static void ReBuildCreatePanel(Control ctl, string name, MouseEventHandler panel_MouseMove, MouseEventHandler panel_MouseDown, PaintEventHandler panel_Paint, double x, double y, double width, double height)
{
Panel panel = new Panel()
{
Name = name,
BackColor = Color.FromArgb(10 * 5, 255, 255, 255),
BorderStyle = BorderStyle.FixedSingle,
Size = new Size((int)(width * 0.01 * ctl.Width), (int)(height * 0.01 * ctl.Height)),
Location = new System.Drawing.Point((int)(x * 0.01 * ctl.Width), (int)(y * 0.01 * ctl.Height))
};
panel.MouseMove += new MouseEventHandler(panel_MouseMove);
panel.MouseDown += new MouseEventHandler(panel_MouseDown);
panel.Paint += new PaintEventHandler(panel_Paint);
panel.GetType().GetProperty("DoubleBuffered", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic).SetValue(panel, true, null);//设定让panel不闪烁
ctl.Controls.Add(panel);
panel.BringToFront();
}
}
/// <summary>
/// 功能:panel属性
/// </summary>
public class PanelRectangle
{
private string name;
private double x;
private double y;
private double width;
private double height;
public string Name { get => name; set => name = value; }
public double X { get => x; set => x = value; }
public double Y { get => y; set => y = value; }
public double Width { get => width; set => width = value; }
public double Height { get => height; set => height = value; }
}
}