如何捕获结束调整大小窗口?

Med*_*tor 21 c# wpf xaml resize window

我需要在WPF中捕获事件endresize.

Mar*_*tin 27

WPF不提供仅在调整大小过程结束时触发的事件.SizeChanged是与Window大小调整相关联的唯一事件 - 它将在调整大小过程中多次触发.

完全破解是在SizeChanged事件触发时不断设置计时器滴答.然后计时器将无法勾选,直到调整结束并在那时进行一次处理.

public MyUserControl()
{
    _resizeTimer.Tick += _resizeTimer_Tick;
}

DispatcherTimer _resizeTimer = new DispatcherTimer { Interval = new TimeSpan(0, 0, 0, 0, 1500), IsEnabled = false };

private void UserControl_SizeChanged(object sender, SizeChangedEventArgs e)
{
    _resizeTimer.IsEnabled = true;
    _resizeTimer.Stop();
    _resizeTimer.Start();
}

void _resizeTimer_Tick(object sender, EventArgs e)
{
    _resizeTimer.IsEnabled = false;    

    //Do end of resize processing
}
Run Code Online (Sandbox Code Playgroud)

  • Winform中的Form.ResizeBegin/End.通知仍然存在,但在WPF中被忽略.前进两步,后退一步. (6认同)
  • @Martin,请解释为什么你把 _resizeTimer.IsEnabled = true; 停止启动前?这对我来说似乎没有意义。 (2认同)

Jes*_*ter 13

.NET的Reactive Extensions为处理标准事件模式提供了一些非常酷的功能,包括能够限制事件.我在处理大小更改事件时遇到了类似的问题,虽然解决方案仍然有点"hacky",但我认为Reactive Extensions提供了一种更优雅的实现方式.这是我的实现:

IObservable<SizeChangedEventArgs> ObservableSizeChanges = Observable
    .FromEventPattern<SizeChangedEventArgs>(this, "SizeChanged")
    .Select(x => x.EventArgs)
    .Throttle(TimeSpan.FromMilliseconds(200));

IDisposable SizeChangedSubscription = ObservableSizeChanges
    .ObserveOn(SynchronizationContext.Current)
    .Subscribe(x => {
        Size_Changed(x);
    });
Run Code Online (Sandbox Code Playgroud)

这将有效地限制SizeChanged事件,这样你的Size_Changed方法(你可以执行自定义代码)将不会被执行,直到200毫秒(或者你想等待的时间长)已经过去而没有SizeChanged被触发的另一个事件.

private void Size_Changed(SizeChangedEventArgs e) {
    // custom code for dealing with end of size changed here
}
Run Code Online (Sandbox Code Playgroud)


Bil*_*lal 7

不需要计时器,@Bohoo 提供的非常干净的解决方案,我只是将他的代码从 vb.net 改编为 c#

     public partial class MainWindow : Window
        {
            public MainWindow()
            {
                InitializeComponent();
                this.Loaded += MainWindow_Loaded;
            }

            private void MainWindow_Loaded(object sender, RoutedEventArgs e)
            {
                // this two line have to be exactly onload
                HwndSource source = HwndSource.FromHwnd(new WindowInteropHelper(this).Handle);
                source.AddHook(new HwndSourceHook(WndProc));
            }


            const int WM_SIZING = 0x214;
            const int WM_EXITSIZEMOVE = 0x232;
            private static bool WindowWasResized = false;


            private static IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
            {
                if (msg == WM_SIZING)
                {

                    if (WindowWasResized == false)
                    {

                        //    'indicate the the user is resizing and not moving the window
                        WindowWasResized = true;
                    }
                }

                if (msg == WM_EXITSIZEMOVE)
                {

                    // 'check that this is the end of resize and not move operation          
                    if (WindowWasResized == true)
                    {

                        // your stuff to do 
                        Console.WriteLine("End");

                        // 'set it back to false for the next resize/move
                        WindowWasResized = false;
                    }
                }

                return IntPtr.Zero;
            }

    }
Run Code Online (Sandbox Code Playgroud)


Boh*_*hoo 5

您可以准确地检测WPF窗口调整大小的结束时间,并且不需要计时器。WM_EXITSIZEMOVE当用户在窗口调整大小移动操作结束时释放鼠标左键时,本机窗口会收到此消息。WPF窗口没有收到此消息,因此我们需要连接一个WndProc将接收该消息的函数。我们可以使用HwndSourcewith WindowInteropHelper来获取窗口句柄。然后我们将钩子添加到我们的WndProc函数中。我们将在window Loaded事件(vb.net代码)中执行所有操作:

Dim WinSource As HwndSource    

Private Sub WindowLoaded_(sender As Object, e As RoutedEventArgs)

    WinSource = HwndSource.FromHwnd(New WindowInteropHelper(Me).Handle)
    WinSource.AddHook(New HwndSourceHook(AddressOf WndProc))
End Sub
Run Code Online (Sandbox Code Playgroud)

现在,在我们的中WndProc,我们将收听以下WM_EXITSIZEMOVE消息:

Const WM_EXITSIZEMOVE As Integer = &H232

Private Function WndProc(hwnd As IntPtr, msg As Integer, wParam As IntPtr, lParam As IntPtr, ByRef handled As Boolean) As IntPtr

    If msg = WM_EXITSIZEMOVE Then

        DoWhatYouNeed()
    End If

    Return IntPtr.Zero
End Function
Run Code Online (Sandbox Code Playgroud)

此处此处将说明技术和类似技术。

请注意,该函数应返回IntPtr.Zero。另外,除了处理您感兴趣的特定消息外,请勿在此功能中进行任何操作。

现在, WM_EXITSIZEMOVE在移动操作结束时也会发送,我们只对调整大小感兴趣。有几种方法可以确定这是调整大小操作的结束。我通过听WM_SIZING消息(在调整大小期间发送了多次)并结合一个标志来做到这一点。整个解决方案如下所示:

(注意:不要与此处突出显示的代码混淆,对vb.net造成错误)

Dim WinSource As HwndSource
Const WM_SIZING As Integer = &H214
Const WM_EXITSIZEMOVE As Integer = &H232

Dim WindowWasResized As Boolean = False

Private Sub WindowLoaded_(sender As Object, e As RoutedEventArgs)

    WinSource = HwndSource.FromHwnd(New WindowInteropHelper(Me).Handle)
    WinSource.AddHook(New HwndSourceHook(AddressOf WndProc))
End Sub

Private Function WndProc(hwnd As IntPtr, msg As Integer, wParam As IntPtr, lParam As IntPtr, ByRef handled As Boolean) As IntPtr

    If msg = WM_SIZING Then

        If WindowWasResized = False Then

            'indicate the the user is resizing and not moving the window
            WindowWasResized = True
        End If
    End If

    If msg = WM_EXITSIZEMOVE Then

        'check that this is the end of resize and not move operation          
        If WindowWasResized = True Then

             DoWhatYouNeed()

             'set it back to false for the next resize/move
             WindowWasResized = False
        End If            
    End If

    Return IntPtr.Zero
End Function
Run Code Online (Sandbox Code Playgroud)

而已。