C# Form Move Stopped Event

强颜欢笑 提交于 2019-12-01 05:27:14

The ResizeEnd event fires after a move ends. Perhaps you could use that.

This is not a failsafe solution, but it's pure .NET and it's dead simple. Add a timer to your form, set it to a relatively short delay (100-150 ms seemed OK for me). Add the following code for the Form.LocationChanged and Timer.Tick events:

private void Form_LocationChanged(object sender, EventArgs e)
{
    if (this.Text != "Moving")
    {
        this.Text = "Moving";
    }
    tmrStoppedMoving.Start();
}

private void Timer_Tick(object sender, EventArgs e)
{
    tmrStoppedMoving.Start();
    this.Text = "Stopped";
}

If you want more exact handling (knowing exactly when the mouse button is release in the title bar and such) you will probably need to dive into monitoring windows messages.

Just set a flag to true when onmove events are fired. If a mouseup event happens and the flag is true, the form stopped being moved.

I admit this probably won't work in the case of a user moving a form via the keyboard, but that's pretty rare.

I had the same problem with a user control, but it does not have the ResizeEnd event. The solution, which worked is to override the WndProc method and listen for EXITSIZEMOVE.

See example here

I tested ResizeChanged event, and it works fine, however I don't know relation between move and resize, but it works for me

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!