How to prevent window resizing when the workstation is locked and then unlocked?

We have an application that runs in multi-monitor environments. Typically, users have an application dialog that applies to multiple vats.

If the user locks the workstation and then unlocks it, our application is invited to resize.

Our users find this behavior disappointing, as they then spend some time restoring the previous layout.

We are not yet sure if this is a graphics driver requesting resizing or Windows. I hope through this question it becomes clear which component is responding,

Popular apps like (File) Explorer and Firefox behave the same in this setting. For replication, simply:

  • open explorer ( Win+E )
    • drag the explorer window to a horizontal position greater than 1 screen.
    • lock a workstation ( Win+L ),
    • unlock
    • the application should now only resize to 1 screen.

How to prevent window resizing when the workstation is locked and unlocked?
Do we need to enter a code to check for (un) blocking?
Is there any other mechanism that we do not know about?

+4
source share
3 answers

Before resizing the window, the application will receive the WM_WINDOWPOSCHANGING message from Windows. You can intercept this message and change the settings, forcing the window to remain in place. You have to be careful because you get the same message when the user tries to move or resize the window. Probably when it is maximized or minimized.

Edit: You can use the WTSRegisterSessionNotification function to receive additional messages. Messages are designed to quickly switch users, but the lock screen is implemented in Windows as a system session.

+1
source

A similar question has an answer that allows you to restore the window size in the .net application after unlocking the session .

Someone asked essentially the same question in SuperUser, but from your point of view of the user: How do I stop large window resizing while locking my workstation?

+1
source

I tried the solution given to the question referenced by Leif, and found that the SessionSwitchReason.SessionUnlock event seemed to be fired after the computer was locked, not before. This meant that the size and location of the window was already reset, so resizing failed.

So I had to find another way to store the current size and location before the computer was locked. The only thing I could see was to subscribe to ResizeEnd for ResizeEnd applications and update the pre-lock size and location.

I have not yet managed to get it to work for WPF applications, because WPF does not have the ResizeEnd equivalent (or I have not found it yet) and the subscription to SizeChanged and LocationChanged not good enough, because they start when the computer is locked, and also overwrites the size and location.

In the end, I had to hook into the Windows ExitSizeMove event to keep the current size and position. Details on how to connect to this event can be found here :

 private const int WM_EXITSIZEMOVE = 0x232; private void Window_Loaded(object sender, RoutedEventArgs e) { HwndSource source = HwndSource.FromHwnd(new WindowInteropHelper(this).Handle); source.AddHook(new HwndSourceHook(WndProc)); } private IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled) { if (msg == WM_EXITSIZEMOVE) { // save location and size of window handled = true; } return IntPtr.Zero; } 
0
source

All Articles