C# 模拟VisualStudio窗体设计器拖放控件
C# 模拟VisualStudio窗体设计器拖放控件
从设计角度来看光画个控件是很简单的事,捕捉Mouse down/move/up 事件记录好坐标在Mouse Up事件触发时画出控件或者图形,其实最重要的时如何设计这个小框架。
我定义几个类仅供参考。
/// <summary>
/// 控件样式基类定义
/// </summary>
public class DrawStyle
{
protected bool _Drawing = false;
protected Control _Container = null;
protected Control _ActivedControl = null;
protected Point _S; //Start Point.
public virtual void BeginDraw(Control container, Point P) { }
public virtual void EndDraw(Control container, Point P) { }
public virtual void OnMove(Control container, Point P) { }
}
// 来源:www.CSFramework.com, C/S结构框架学习网
/// 控件样式基类定义
/// </summary>
public class DrawStyle
{
protected bool _Drawing = false;
protected Control _Container = null;
protected Control _ActivedControl = null;
protected Point _S; //Start Point.
public virtual void BeginDraw(Control container, Point P) { }
public virtual void EndDraw(Control container, Point P) { }
public virtual void OnMove(Control container, Point P) { }
}
// 来源:www.CSFramework.com, C/S结构框架学习网
/// <summary>
/// 拖放TextBox控件
/// </summary>
public class DrawTextBox : DrawStyle
{
private TextBox _CurrentText;
public DrawTextBox() { }
public override void BeginDraw(Control container, Point P)
{
_S = P;
_Drawing = true;
_Container = container;
_CurrentText = new TextBox();
_CurrentText.Multiline = true;
_ActivedControl = _CurrentText;
if (container.Controls.IndexOf(_ActivedControl) < 0)
container.Controls.Add(_ActivedControl);
_ActivedControl.Left = P.X;
_ActivedControl.Top = P.Y;
}
public override void EndDraw(Control container, Point P)
{
//向右下方拖動X,Y為正數,當為負數時為反向拖動,需要更改Top,Left屬性
_ActivedControl.Width = P.X - _S.X;
_ActivedControl.Height = P.Y - _S.Y;
_ActivedControl.Visible = true;
_Drawing = false;
}
public override void OnMove(Control container, Point P)
{
if (_Drawing == false) return;
if (_Container != container) return;
//向右下方拖動X,Y為正數,當為負數時為反向拖動,需要更改Top,Left屬性
_ActivedControl.Width = P.X - _S.X;
_ActivedControl.Height = P.Y - _S.Y;
_ActivedControl.Invalidate();
}
}
// 来源:www.CSFramework.com, C/S结构框架学习网
/// <summary>
/// 自定义IDE设计器,控件容器
/// </summary>
public class MyCanvasDesigner
{
private Control _Designer;
public MyCanvasDesigner(Control control)
{
_Designer = control;
}
public void Clear()
{
_Designer.Controls.Clear();
}
}
// 来源:www.CSFramework.com, C/S结构框架学习网
/// 自定义IDE设计器,控件容器
/// </summary>
public class MyCanvasDesigner
{
private Control _Designer;
public MyCanvasDesigner(Control control)
{
_Designer = control;
}
public void Clear()
{
_Designer.Controls.Clear();
}
}
// 来源:www.CSFramework.com, C/S结构框架学习网
/// <summary>
/// 应用场景/控制器
/// </summary>
public class DrawController
{
private static MyCanvasDesigner _Canvas = null;
private static DrawStyle _CurrentDraw = null;
/// <summary>
/// 当前拖放的控件
/// </summary>
public static DrawStyle CurrentDraw
{
get
{
if (_CurrentDraw == null) _CurrentDraw = new DrawTextBox();
return _CurrentDraw;
}
set { _CurrentDraw = value; }
}
/// <summary>
///当前Designer, 支持多个IDE设计器,控件容器
/// </summary>
public static MyCanvasDesigner CurrentDesigner
{
get { return _Canvas; }
set { _Canvas = value; }
}
}
// 来源:www.CSFramework.com, C/S结构框架学习网
版权声明:本文为开发框架文库发布内容,转载请附上原文出处连接
NewDoc C/S框架网