简体   繁体   English

C#表单移动已停止事件

[英]C# Form Move Stopped Event

Is there any event in C# that fires when the form STOPS being moved. 当表格STOPS被移动时,C#中是否有任何事件触发。 Not while its moving. 不是在它移动的时候。

If there is no event for it, is there a way of doing it with WndProc? 如果没有它的事件,有没有办法用WndProc做到这一点?

The ResizeEnd event fires after a move ends. 移动结束后,ResizeEnd事件将触发。 Perhaps you could use that. 也许你可以使用它。

This is not a failsafe solution, but it's pure .NET and it's dead simple. 这不是一个故障安全的解决方案,但它是纯粹的.NET,它很简单。 Add a timer to your form, set it to a relatively short delay (100-150 ms seemed OK for me). 在表单中添加一个计时器,将其设置为相对较短的延迟(100-150毫秒对我来说似乎没问题)。 Add the following code for the Form.LocationChanged and Timer.Tick events: 为Form.LocationChanged和Timer.Tick事件添加以下代码:

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. 如果您想要更精确的处理(确切地知道标题栏中何时释放鼠标按钮等),您可能需要深入了解监视窗口消息。

我测试了ResizeChanged事件,它工作正常,但我不知道移动和调整大小之间的关系,但它适用于我

I had the same problem with a user control, but it does not have the ResizeEnd event. 我遇到了与用户控件相同的问题,但它没有ResizeEnd事件。 The solution, which worked is to override the WndProc method and listen for EXITSIZEMOVE. 解决方案是覆盖WndProc方法并监听EXITSIZEMOVE。

See example here 这里的例子

Just set a flag to true when onmove events are fired. 只需在触发onmove事件时将标志设置为true即可。 If a mouseup event happens and the flag is true, the form stopped being moved. 如果发生mouseup事件且标志为true,则表单停止移动。

I admit this probably won't work in the case of a user moving a form via the keyboard, but that's pretty rare. 我承认这可能不适用于用户通过键盘移动表单的情况,但这种情况非常罕见。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM