本文介绍了我怎样才能移动窗口时,按下鼠标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们能够移动窗口的形式,当我们按下鼠标标题栏。
,但我怎么能移动窗口时,按下鼠标形式?

we able to move windows forms when we mouse down on title bar .but how can I move windows when mouse down in form ?

推荐答案

您需要的时候鼠标记录是向下和向上使用的MouseDown 的MouseUp 事件:

You'll need to record when the mouse is down and up using the MouseDown and MouseUp events:

private bool mouseIsDown = false;
private Point firstPoint;

private void Form1_MouseDown(object sender, MouseEventArgs e)
{
    firstPoint = e.Location;
    mouseIsDown = true;
}

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



正如你所看到的,第一点被记录下来,这样你就可以使用的MouseMove 事件如下:

private void Form1_MouseMove(object sender, MouseEventArgs e)
{
    if (mouseIsDown)
    {
        // Get the difference between the two points
        int xDiff = firstPoint.X - e.Location.X;
        int yDiff = firstPoint.Y - e.Location.Y;

        // Set the new point
        int x = this.Location.X - xDiff;
        int y = this.Location.Y - yDiff;
        this.Location = new Point(x, y);
    }
}

这篇关于我怎样才能移动窗口时,按下鼠标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 06:45