因为业务需要,百度了个可移动可改变大小的控件,然后自己修改了下,功能类似vs的设计面板中的功能差不多,可拖拽,改变大小

拖动的

 public class MoveControl
{
#region 自定义事件
/// <summary>
/// 控件移动时触发事件
/// </summary>
public event EventHandler ControlMoving;
/// <summary>
/// 控件移动完成触发事件
/// </summary>
public event EventHandler ControlMoved;
/// <summary>
/// 控件改变大小时触发事件
/// </summary>
public event EventHandler ControlResizing;
/// <summary>
/// 控件改变大小完成触发事件
/// </summary>
public event EventHandler ControlResized;
/// <summary>
/// 选中控件时发生
/// </summary>
public event EventHandler ControlSelected;
#endregion
/// <summary>
/// 是否可调整尺寸
/// </summary>
bool m_blnResize = true;
MoveControlType m_moveType;
#region Constructors
/// <summary>
/// 功能描述:构造函数
/// 作  者:beck.huang
/// 创建日期:2018-07-07 09:20:41
/// 任务编号:中餐
/// </summary>
/// <param name="ctrl">ctrl</param>
/// <param name="blnResize">是否可调整尺寸</param>
/// <param name="moveType">移动模式</param>
public MoveControl(Control ctrl, bool blnResize = true, MoveControlType moveType = MoveControlType.ANY)
{
m_blnResize = blnResize;
m_moveType = moveType;
currentControl = ctrl;
AddEvents();
}
#endregion #region Fields
private Control currentControl; //传入的控件
private Point pPoint; //上个鼠标坐标
private Point cPoint; //当前鼠标坐标
FrameControl fc;//边框控件
#endregion #region Properties #endregion #region Methods
/// <summary>
/// 挂载事件
/// </summary>
private void AddEvents()
{
currentControl.MouseClick -= new MouseEventHandler(MouseClick);
currentControl.MouseClick += new MouseEventHandler(MouseClick);
currentControl.MouseDown -= new MouseEventHandler(MouseDown);
currentControl.MouseDown += new MouseEventHandler(MouseDown);
currentControl.MouseMove -= new MouseEventHandler(MouseMove);
currentControl.MouseMove += new MouseEventHandler(MouseMove);
currentControl.MouseUp -= new MouseEventHandler(MouseUp);
currentControl.MouseUp += new MouseEventHandler(MouseUp);
} /// <summary>
/// 绘制拖拉时的黑色边框
/// </summary>
public static void DrawDragBound(Control ctrl)
{
ctrl.Refresh();
Graphics g = ctrl.CreateGraphics();
int width = ctrl.Width;
int height = ctrl.Height;
Point[] ps = new Point[]{new Point(,),new Point(width -,),
new Point(width -,height -),new Point(,height-),new Point(,)};
g.DrawLines(new Pen(Color.Black), ps);
} #endregion #region Events
/// <summary>
/// 鼠标单击事件:用来显示边框
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void MouseClick(object sender, MouseEventArgs e)
{
if (m_blnResize)
{
this.currentControl.Parent.Refresh();//刷新父容器,清除掉其他控件的边框
for (int i = currentControl.Parent.Controls.Count-; i >=; i--)
{
Control item = currentControl.Parent.Controls[i];
if (item is FrameControl)
{
currentControl.Parent.Controls.RemoveAt(i);
}
} this.currentControl.BringToFront();
fc = new FrameControl(this.currentControl);
fc.ControlResized += fc_ControlResized;
fc.ControlResizing += fc_ControlResizing;
this.currentControl.Parent.Controls.Add(fc);
fc.Visible = true;
fc.Draw();
}
if (ControlSelected != null)
{
ControlSelected(this.currentControl, e);
}
} /// <summary>
/// 功能描述:控件改变大小时事件
/// 作  者:beck.huang
/// 创建日期:2018-07-07 09:51:27
/// 任务编号:中餐
/// </summary>
/// <param name="sender">sender</param>
/// <param name="e">e</param>
void fc_ControlResizing(object sender, EventArgs e)
{
if (ControlResizing != null)
{
ControlResizing(sender, e);
}
} /// <summary>
/// 功能描述:控件改变大小完成事件
/// 作  者:beck.huang
/// 创建日期:2018-07-07 09:51:12
/// 任务编号:中餐
/// </summary>
/// <param name="sender">sender</param>
/// <param name="e">e</param>
void fc_ControlResized(object sender, EventArgs e)
{
if (ControlResized != null)
{
ControlResized(sender, e);
}
} /// <summary>
/// 鼠标按下事件:记录当前鼠标相对窗体的坐标
/// </summary>
void MouseDown(object sender, MouseEventArgs e)
{
pPoint = Cursor.Position;
} /// <summary>
/// 鼠标移动事件:让控件跟着鼠标移动
/// </summary>
void MouseMove(object sender, MouseEventArgs e)
{
if (m_moveType != MoveControlType.NONE)
{
if (m_moveType == MoveControlType.ANY)
{
Cursor.Current = Cursors.SizeAll;
}
else if (m_moveType == MoveControlType.VERTICAL)
{
Cursor.Current = Cursors.HSplit;
}
else
{
Cursor.Current = Cursors.VSplit;
}
// Cursor.Current = Cursors.SizeAll; //当鼠标处于控件内部时,显示光标样式为SizeAll
//当鼠标左键按下时才触发
if (e.Button == MouseButtons.Left)
{
MoveControl.DrawDragBound(this.currentControl);
if (fc != null) fc.Visible = false; //先隐藏
cPoint = Cursor.Position;//获得当前鼠标位置 int x = cPoint.X - pPoint.X;
int y = cPoint.Y - pPoint.Y;
if (m_moveType == MoveControlType.ANY)
{
currentControl.Location = new Point(currentControl.Location.X + x, currentControl.Location.Y + y);
}
else if (m_moveType == MoveControlType.VERTICAL)
{
currentControl.Location = new Point(currentControl.Location.X, currentControl.Location.Y + y);
}
else
{
currentControl.Location = new Point(currentControl.Location.X + x, currentControl.Location.Y);
}
pPoint = cPoint;
if (ControlMoving != null)
{
ControlMoving(currentControl, e);
}
}
}
} /// <summary>
/// 鼠标弹起事件:让自定义的边框出现
/// </summary>
void MouseUp(object sender, MouseEventArgs e)
{
this.currentControl.Refresh();
if (fc != null)
{
fc.Visible = true;
fc.Draw();
}
if (ControlMoved != null)
{
ControlMoved(currentControl, e);
}
}
#endregion
}
/// <summary>
/// 移动控件模式
/// </summary>
public enum MoveControlType
{
/// <summary>
/// 任意
/// </summary>
ANY = ,
/// <summary>
/// 垂直
/// </summary>
VERTICAL,
/// <summary>
/// 水平
/// </summary>
HORIZONTAL,
/// <summary>
/// 不可移动
/// </summary>
NONE
}

