非常简单和容易理解,我就不再赘述,其它的各个事件也都一样简单,这里也不给出所有事件的实现代码,只是列举一下还需要实现的代码响应类:
MouseSizeLeft:拉伸左边框
MouseSizeBottom:拉伸下边框
MouseSizeTop:拉伸上边框
MouseSizeTopLeft:拉伸左上角
MouseSizeTopRight:拉伸右上角
MouseSizeBottomLeft:拉伸左下角
MouseSizeBottomRight:拉伸右下角
MouseDrag:鼠标拖动
鼠标拖动同样也很简单,不过却稍不同于窗口的缩放拉伸,这里举出它的实现代码:
public class MouseDrag : MouseAction
{
private int x, y;
public MouseDrag(int hitX, int hitY)
{
x = hitX;
y = hitY;
}
public override void Action(int ScreenX, int ScreenY, System.Windows.Forms.Form form)
{
form.Location = new Point(ScreenX - x, ScreenY - y);
}
}
接下来我们开始编写发出事件的代码,先定义几个变量:
private int LEFT = 5, RIGHT = 5, BOTTOM = 5, TOP = 5, TITLE_WIDTH = 45;//边框和标题栏的大小
private int x = 0, y = 0;//保存鼠标的临时坐标
private MouseAction mouse;//鼠标的事件响应对象
然后在Form的MouseDown事件中记录下鼠标的当前坐标:
x = e.X;
y = e.Y;
附:e为System.Windows.Forms.MouseEventArgs
然后再根据鼠标的坐标定义出事件响应对象:
//鼠标点击左上边框
if((e.X <= LEFT + 10 && e.Y <= TOP) || (e.Y <= TOP + 10 && e.X <= LEFT))
{
mouse = new MouseSizeTopLeft(Location.X, Location.Y, Width, Height);
return;
}
当然有的事件也可以直接响应:
//鼠标点击系统关闭按纽
if(e.X > Width - 20 && e.Y > 6 && e.X < Width - 20 + SysButton_Min.Width && e.Y < 6 + SysButton_Min.Height)
{
Close();
return;
}
大部分的事件响应实际上是在MouseMove事件中完成的:
private void Form_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
{
this.Parent.Cursor = CheckCursorType(e.X, e.Y);//改变鼠标的指针形状
if(mouse != null)
{
mouse.Action(Control.MousePosition.X, Control.MousePosition.Y, this);//执行时间响应
//注意坐标是Control.MousePosition这个静态变量给出的,它的值为鼠标在桌面上的全局坐标
}
}