RaUI/Source/RyPrint/Mod/MoveControl.cs

204 lines
6.5 KiB
C#
Raw Normal View History

2020-11-28 07:03:28 +00:00
/******************************************************************
* SamWang
* 2012-5-10 16:06
*
*
*
* V1.0
* VS2010
******************************************************************/
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
namespace DragControl
{
public class MoveControl
{
#region Constructors
public MoveControl(Control ctrl)
{
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.MouseDown += new MouseEventHandler(MouseDown);
currentControl.MouseMove += new MouseEventHandler(MouseMove);
currentControl.MouseUp += new MouseEventHandler(MouseUp);
currentControl.KeyDown += CurrentControl_KeyDown;
//currentControl.KeyUp += CurrentControl_KeyUp;
}
private void CurrentControl_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Up)
{
foreach (Control ct in this.currentControl.Parent.Controls)
{
if (ct is FrameControl)
{
ct.Top--;
}
}
}
else if (e.KeyCode == Keys.Down)
{
foreach (Control ct in this.currentControl.Parent.Controls)
{
if (ct is FrameControl)
{
ct.Top++;
}
}
}
else if (e.KeyCode == Keys.Left)
{
foreach (Control ct in this.currentControl.Parent.Controls)
{
if (ct is FrameControl)
{
ct.Left--;
}
}
}
else if (e.KeyCode == Keys.Right)
{
foreach (Control ct in this.currentControl.Parent.Controls)
{
if (ct is FrameControl)
{
ct.Left++;
}
}
}
}
private void CurrentControl_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Delete)
{
if (this.currentControl.Parent != null)
{
this.currentControl.Parent.Refresh();//刷新父容器,清除掉其他控件的边框
foreach (Control ct in this.currentControl.Parent.Controls)
{
if (ct is FrameControl)
{
this.currentControl.Parent.Controls.Remove(ct);
}
}
}
if (fc != null) { fc.Dispose(); }
}
}
public void CurrentControl_Disposed(object sender, EventArgs e)
{
}
/// <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[5]{new Point(0,0),new Point(width -1,0),
new Point(width -1,height -1),new Point(0,height-1),new Point(0,0)};
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)
{
this.currentControl.Parent.Refresh();//刷新父容器,清除掉其他控件的边框
this.currentControl.BringToFront();
foreach (Control ct in this.currentControl.Parent.Controls)
{
if (ct is FrameControl)
{
this.currentControl.Parent.Controls.Remove(ct);
}
}
if (fc != null) { fc.Dispose(); }
fc = new FrameControl(this.currentControl);
this.currentControl.Parent.Controls.Add(fc);
fc.Visible = true;
fc.Draw();
}
/// <summary>
/// 鼠标按下事件:记录当前鼠标相对窗体的坐标
/// </summary>
void MouseDown(object sender, MouseEventArgs e)
{
pPoint = Cursor.Position;
}
/// <summary>
/// 鼠标移动事件:让控件跟着鼠标移动
/// </summary>
void MouseMove(object sender, MouseEventArgs e)
{
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;
currentControl.Location = new Point(currentControl.Location.X + x, currentControl.Location.Y + y);
if (fc != null) fc.Location = currentControl.Location;
pPoint = cPoint;
}
}
/// <summary>
/// 鼠标弹起事件:让自定义的边框出现
/// </summary>
void MouseUp(object sender, MouseEventArgs e)
{
this.currentControl.Refresh();
if (fc != null)
{
fc.Visible = true;
fc.Draw();
}
}
#endregion
}
}