.....省略之前代码 public abstract class MouseAction
{
public abstract void Action(int ScreenX, int ScreenY, System.Windows.Forms.Form form);
} public class MouseSizeRight : MouseAction //向右拉伸窗口
{
private int lx;
public MouseSizeRight(int LocationX)
{
lx = LocationX;
} public override void Action(int ScreenX, int ScreenY, System.Windows.Forms.Form form)
{
form.Width = ScreenX - lx;
form.Invalidate();
}
} public class MouseSizeLeft : MouseAction //拉伸左边框
{
private int lx;
public MouseSizeLeft(int LocationX)
{
lx = LocationX;
} public override void Action(int ScreenX, int ScreenY, System.Windows.Forms.Form form)
{
form.Width =??这里填什么我原本写的是this.Right-ScreenX;
form.Invalidate();
}
} //public class MouseSizeTop : MouseAction { } //拉伸上边框 //public class MouseSizeTopLeft : MouseAction { } //拉伸左上角 //public class MouseSizeTopRight : MouseAction { } //拉伸右上角 //public class MouseSizeBottomLeft : MouseAction { } //拉伸左下角 //public class MouseSizeBottomRight : MouseAction { } //拉伸右下角 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;//边框和标题栏的大小
private int x = 0, y = 0;//保存鼠标的临时坐标
private MouseAction mouse; //鼠标的事件响应对象 private void Form1_MouseDown(object sender, MouseEventArgs e)
{
x = e.X;
y = e.Y; int LeftToRight = this.Right - this.Left;
int TopToBottom = this.Bottom - this.Top;
//鼠标点击左上边框
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 >=LeftToRight-5 && e.X<=LeftToRight )
{
mouse = new MouseSizeRight( this.Left);
return;
} //鼠标点击左边框
if (e.X <= LEFT )
{
mouse = new MouseSizeLeft(this.Left);
return;
} //鼠标拖动窗口
if (e.Button ==MouseButtons.Left )
{
mouse = new MouseDrag(Location.X,Location.Y);
return;
}
} private void Form1_MouseMove(object sender, MouseEventArgs e)
{
this.Cursor = CheckCursorType(e.X, e.Y);//改变鼠标的指针形状
if (mouse != null)
{
mouse.Action(Control.MousePosition.X, Control.MousePosition.Y, this);//执行时间响应
//注意坐标是Control.MousePosition这个静态变量给出的,它的值为鼠标在桌面上的全局坐标
} } private Cursor CheckCursorType(int X, int Y)
{ 省略
} private void Form1_MouseUp(object sender, MouseEventArgs e)
{
省略
}相信大家做过无边框 拉伸都看到过类似代码 在网上查到的 。 的确可以实现向右拉伸功能。 有些地方还是看不懂。 mouse.Action(Control.MousePosition.X, Control.MousePosition.Y, this)
这句是不是 获得鼠标当前的坐标 再把X 值传给
public override void Action(int ScreenX, int ScreenY, System.Windows.Forms.Form form)如果向右拉伸 就把 SCREENX-this.Left 就实现了。为什么向左拉伸 用this.Right-鼠标当前坐标横坐标。却不是我想要的结果?表现为 做边框不动 ,
右边框鼠标向左增大 向右减小。自己刚刚开始学C# 对这个问题不是很清楚, 忘高手 耐心看下代码 和 我的文字帮我解答 我想把这个 拉伸边框功能写写完整。
}
}
if (e.X >= wid - 5 && e.Y <= hig - 5 && e.Y >= 5)
{
mouse = new MouseSizeRight(Location.X);
return;
}
//向右下
if ((e.X >= wid - 5 && e.Y >= hig - 5) ¦ ¦ (e.Y >= hig - 5 && e.X >= wid - 5))
{
mouse = new MouseSizeBottomRight(Location.X, Location.Y);
return;
}
//向下
if (y >= hig - 5 && x <= wid - 5 && x >= 5)
{
mouse = new MouseSizeBottom(Location.Y);
return; }
//向左下
if (x <=5 && y >= hig - 5 && y <= hig)
{
mouse = new MouseSizeBottomLeft(Location.Y, wid, hig);
return;
} //向左拉
if (e.X <= 5 && e.Y <= hig - 5 && e.Y >=5)
{ mouse = new MouseSizeLeft(hig);
return;
}
//向左上拉
if (e.X <= 5 && e.Y <= 5&& e.Y > 0)
{
mouse = new MouseSizeTopLeft(Location.Y, hig);
return;
}
//向上拉
if (e.Y <= 5&& e.X <= wid - 5 && e.X >= 5)
{
mouse = new MouseSizeTop(Location.X, Location.Y, wid, hig);
return;
}
//向右上
if (e.X >= wid - 5 && e.Y <= 5)
{
mouse = new MouseSizeTopRight(Location.X, Location.Y, wid, hig);
return;
}
//拉右边
public class MouseSizeRight : MouseAction
{
private int lx;
public MouseSizeRight(int LocationX)
{
lx = LocationX;
}
public override void Action(int ScreenX, int ScreenY, System.Windows.Forms.Form form)
{
form.Width = ScreenX -lx;
form.Invalidate();
}
}
//向下拉
public class MouseSizeBottom : MouseAction
{
private int ly;
public MouseSizeBottom(int LocationY)
{
ly = LocationY;
}
public override void Action(int ScreenX, int ScreenY, System.Windows.Forms.Form form)
{
form.Height = ScreenY -ly;
form.Invalidate();
}
}
//向左下拉
public class MouseSizeBottomLeft : MouseAction
{
private int wid;
private int hig;
private int ly;
public MouseSizeBottomLeft(int LocationY,int LocationWid,int LocationHig)
{
ly = LocationY;
wid = LocationWid;
hig = LocationHig;
}
public override void Action(int ScreenX, int ScreenY, System.Windows.Forms.Form form)
{
int a = ScreenX;
int b = form.DesktopLocation.Y;
form.SetDesktopBounds(a, b, form.Right - a,ScreenY-ly);
form.Invalidate();
form.Invalidate();
}
} //拉右下角
public class MouseSizeBottomRight : MouseAction
{
private int lx;
private int ly;
public MouseSizeBottomRight(int LocationX,int LocationY)
{
lx = LocationX;
ly = LocationY;
}
public override void Action(int ScreenX, int ScreenY, System.Windows.Forms.Form form)
{ form.Width = ScreenX -lx;
form.Height = ScreenY - ly;
form.Invalidate();
}
}
//拉左边
public class MouseSizeLeft : MouseAction
{
private int hig;
public MouseSizeLeft(int Locationhig)
{
hig = Locationhig;
}
public override void Action(int ScreenX, int ScreenY, System.Windows.Forms.Form form)
{
int b = form.DesktopLocation.Y;
form.SetDesktopBounds(ScreenX, b, form.Right - ScreenX, hig);
form.Invalidate();
}
} //拉左上
public class MouseSizeTopLeft : MouseAction
{
private int hig;
private int ly;
public MouseSizeTopLeft( int LxcationY, int LxcationHig)
{
ly = LxcationY;
hig = LxcationHig;
}
public override void Action(int ScreenX, int ScreenY, System.Windows.Forms.Form form)
{
form.SetDesktopBounds(ScreenX, ScreenY, form.Right - ScreenX, hig - ScreenY + ly);
form.Invalidate();
}
}
//拉上边
public class MouseSizeTop : MouseAction
{
private int hig;
private int wid;
private int lx;
private int ly;
public MouseSizeTop(int LxcationX, int LxcationY, int LxcationWid, int LxcationHig)
{
hig = LxcationHig;
lx = LxcationX;
wid = LxcationWid;
ly = LxcationY;
}
public override void Action(int ScreenX, int ScreenY, System.Windows.Forms.Form form)
{
form.SetDesktopBounds(lx, ScreenY, wid, hig - ScreenY+ly);
form.Invalidate();
}
}
//拉右上
public class MouseSizeTopRight : MouseAction
{
private int hig;
private int wid;
private int lx;
private int ly;
public MouseSizeTopRight(int LxcationX, int LxcationY, int LxcationWid, int LxcationHig)
{
hig = LxcationHig;
lx = LxcationX;
wid = LxcationWid;
ly = LxcationY;
}
public override void Action(int ScreenX, int ScreenY, System.Windows.Forms.Form form)
{
form.SetDesktopBounds(lx, ScreenY, ScreenX-lx, hig - ScreenY + ly);
form.Invalidate();
}