改变大小的类

public class FrameControl : UserControl
{
/// <summary>
/// 控件改变大小时触发事件
/// </summary>
public event EventHandler ControlResizing;
/// <summary>
/// 控件改变大小完成触发事件
/// </summary>
public event EventHandler ControlResized;
#region Constructors
/// <summary>
/// 构造函数
/// </summary>
public FrameControl(Control ctrl)
{
baseControl = ctrl;
AddEvents();
CreateBounds();
}
#endregion #region Fields
const int Band = ; //调整大小的响应边框
private int MinWidth = ; //最小宽度
private int MinHeight = ;//最小高度
Size square = new Size(Band, Band);//小矩形大小
Control baseControl; //基础控件,即被包围的控件
Rectangle[] smallRects = new Rectangle[];//边框中的八个小圆圈
Rectangle[] sideRects = new Rectangle[];//四条边框,用来做响应区域
Point[] linePoints = new Point[];//四条边,用于画虚线
Graphics g; //画图板
Rectangle ControlRect; //控件包含边框的区域
private Point pPoint; //上个鼠标坐标
private Point cPoint; //当前鼠标坐标
private MousePosOnCtrl mpoc; #endregion #region Properties
/// <summary>
/// 鼠标在控件中位置
/// </summary>
enum MousePosOnCtrl
{
NONE = ,
TOP = ,
RIGHT = ,
BOTTOM = ,
LEFT = ,
TOPLEFT = ,
TOPRIGHT = ,
BOTTOMLEFT = ,
BOTTOMRIGHT = ,
}
#endregion #region Methods
/// <summary>
/// 加载事件
/// </summary>
private void AddEvents()
{
this.Name = "FrameControl" + baseControl.Name;
this.MouseDown += new MouseEventHandler(FrameControl_MouseDown);
this.MouseMove += new MouseEventHandler(FrameControl_MouseMove);
this.MouseUp += new MouseEventHandler(FrameControl_MouseUp);
} #region 创建边框
/// <summary>
/// 建立控件可视区域
/// </summary>
private void CreateBounds()
{
//创建边界
int X = baseControl.Bounds.X - square.Width - ;
int Y = baseControl.Bounds.Y - square.Height - ;
int Height = baseControl.Bounds.Height + (square.Height * ) + ;
int Width = baseControl.Bounds.Width + (square.Width * ) + ;
this.Bounds = new Rectangle(X, Y, Width, Height);
this.BringToFront();
SetRectangles();
//设置可视区域
this.Region = new Region(BuildFrame());
g = this.CreateGraphics();
} /// <summary>
/// 设置定义8个小矩形的范围
/// </summary>
void SetRectangles()
{
//左上
smallRects[] = new Rectangle(new Point(, ), square);
//右上
smallRects[] = new Rectangle(new Point(this.Width - square.Width - , ), square);
//左下
smallRects[] = new Rectangle(new Point(, this.Height - square.Height - ), square);
//右下
smallRects[] = new Rectangle(new Point(this.Width - square.Width - , this.Height - square.Height - ), square);
//上中
smallRects[] = new Rectangle(new Point(this.Width / - , ), square);
//下中
smallRects[] = new Rectangle(new Point(this.Width / - , this.Height - square.Height - ), square);
//左中
smallRects[] = new Rectangle(new Point(, this.Height / - ), square);
//右中
smallRects[] = new Rectangle(new Point(square.Width + baseControl.Width + , this.Height / - ), square); //四条边线
//左上
linePoints[] = new Point(square.Width / , square.Height / );
//右上
linePoints[] = new Point(this.Width - square.Width / - , square.Height / );
//右下
linePoints[] = new Point(this.Width - square.Width / - , this.Height - square.Height / );
//左下
linePoints[] = new Point(square.Width / , this.Height - square.Height / - );
//左上
linePoints[] = new Point(square.Width / , square.Height / ); //整个包括周围边框的范围
ControlRect = new Rectangle(new Point(, ), this.Bounds.Size);
} /// <summary>
/// 设置边框控件可视区域
/// </summary>
/// <returns></returns>
private GraphicsPath BuildFrame()
{
GraphicsPath path = new GraphicsPath();
//上边框
sideRects[] = new Rectangle(, , this.Width - square.Width - , square.Height + );
//左边框
sideRects[] = new Rectangle(, square.Height + , square.Width + , this.Height - square.Height - );
//下边框
sideRects[] = new Rectangle(square.Width + , this.Height - square.Height - , this.Width - square.Width - , square.Height + );
//右边框
sideRects[] = new Rectangle(this.Width - square.Width - , , square.Width + , this.Height - square.Height - ); path.AddRectangle(sideRects[]);
path.AddRectangle(sideRects[]);
path.AddRectangle(sideRects[]);
path.AddRectangle(sideRects[]);
return path;
}
#endregion /// <summary>
/// 绘图
/// </summary>
public void Draw()
{
this.BringToFront();
//g.FillRectangles(Brushes.LightGray, sideRects); //填充四条边框的内部
Pen pen = new Pen(Color.Black);
pen.DashStyle = DashStyle.Dot;//设置为虚线,用虚线画四边,模拟微软效果
g.DrawLines(pen, linePoints);//绘制四条边线
g.FillRectangles(Brushes.White, smallRects); //填充8个小矩形的内部
foreach (Rectangle smallRect in smallRects)
{
g.DrawEllipse(Pens.Black, smallRect); //绘制8个小椭圆
}
//g.DrawRectangles(Pens.Black, smallRects); //绘制8个小矩形的黑色边线
} /// <summary>
/// 设置光标状态
/// </summary>
public bool SetCursorShape(int x, int y)
{
Point point = new Point(x, y);
if (!ControlRect.Contains(point))
{
Cursor.Current = Cursors.Arrow;
return false;
}
else if (smallRects[].Contains(point))
{
Cursor.Current = Cursors.SizeNWSE;
mpoc = MousePosOnCtrl.TOPLEFT;
}
else if (smallRects[].Contains(point))
{
Cursor.Current = Cursors.SizeNESW;
mpoc = MousePosOnCtrl.TOPRIGHT;
}
else if (smallRects[].Contains(point))
{
Cursor.Current = Cursors.SizeNESW;
mpoc = MousePosOnCtrl.BOTTOMLEFT;
}
else if (smallRects[].Contains(point))
{
Cursor.Current = Cursors.SizeNWSE;
mpoc = MousePosOnCtrl.BOTTOMRIGHT;
}
else if (sideRects[].Contains(point))
{
Cursor.Current = Cursors.SizeNS;
mpoc = MousePosOnCtrl.TOP;
}
else if (sideRects[].Contains(point))
{
Cursor.Current = Cursors.SizeWE;
mpoc = MousePosOnCtrl.LEFT;
}
else if (sideRects[].Contains(point))
{
Cursor.Current = Cursors.SizeNS;
mpoc = MousePosOnCtrl.BOTTOM;
}
else if (sideRects[].Contains(point))
{
Cursor.Current = Cursors.SizeWE;
mpoc = MousePosOnCtrl.RIGHT;
}
else
{
Cursor.Current = Cursors.Arrow;
}
return true;
} /// <summary>
/// 控件移动
/// </summary>
private void ControlMove(MouseEventArgs e)
{
cPoint = Cursor.Position;
int x = cPoint.X - pPoint.X;
int y = cPoint.Y - pPoint.Y;
switch (this.mpoc)
{
case MousePosOnCtrl.TOP:
if (baseControl.Height - y > MinHeight)
{
baseControl.Top += y;
baseControl.Height -= y;
}
else
{
baseControl.Top -= MinHeight - baseControl.Height;
baseControl.Height = MinHeight;
}
break;
case MousePosOnCtrl.BOTTOM:
if (baseControl.Height + y > MinHeight)
{
baseControl.Height += y;
}
else
{
baseControl.Height = MinHeight;
}
break;
case MousePosOnCtrl.LEFT:
if (baseControl.Width - x > MinWidth)
{
baseControl.Left += x;
baseControl.Width -= x;
}
else
{
baseControl.Left -= MinWidth - baseControl.Width;
baseControl.Width = MinWidth;
} break;
case MousePosOnCtrl.RIGHT:
if (baseControl.Width + x > MinWidth)
{
baseControl.Width += x;
}
else
{
baseControl.Width = MinWidth;
}
break;
case MousePosOnCtrl.TOPLEFT:
if (baseControl.Height - y > MinHeight)
{
baseControl.Top += y;
baseControl.Height -= y;
}
else
{
baseControl.Top -= MinHeight - baseControl.Height;
baseControl.Height = MinHeight;
}
if (baseControl.Width - x > MinWidth)
{
baseControl.Left += x;
baseControl.Width -= x;
}
else
{
baseControl.Left -= MinWidth - baseControl.Width;
baseControl.Width = MinWidth;
}
break;
case MousePosOnCtrl.TOPRIGHT:
if (baseControl.Height - y > MinHeight)
{
baseControl.Top += y;
baseControl.Height -= y;
}
else
{
baseControl.Top -= MinHeight - baseControl.Height;
baseControl.Height = MinHeight;
}
if (baseControl.Width + x > MinWidth)
{
baseControl.Width += x;
}
else
{
baseControl.Width = MinWidth;
}
break;
case MousePosOnCtrl.BOTTOMLEFT:
if (baseControl.Height + y > MinHeight)
{
baseControl.Height += y;
}
else
{
baseControl.Height = MinHeight;
}
if (baseControl.Width - x > MinWidth)
{
baseControl.Left += x;
baseControl.Width -= x;
}
else
{
baseControl.Left -= MinWidth - baseControl.Width;
baseControl.Width = MinWidth;
}
break;
case MousePosOnCtrl.BOTTOMRIGHT:
if (baseControl.Height + y > MinHeight)
{
baseControl.Height += y;
}
else
{
baseControl.Height = MinHeight;
}
if (baseControl.Width + x > MinWidth)
{
baseControl.Width += x;
}
else
{
baseControl.Width = MinWidth;
}
break; }
pPoint = Cursor.Position;
if (ControlResizing != null)
{
ControlResizing(baseControl, e);
}
} #endregion #region Events
/// <summary>
/// 鼠标按下事件:记录当前鼠标相对窗体的坐标
/// </summary>
void FrameControl_MouseDown(object sender, MouseEventArgs e)
{
pPoint = Cursor.Position;
} /// <summary>
/// 鼠标移动事件:让控件跟着鼠标移动
/// </summary>
void FrameControl_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
this.Visible = false;
MoveControl.DrawDragBound(baseControl);
ControlMove(e);
}
else
{
this.Visible = true;
SetCursorShape(e.X, e.Y); //更新鼠标指针样式
}
} /// <summary>
/// 鼠标弹起事件:让自定义的边框出现
/// </summary>
void FrameControl_MouseUp(object sender, MouseEventArgs e)
{
this.baseControl.Refresh(); //刷掉黑色边框
this.Visible = true;
CreateBounds();
Draw();
if (ControlResized != null)
{
ControlResized(this.baseControl, e);
}
}
#endregion private void InitializeComponent()
{
this.SuspendLayout();
//
// FrameControl
//
this.BackColor = System.Drawing.Color.Transparent;
this.Name = "FrameControl";
this.ResumeLayout(false); }
}

使用方法

private void Form1_Load(object sender, EventArgs e)
{
//this.TransparencyKey = SystemColors.Control;
foreach (Control subCtrl in this.Controls)
{
MoveControl mc= new MoveControl(subCtrl,false, MoveControlType.HORIZONTAL);
mc.ControlMoving += mc_ControlMoving;
mc.ControlMoved += mc_ControlMoved;
mc.ControlResized += mc_ControlResized;
mc.ControlResizing += mc_ControlResizing;
}
} void mc_ControlResizing(object sender, EventArgs e)
{
Console.WriteLine("Resizing");
} void mc_ControlResized(object sender, EventArgs e)
{
Console.WriteLine("Resized");
} void mc_ControlMoved(object sender, EventArgs e)
{
Console.WriteLine("moved");
} void mc_ControlMoving(object sender, EventArgs e)
{
Console.WriteLine("moving");
}

效果如下

c# 可移动可改变大小的控件的更多相关文章

  1. 如何实现能像windows 窗体一样改变大小的控件 Silverlight

    众所周知,我们可以将鼠标放在windows窗体的边框上,按住鼠标左键改变窗体大小.那么,在silverlight上如何实现呢? 1. 需要将改控件放置在canvas上. 2. 判断鼠标位置,然后将Ar ...

  2. 前台改变asp button控件的值,后台取值没有改变的问题

    前台: <asp:Button ID="btnEdit" Style="margin-left: 600px;" runat="server&q ...

  3. 改变Web Browser控件IE版本

    默认的webbrowser控件使用的渲染模式版本似乎是IE7,想要更改更高版本,如下: 在注册表位置 HKEY_CURRENT_USER\Software\Microsoft\Internet Exp ...

  4. 【转载】在程序中动态改变static text控件的caption值

    方法1,给STATIC控件取个名字叫IDC_STATICTITLE 然后在ClassWizard中设定一个控件变量给它叫m_statictitle 然后用m_statictitle.SetWindow ...

  5. (转载)c# winform 用子窗体刷新父窗体,子窗体改变父窗体控件的值

    第一种方法: 用委托,Form2和Form3是同一组 Form2 C#代码 using System; using System.Collections.Generic; using System.C ...

  6. [转]- Winform 用子窗体刷新父窗体,子窗体改变父窗体控件的值

    转自:http://heisetoufa.iteye.com/blog/382684 第一种方法: 用委托,Form2和Form3是同一组 Form2  using System; using Sys ...

  7. 通过js子页面回写父页面,改变父页面控件的值

    [原]js中实现子页面向父页面中赋值 (方法一) 父页面:<input  id="input1" type="text"/><a href=& ...

  8. [UE4]动态改变UniFormGird子控件的row属性

  9. wpf 控件大小随窗体大小改变而改变

    WPF可以直接通过设置图形类控件的水平和垂直Alighment为Stretch实现用一个ViewBox装上所有的Window内容然后当window缩放时就可以一起放大缩小了ViewBox的显示机制是, ...

