我把Form1的FormBorderStyle设为none,当我按下鼠标左键时,如何让窗体一起移动?(移动时窗体不能闪烁)

解决方案 »

  1.   

    响应mousedown和mouseup事件,在鼠标按下后记录鼠标相对屏幕左上角的位移,同时让form在x和y方向移动相同距离即可
      

  2.   

    首先我们先定义出一些响应事件的代码,我定义了一个抽象的基类MouseAction,用来表示所有的鼠标事件,它有一个抽象方法Action:
    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();
    }
    }
    非常简单和容易理解,我就不再赘述,其它的各个事件也都一样简单,这里也不给出所有事件的实现代码,只是列举一下还需要实现的代码响应类:
    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这个静态变量给出的,它的值为鼠标在桌面上的全局坐标
    }
    }
    给出每个不同部位的鼠标的指针形状:
    private Cursor CheckCursorType(int X, int Y)
    {
    if(((X <= LEFT + 10 && Y <= TOP) || (Y <= TOP + 10 && X <= LEFT)) || ((X >= Width - RIGHT - 10 && Y >= Height - BOTTOM) || (Y >= Height - BOTTOM - 10 && X >= Width - RIGHT)))
    {
    return Cursors.SizeNWSE;
    }
    else if(((Y <= TOP + 10 && X >= Width - RIGHT) || (Y <= TOP && X >= Width - RIGHT - 10)) || ((X <= LEFT && Y >= Height - BOTTOM - 10) || (Y >= Height - BOTTOM && X <= LEFT + 10)))
    {
    return Cursors.SizeNESW;
    }
    else if(X >= Width - RIGHT || X <= LEFT)
    {
    return Cursors.SizeWE;
    }
    else if(Y >= Height - BOTTOM || Y <= TOP)
    {
    return Cursors.SizeNS;
    }
    else
    {
    return Cursors.Arrow;
    }
    }
    最后在MouseUp事件中将mouse变量释放掉:
    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();
    }
    }
    }
    防止窗体被缩小成一个点,最好给窗口的MinimumSize赋上一个适当的值,例如200,200。