// Mouse Position Operation //图片的划分 //标题栏 //系统按钮 namespace mouse 鼠标移动 ,以及反应代码 //定义了一个抽象的基类MouseAction,用来表示所有的鼠标事件,它有一个抽象方法Action: //向右拉伸窗口事件的代码响应 public override void Action(int ScreenX, int ScreenY, System.Windows.Forms.Form form) // 鼠标拖动同样也很简单,不过却稍不同于窗口的缩放拉伸,这里举出它的实现代码: public override void Action(int ScreenX, int ScreenY, System.Windows.Forms.Form form) //接下来我们开始编写发出事件的代码,先定义几个变量: ////////// //鼠标点击左上边框 //鼠标点击系统关闭按纽 //大部分的事件响应实际上是在MouseMove事件中完成的: 最后,MouseUp事件中将mouse变量释放掉: 加上标题栏的双击最大化或者还原的事件: //防止窗体被缩小成一个点,最好给窗口的MinimumSize赋上一个适当的值,例如200,200 }
//Code Technology Document
//NITI Collection
//Bottom_Left
//Bottom_Middle
//Bottom_Right
//
//Middle_Left左边框
//Middle_Right右边框
//Top_Left
//Top_Middle
//Top_Right
//SysButton_Min
//SysButton_Max
//SysButton_Close
//SysButton_Restore
{
//以下是上述画皮肤方法的具体实现部分,举一个画左边框的代码示例,
private void DrawMiddle_Left(Graphics g)
{
Brush brush = new TextureBrush(Middle_Left, new Rectangle(0, 0,Middle_Left.Width, Middle_Left.Height));
g.FillRectangle(brush, 0, TITLE_WIDTH, Middle_Left.Width,Height - Bottom_Middle.Height - TITLE_WIDTH);
}
public abstract class MouseAction
{
public abstract void Action(int ScreenX, int ScreenY, System.Windows.Forms.Form form);
}
// MouseSizeLeft:拉伸左边框
// MouseSizeBottom:拉伸下边框
// MouseSizeTop:拉伸上边框
// MouseSizeTopLeft:拉伸左上角
// MouseSizeTopRight:拉伸右上角
// MouseSizeBottomLeft:拉伸左下角
// MouseSizeBottomRight:拉伸右下角
// MouseDrag:鼠标拖动
public class MouseSizeRight : MouseAction
{
private int lx;
public MouseSizeRight(int LocationX)
{
lx = LocationX;
}
{
form.Width = ScreenX - lx;
form.Invalidate();
}
}
public class MouseDrag : MouseAction
{
private int x, y;
public MouseDrag(int hitX, int hitY)
{
x = hitX;
y = hitY;
}
{
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;
}
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这个静态变量给出的,它的值为鼠标在桌面上的全局坐标
}
}
private void Form_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
{
mouse = null;
}
private void Form_DoubleClick(object sender, System.EventArgs e)
{
if(y > TOP && y < TITLE_WIDTH)
{
if(WindowState == FormWindowState.Normal)
{
WindowState = FormWindowState.Maximized;
SysButton = SysButton_Restore;
Invalidate();
}
else if(WindowState == FormWindowState.Maximized)
{
WindowState = FormWindowState.Normal;
SysButton = SysButton_Max;
Invalidate();
}
}
}
C#鼠标位置
80酷酷网 80kuku.com