-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Dpi awareness context fixes from classic + deprecate unspecified context #399
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
Closed
Closed
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
c59877c
move dpi related common unsafes to DpiCommonUnsafeNatives, privatize …
1f15d61
nullable dpiAwarenessContext
a2880b3
fix exception thrown, fix tests
f18222d
Merge branch 'master' into dev/zadanz/hdpi-fixes
zsd4yr 80c0fe0
Merge branch 'master' into dev/zadanz/hdpi-fixes
cd699e5
reorganize dpihelper methods into submethods, add nullable
b3889dd
access denied exception
a50d68c
resolve merge from master conflict
c191d6c
respond to pr feedback: redundant elses
zsd4yr 740fca0
Merge branch 'master' into dev/zadanz/hdpi-fixes
zsd4yr File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
108 changes: 108 additions & 0 deletions
108
src/System.Windows.Forms.Design.Editors/src/Misc/DpiUnsafeNativeMethods.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,108 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
| // See the LICENSE file in the project root for more information. | ||
|
|
||
| namespace System.Windows.Forms | ||
| { | ||
| using Runtime.InteropServices; | ||
| using Runtime.Versioning; | ||
| using System; | ||
|
|
||
| internal class DpiUnsafeNativeMethods | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as before: Would it make sense to consolidate this with the Runtime via a File Reference?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes very probably |
||
| { | ||
|
|
||
| // This section could go to Nativemethods.cs or Safenativemethods.cs but we have separate copies of them in each library (System.winforms, System.Design and System.Drawing). | ||
| // Keeping them here will reduce duplicating code but may have to take care of security warnings (if any). | ||
| // These APIs are available starting Windows 10, version 1607 only. | ||
|
|
||
| [DllImport(ExternDll.User32, SetLastError = true, ExactSpelling = true, CharSet = CharSet.Auto)] | ||
| [ResourceExposure(ResourceScope.None)] | ||
| internal static extern DpiAwarenessContext GetThreadDpiAwarenessContext(); | ||
|
|
||
| [DllImport(ExternDll.User32, SetLastError = true, ExactSpelling = true, CharSet = CharSet.Auto)] | ||
| [ResourceExposure(ResourceScope.None)] | ||
| internal static extern DpiAwarenessContext SetThreadDpiAwarenessContext(DpiAwarenessContext dpiContext); | ||
|
|
||
| [DllImport(ExternDll.User32, SetLastError = true, ExactSpelling = true, CharSet = CharSet.Auto)] | ||
| [return: MarshalAs(UnmanagedType.Bool)] | ||
| internal static extern bool AreDpiAwarenessContextsEqual(DpiAwarenessContext dpiContextA, DpiAwarenessContext dpiContextB); | ||
|
|
||
| [DllImport(ExternDll.User32, SetLastError = true, ExactSpelling = true, CharSet = CharSet.Auto)] | ||
| [ResourceExposure(ResourceScope.None)] | ||
| internal static extern DpiAwarenessContext GetWindowDpiAwarenessContext(IntPtr hwnd); | ||
|
|
||
| /// <summary> | ||
| /// Tries to compare two DPIawareness context values. Return true if they were equal. | ||
| /// Return false when they are not equal or underlying OS does not support this API. | ||
| /// </summary> | ||
| /// <returns>true/false</returns> | ||
| public static bool TryFindDpiAwarenessContextsEqual(DpiAwarenessContext dpiContextA, DpiAwarenessContext dpiContextB) | ||
| { | ||
| if(dpiContextA == DpiAwarenessContext.DPI_AWARENESS_CONTEXT_UNSPECIFIED && dpiContextB == DpiAwarenessContext.DPI_AWARENESS_CONTEXT_UNSPECIFIED) | ||
| { | ||
| return true; | ||
| } | ||
| if (ApiHelper.IsApiAvailable(ExternDll.User32, nameof(DpiUnsafeNativeMethods.AreDpiAwarenessContextsEqual))) | ||
| { | ||
| return AreDpiAwarenessContextsEqual(dpiContextA, dpiContextB); | ||
| } | ||
|
|
||
| return false; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Tries to get thread dpi awareness context | ||
| /// </summary> | ||
| /// <returns> returns thread dpi awareness context if API is available in this version of OS. otherwise, return IntPtr.Zero.</returns> | ||
| public static DpiAwarenessContext TryGetThreadDpiAwarenessContext() | ||
| { | ||
| if (ApiHelper.IsApiAvailable(ExternDll.User32, nameof(DpiUnsafeNativeMethods.GetThreadDpiAwarenessContext))) | ||
| { | ||
| return GetThreadDpiAwarenessContext(); | ||
| } | ||
| else | ||
| { | ||
| // legacy OS that does not have this API available. | ||
| return DpiAwarenessContext.DPI_AWARENESS_CONTEXT_UNSPECIFIED; | ||
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Tries to set thread dpi awareness context | ||
| /// </summary> | ||
| /// <returns> returns old thread dpi awareness context if API is available in this version of OS. otherwise, return IntPtr.Zero.</returns> | ||
| public static DpiAwarenessContext TrySetThreadDpiAwarenessContext(DpiAwarenessContext dpiContext) | ||
| { | ||
| if (ApiHelper.IsApiAvailable(ExternDll.User32, nameof(DpiUnsafeNativeMethods.SetThreadDpiAwarenessContext))) | ||
| { | ||
| if (dpiContext == DpiAwarenessContext.DPI_AWARENESS_CONTEXT_UNSPECIFIED) | ||
| { | ||
| throw new ArgumentException(nameof(dpiContext), dpiContext.ToString()); | ||
| } | ||
| return SetThreadDpiAwarenessContext(dpiContext); | ||
| } | ||
| else | ||
| { | ||
| // legacy OS that does not have this API available. | ||
| return DpiAwarenessContext.DPI_AWARENESS_CONTEXT_UNSPECIFIED; | ||
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Tries to get window dpi awareness context | ||
| /// </summary> | ||
| /// <returns> returns window dpi awareness context if API is available in this version of OS. otherwise, return DpiAwarenessContext.DPI_AWARENESS_CONTEXT_UNSPECIFIED.</returns> | ||
| public static DpiAwarenessContext TryGetWindowDpiAwarenessContext(IntPtr hwnd) | ||
| { | ||
| if (ApiHelper.IsApiAvailable(ExternDll.User32, "GetWindowDpiAwarenessContext")) | ||
| { | ||
| return GetWindowDpiAwarenessContext(hwnd); | ||
| } | ||
| else | ||
| { | ||
| // legacy OS that does not have this API available. | ||
| return DpiAwarenessContext.DPI_AWARENESS_CONTEXT_UNSPECIFIED; | ||
| } | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In line 585ff in this file, should we add DpiUnawareGdiScaled to have parity with the DpiHelper from the runtime?
Can we actually consolidate DpiHelper, maybe with #ifs and File References, so we have parity here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, DpiHelper and other common files should be shared between the projects. Either linked into each project or into a m=new assembly that has internals visible to all other assemblies in this repo(as needed). Probably keeping files in
commonfolder is a good stating point.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@JuditRose is working on creating this infrastructure for PR #439
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is a Common folder already in winforms/src folder. Files in the Common folder can be referenced from the csproj files. E.g. https://github.com/dotnet/winforms/blob/master/src/System.Windows.Forms/src/System.Windows.Forms.csproj#L24