• 保存到桌面加入收藏设为首页
技术

C# 实现无标题栏窗口拖动效果

作者:CrazyEditor   来源:CrazyEditor.cn  
/* 首先将窗体的边框样式修改为None,让窗体没有标题栏
 * 实现这个效果使用了三个事件:鼠标按下、鼠标弹起、鼠标移动
 * 鼠标按下时更改变量isMouseDown标记窗体可以随鼠标的移动而移动
 * 鼠标移动时根据鼠标的移动量更改窗体的location属性,实现窗体移动
 * 鼠标弹起时更改变量isMouseDown标记窗体不可以随鼠标的移动而移动
 */
private bool isMouseDown = false;
private Point FormLocation;     //form的location
private Point mouseOffset;      //鼠标的按下位置
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
    if (e.Button == MouseButtons.Left)
    {
        isMouseDown = true;
        FormLocation = this.Location;
        mouseOffset = Control.MousePosition;
    }
}

private void Form1_MouseUp(object sender, MouseEventArgs e)
{
    isMouseDown = false;
}

private void Form1_MouseMove(object sender, MouseEventArgs e)
{
    int _x = 0;
    int _y = 0;
    if (isMouseDown)
    {
        Point pt = Control.MousePosition;
        _x = mouseOffset.X - pt.X;
        _y = mouseOffset.Y - pt.Y;

        this.Location = new Point(FormLocation.X - _x, FormLocation.Y - _y);
    }

}

本栏最新更新
本栏推荐
阅读排行

(C) 2018-2019 疯狂小编