Event Order with Maximized ToolWindow

1 Answer 34 Views
Window
Mustafa
Top achievements
Rank 1
Mustafa asked on 21 Dec 2023, 09:38 AM | edited on 22 Dec 2023, 01:26 PM

To simplify things, consider the following MainWindow class inheriting fromSystem.Windows.Window:

using System;
using System.Diagnostics;
using System.Windows;
using System.Windows.Input;

namespace WpfApp1
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            StateChanged += MainWindow_StateChanged;
            MouseUp += MainWindow_MouseUp;
        }

        private void MainWindow_StateChanged(object sender, EventArgs e)
        {
            if (WindowState == WindowState.Maximized)
            {
                Debug.WriteLine("Window Maximized");
            }
        }

        private void MainWindow_MouseUp(object sender, MouseButtonEventArgs e)
        {
            Debug.WriteLine("Window MouseUp");
        }
    }
}

When this window is in normal state if I double click its title bar then it is maximized and a Window.MouseUp event is raised, which results in the following output in VS Output pane:

Window Maximized
Window MouseUp
Now replace this Window with Telerik.Windows.Controls.Docking.ToolWindow.cs (v 2022.3.1005.60) and assume we double click the title bar of a ToolWindow and make it maximized. My question is: Are these events raised always in this particular order; that is first maximized and then mouse up in ToolWindow? Can you guarantee this order with all conditions? (Heavy UI load, multiple threads etc.) Guaranteeing this order is crucial for me since I depend on this order for my control in the ToolWindow.

1 Answer, 1 is accepted

Sort by
0
Martin Ivanov
Telerik team
answered on 25 Dec 2023, 07:03 AM

Hello Mustafa,

I already answered this in the other thread, but I will post my post also here:

"Yes, this is the standard events lifecycle of the corresponding action (double click to maximize the window). You have a double click which consists of two MouseDown and two MouseUp events. The window state change happens on the second MouseDown. However, you will notice that the MouseUp won't be invoked when the window state changes from "maximized" to "restored" using the mouse double click. This happens because the restoration happens before the mouse up event, the mouse position is no longer under the window, which is why MouseUp is not invoked.

In the general case, you can expect the MouseUp to be invoked in the corresponding order when you maximize the window. But, I won't guarantee you this will always be the case absolutely every time. I suggest you to implement code that checks if this happens or approach your original problem from another angle. That is because you have some factors that can affect this. The first one is that the mouse can be captured from someone else during this action. For this to happen you should write it manually, but it is good to take the following situation into account. If another element captures the mouse between the MouseDown and MouseUp actions, the window won't receive the mouse up event. The same will happen also if the window gets moved and it is no longer under the mouse when its left button is up. Also, I can't tell what will happen in a multithreading scenario. 

To summarize this, that should work, but please don't rely solely on this and write some code that handles your functionality in case the MouseUp event is not invoked."

Regards,
Martin Ivanov
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

Hüseyin
Top achievements
Rank 1
commented on 27 Dec 2023, 06:52 AM

Hi Martin. Thanks a lot.
Tags
Window
Asked by
Mustafa
Top achievements
Rank 1
Answers by
Martin Ivanov
Telerik team
Share this question
or