随机推荐

  1. Centos Apache和tomcat集成配置,同一时候支持PHP和JAVA执行

    近期因为项目的须要,须要再原来执行Tomcatserver上支持PHP执行.非常显然,PHP执行使用的是Apacheserver.尽管Tomcat也属于Apache,可是并没有现有的环境,须要我们自己 ...

  2. angularjs1.6 制作流程图,启动流程,流程设置

    话不多说,我们先来看一下效果图: 点击添加按扭要增加一个,可以叠加 代码如下: server.server().addprojectrooml({ type: , processName: $scop ...

  3. scala 通过apply创建类的对象

    package cn.scala_base.oop.scalaobject; class Boy(name: String) { private var age: Int = 0; println(n ...

  4. centos7 firewall-cmd查看端口是否开放及开放端口

    查询端口号80 是否开启:firewall-cmd /tcp 永久开放80端口号:firewall-cmd --permanent --zone=public /tcp 移除80端口号:/tcp -- ...

  5. java开发环境配置(windows下JDK7+tomcat7)

    參考原文:http://www.cnblogs.com/goto/archive/2012/11/16/2772683.html http://www.cnblogs.com/feilong35407 ...

  6. 简明Python3教程 10.模块

    简介 现在你已经知道通过定义函数可以在你的程序中复用代码.但当你想在你编写的其他程序中复用大量函数怎么办呢? 也许你可以猜到了,办法就是利用模块. 有各种编写模块的方式,但最简单的方式是创建一个以.p ...

  7. C#: Get current keyboard layout\input language

    原文 https://yal.cc/csharp-get-current-keyboard-layout/ On some occasions, you may want to get a " ...

  8. Qt Installer Framework 3.0.1 Released(功能比较强)

    We are happy to announce the release of Qt IFW 3.0.1. 3.0.1 is fully compatible with 2.0.5, which me ...

  9. WPF 图形绘制 及各种线帽、箭头的实现

    原文:WPF 图形绘制 及各种线帽.箭头的实现  /// <summary>     /// 矩形类     /// </summary>     public sealed ...

  10. SAAS是否能实现人在家工作的梦想?

    在过去的十年,在人们的工作环境的巨大变化已经发生,越来越多的人选择在家工作. 高租金的办公室,络,快速宽带的广泛应用.这些因素都使得远程办公成为了人们工作中密不可分的一种方式.使用普通手机和办公操作系 ...