C# Form Move Stopped Event

后端 未结 5 1370
深忆病人
深忆病人 2020-12-19 13:53

Is there any event in C# that fires when the form STOPS being moved. Not while its moving.

If there is no event for it, is there a way of doing it with WndProc?

5条回答
  •  攒了一身酷
    2020-12-19 14:27

    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.

提交回复
热议问题