C#怎么用鼠标画框创建控件
本篇内容主要讲解“C#怎么用鼠标画框创建控件”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“C#怎么用鼠标画框创建控件”吧!
大姚ssl适用于网站、小程序/APP、API接口等需要进行数据传输应用场景,ssl证书未来市场广阔!成为成都创新互联公司的ssl证书销售渠道,可以享受市场价格4-6折优惠!如果有意向欢迎电话联系或者加微信:13518219792(备注:SSL证书合作)期待与您的合作!
首先定义Control _Owner,使用是的Control类型是因为我们不仅仅需要在Winform上增加控件,也需要在其它容器,比如Panel,GroupBox等上面增加容器,比如Panel,GroupBox等上面增加容器。
然后重新Control上的鼠标事件Control_MouseDown,Control_MouseMove,Control_MouseUp,Control_MouseEnter。
直接上源码
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
namespace SumBLL.DrawControl
{
#region 鼠标拖动创建控件
///
/// 鼠标拖动创建事件
///
public class MouseHook
{
Control _Owner;
//要画的子控件
private Control _childCtrl;
private bool _autosize;
private int _CLickAtX;
private int _ClickAtY;
private int _MoveAtX;
private int _MoveAtY;
private bool _BeginDrag;
private bool _BeginDrawControl;
///
/// 对外公共类
///
public Control DrawCtrl
{
get
{
return _childCtrl;
}
set
{
_childCtrl = value;
}
}
///
/// 这里Owner使用的是Control类型,是因为我们不仅仅需要在Winform上增加控件,
/// 也需要在其它容器,比如Panel,GroupBox等上面增加容器
///
///
public MouseHook(Control Owner, bool autosize = true)
{
this._Owner = Owner;
this._Owner.MouseDown += new MouseEventHandler(this.Control_MouseDown);
this._Owner.MouseMove += new MouseEventHandler(this.Control_MouseMove);
this._Owner.MouseUp += new MouseEventHandler(this.Control_MouseUp);
this._Owner.MouseEnter += new EventHandler(this.Control_MouseEnter);
this._BeginDrawControl = false;
this._autosize = autosize;
}
#region Control上的鼠标事件
void Control_MouseDown(object sender, MouseEventArgs e)
{
//判断选择控件,还是选择画笔
if (_childCtrl != null)
{
this._CLickAtX = e.X;
this._ClickAtY = e.Y;
this._MoveAtX = e.X;
this._MoveAtY = e.Y;
this._BeginDrag = true;
if (_childCtrl != null)
{
this._BeginDrawControl = true;
}
else
{
this._BeginDrawControl = false;
}
}
}
void Control_MouseMove(object sender, MouseEventArgs e)
{
if(_childCtrl != null)
{
if(this._BeginDrag)
{
//取消上次绘制的选择框
int iLeft, iTop, iWidth, iHeight;
Pen pen;
Rectangle rect;
pen = new Pen(this._Owner.BackColor);
if(this._BeginDrawControl == true)
{
pen.DashStyle = DashStyle.Solid;
pen.Width = 2;
}
else
{
pen.DashStyle = DashStyle.Dot;
}
iLeft = this._CLickAtX < this._MoveAtX ? this._CLickAtX : this._MoveAtX;
iTop = this._ClickAtY < this._MoveAtY ? this._ClickAtY : this._MoveAtY;
iWidth = Math.Abs(this._MoveAtX - this._CLickAtX);
iHeight = Math.Abs(this._MoveAtY - this._ClickAtY);
rect = new Rectangle(iLeft, iTop, iWidth, iHeight);
this._Owner.CreateGraphics().DrawRectangle(pen, rect);
//重新绘制选择框
this._MoveAtX = e.X;
this._MoveAtY = e.Y;
pen = new Pen(Color.Black);
if(this._BeginDrawControl == true)
{
pen.DashStyle = DashStyle.Solid;
pen.Width = 2;
}
else
{
pen.DashStyle = DashStyle.Dot;
}
iLeft = this._CLickAtX < this._MoveAtX ? this._CLickAtX : this._MoveAtX;
iTop = this._ClickAtY < this._MoveAtY ? this._ClickAtY : this._MoveAtY;
iWidth = Math.Abs(this._MoveAtX - this._CLickAtX);
iHeight = Math.Abs(this._MoveAtY - this._ClickAtY);
rect = new Rectangle(iLeft, iTop, iWidth, iHeight);
this._Owner.CreateGraphics().DrawRectangle(pen, rect);
}
}
}
void Control_MouseUp(object sender, MouseEventArgs e)
{
this._BeginDrag = false;
this._Owner.SuspendLayout();
if(_childCtrl != null)
{
//取消上次绘制的选择框
int iLeft, iTop, iWidth, iHeight;
Pen pen;
Rectangle rect;
pen = new Pen(this._Owner.BackColor);
pen.DashStyle = DashStyle.Dot;
iLeft = this._CLickAtX < this._MoveAtX ? this._CLickAtX : this._MoveAtX;
iTop = this._ClickAtY < this._MoveAtY ? this._ClickAtY : this._MoveAtY;
iWidth = Math.Abs(this._MoveAtX - this._CLickAtX);
iHeight = Math.Abs(this._MoveAtY - this._ClickAtY);
rect = new Rectangle(iLeft, iTop, iWidth, iHeight);
this._Owner.CreateGraphics().DrawRectangle(pen, rect);
if(_childCtrl != null)
{
AddControl(_childCtrl, rect);
}
else
{
//这里是拖动鼠标,选择控件,这里将会在后续的介绍中给出
}
}
this._Owner.Refresh();
this._Owner.ResumeLayout();
}
void Control_MouseEnter(object sender, EventArgs e)
{
if(_childCtrl != null)
{
this._Owner.Cursor = Cursors.Cross;
}
else
{
this._Owner.Cursor = Cursors.Default;
}
}
private void AddControl(Control control, Rectangle rect)
{
try
{
control.Location = rect.Location;
if(!_autosize)
control.Size = rect.Size;
//control.Name = GetControlName(control);
//因为对于DataTimePiker控件来说不能设置.Text为非日期型,所以忽略错误
try
{
control.Text = GetControlType(control);
}
catch { }
this._Owner.Controls.Add(control);
control.Visible = true;
this._Owner.Cursor = Cursors.Default;
_childCtrl = null;
}
catch(Exception e)
{
this._Owner.Cursor = Cursors.Default;
_childCtrl = null;
}
}
private string GetControlType(Control ctrl)
{
string strType = ctrl.GetType().ToString();
string strControlType;
string[] strArr = strType.Split(".".ToCharArray());
strControlType = strArr[strArr.Length - 1].Trim();
return strControlType;
}
private string GetControlName(Control control)
{
//这里简单返回控件名,如果需要,可以通过修改这个函数做特殊处理
return control.GetType().Name;
}
#endregion
}
#endregion
}
外部调用方法
//用于拖动控件的函数类,在窗体初始化时直接实例
private MouseHook _MouseHook;
//初始化Panel显示动态巡更点
this._MouseHook = new MouseHook(pnlRouteDetail);
//创建用户控件实例
UCPatRouteDetail ucrd = new UCPatRouteDetail(_nowPatRouteDetail);
ucrd.Name = _nowPatRouteDetail.RouteID + _nowPatRouteDetail.DetailID;
ucrd.MouseDown += MouseDown;
ucrd.MouseMove += MouseMove;
ucrd.MouseUp += MouseUp;
ucrd.Delevent += DelUserControl;
//给画图地方赋值新实例
_MouseHook.DrawCtrl = ucrd;
到此,相信大家对“C#怎么用鼠标画框创建控件”有了更深的了解,不妨来实际操作一番吧!这里是创新互联网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!
名称栏目:C#怎么用鼠标画框创建控件
文章网址:http://cdiso.cn/article/ggsodd.html