Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix regression in ParkHandle method from .NET framework #4796

Merged
merged 10 commits into from
May 21, 2021

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -57,5 +57,137 @@ private Form InitFormWithControlToGarbageCollect()

return form;
}


[WinFormsFact]
public void ParkingWindow_Unaware()
{
// set thread awareness context to PermonitorV2(PMv2).
// if process/thread is not in PMv2, calling 'EnterDpiAwarenessScope' is a no-op and that is by design.
// In this case, we will be setting thread to PMv2 mode and then scope to UNAWARE
IntPtr originalAwarenessContext = User32.SetThreadDpiAwarenessContext(User32.DPI_AWARENESS_CONTEXT.PER_MONITOR_AWARE_V2);

try
{
using (DpiHelper.EnterDpiAwarenessScope(User32.DPI_AWARENESS_CONTEXT.UNAWARE))
{
using var control = new Control();
ThreadContext cxt = GetContextForHandle(new HandleRef(this, control.Handle));
Assert.NotNull(cxt);
ParkingWindow parkingWindow = cxt.GetParkingWindowForContext(User32.DPI_AWARENESS_CONTEXT.UNAWARE);
Assert.NotNull(parkingWindow);

IntPtr dpiContext = User32.GetWindowDpiAwarenessContext(parkingWindow.Handle);
Assert.True(User32.AreDpiAwarenessContextsEqual(User32.DPI_AWARENESS_CONTEXT.UNAWARE, dpiContext));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AssertExtention that compares DPI awareness contexts would be helpful here and in the future. You can factor out this line into a helper method.

}
}
finally
{
// reset back to original awareness context.
User32.SetThreadDpiAwarenessContext(originalAwarenessContext);
}
}

[WinFormsFact]
public void ParkingWindow_SystemAware()
{
// set thread awareness context to PermonitorV2(PMv2).
// if process/thread is not in PMv2, calling 'EnterDpiAwarenessScope' is a no-op and that is by design.
// In this case, we will be setting thread to PMv2 mode and then scope to UNAWARE
IntPtr originalAwarenessContext = User32.SetThreadDpiAwarenessContext(User32.DPI_AWARENESS_CONTEXT.PER_MONITOR_AWARE_V2);

try
{
using (DpiHelper.EnterDpiAwarenessScope(User32.DPI_AWARENESS_CONTEXT.SYSTEM_AWARE))
{
using var control = new Control();
ThreadContext cxt = GetContextForHandle(new HandleRef(this, control.Handle));
Assert.NotNull(cxt);
ParkingWindow parkingWindow = cxt.GetParkingWindowForContext(User32.DPI_AWARENESS_CONTEXT.SYSTEM_AWARE);
Assert.NotNull(parkingWindow);

IntPtr dpiContext = User32.GetWindowDpiAwarenessContext(parkingWindow.Handle);
Assert.True(User32.AreDpiAwarenessContextsEqual(User32.DPI_AWARENESS_CONTEXT.SYSTEM_AWARE, dpiContext));
}
}
finally
{
// reset back to original awareness context.
User32.SetThreadDpiAwarenessContext(originalAwarenessContext);
}
}

[WinFormsFact]
public void ParkingWindow_PermonitorV2()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion: ParkingWindow_PerMonitorV2

{
// set thread awareness context to PermonitorV2(PMv2).
// if process/thread is not in PMv2, calling 'EnterDpiAwarenessScope' is a no-op and that is by design.
// In this case, we will be setting thread to PMv2 mode and then scope to UNAWARE
IntPtr originalAwarenessContext = User32.SetThreadDpiAwarenessContext(User32.DPI_AWARENESS_CONTEXT.PER_MONITOR_AWARE_V2);

try
{
using var control = new Control();
ThreadContext cxt = GetContextForHandle(new HandleRef(this, control.Handle));
Assert.NotNull(cxt);

ParkingWindow parkingWindow = cxt.GetParkingWindowForContext(User32.DPI_AWARENESS_CONTEXT.PER_MONITOR_AWARE_V2);
Assert.NotNull(parkingWindow);

IntPtr dpiContext = User32.GetWindowDpiAwarenessContext(parkingWindow.Handle);
Assert.True(User32.AreDpiAwarenessContextsEqual(User32.DPI_AWARENESS_CONTEXT.PER_MONITOR_AWARE_V2, dpiContext));
}
finally
{
// reset back to original awareness context.
User32.SetThreadDpiAwarenessContext(originalAwarenessContext);
}
}

[WinFormsFact]
public void ParkingWindow_Multiple()
{
// set thread awareness context to PermonitorV2(PMv2).
// if process/thread is not in PMv2, calling 'EnterDpiAwarenessScope' is a no-op and that is by design.
// In this case, we will be setting thread to PMv2 mode and then scope to UNAWARE
IntPtr originalAwarenessContext = User32.SetThreadDpiAwarenessContext(User32.DPI_AWARENESS_CONTEXT.PER_MONITOR_AWARE_V2);

try
{
using var control = new Control();
ThreadContext cxt = GetContextForHandle(new HandleRef(this, control.Handle));
Assert.NotNull(cxt);
ParkingWindow parkingWindow = cxt.GetParkingWindowForContext(User32.DPI_AWARENESS_CONTEXT.PER_MONITOR_AWARE_V2);
Assert.NotNull(parkingWindow);


IntPtr dpiContext = User32.GetWindowDpiAwarenessContext(parkingWindow.Handle);
Assert.True(User32.AreDpiAwarenessContextsEqual(User32.DPI_AWARENESS_CONTEXT.PER_MONITOR_AWARE_V2, dpiContext));

using (DpiHelper.EnterDpiAwarenessScope(User32.DPI_AWARENESS_CONTEXT.SYSTEM_AWARE))
{
using var systemControl = new Control();
cxt = GetContextForHandle(new HandleRef(this, systemControl.Handle));
Assert.NotNull(cxt);
parkingWindow = cxt.GetParkingWindowForContext(User32.DPI_AWARENESS_CONTEXT.SYSTEM_AWARE);
Assert.NotNull(parkingWindow);

dpiContext = User32.GetWindowDpiAwarenessContext(parkingWindow.Handle);
Assert.True(User32.AreDpiAwarenessContextsEqual(User32.DPI_AWARENESS_CONTEXT.SYSTEM_AWARE, dpiContext));

// check PMv2 parking window still available.
parkingWindow = cxt.GetParkingWindowForContext(User32.DPI_AWARENESS_CONTEXT.PER_MONITOR_AWARE_V2);
Assert.NotNull(parkingWindow);

dpiContext = User32.GetWindowDpiAwarenessContext(parkingWindow.Handle);
Assert.True(User32.AreDpiAwarenessContextsEqual(User32.DPI_AWARENESS_CONTEXT.PER_MONITOR_AWARE_V2, dpiContext));
}
}
finally
{
// reset back to original awareness context.
User32.SetThreadDpiAwarenessContext(originalAwarenessContext);
}
}
}
}