This repository was archived by the owner on Feb 25, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6k
Gets the DPI for all awareness mode and older Windows versions #15951
Merged
Merged
Changes from 9 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
b9a131b
Support system dpi awareness for older versions of Windows
franciscojma86 f04b0b9
Fix documentation
franciscojma86 4f5eae9
Fix documentation
franciscojma86 040295d
Address comments
franciscojma86 c6f3aae
Remove unused call
franciscojma86 b2f68e3
Another one
franciscojma86 a9eb31c
Changed to get
franciscojma86 408d22d
Format
franciscojma86 61bc810
Documentation
franciscojma86 1bb1a02
Address review
franciscojma86 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,60 +13,56 @@ bool AssignProcAddress(HMODULE comBaseModule, const char* name, T*& outProc) { | |
} // namespace | ||
|
||
Win32DpiHelper::Win32DpiHelper() { | ||
// TODO ensure that this helper works correctly on downlevel builds. | ||
user32_module_ = LoadLibraryA("User32.dll"); | ||
if (user32_module_ == nullptr) { | ||
return; | ||
} | ||
|
||
if (!AssignProcAddress(user32_module_, "EnableNonClientDpiScaling", | ||
enable_non_client_dpi_scaling_)) { | ||
shlib_module_ = LoadLibraryA("Shcore.dll"); | ||
if (shlib_module_ == nullptr) { | ||
return; | ||
} | ||
|
||
if (!AssignProcAddress(user32_module_, "GetDpiForWindow", | ||
get_dpi_for_window_)) { | ||
return; | ||
if (AssignProcAddress(user32_module_, "GetDpiForWindow", | ||
get_dpi_for_window_)) { | ||
dpi_for_window_supported_ = true; | ||
} | ||
|
||
if (!AssignProcAddress(user32_module_, "SetProcessDpiAwarenessContext", | ||
set_process_dpi_awareness_context_)) { | ||
return; | ||
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. SetProcessDpiAwarenessContext is the most recent version of the API.. any reason why you chose not to use this if available? 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. I just removed it since it's not being used anywhere, and we would want the dpi awareness to be set by the runner app, in the manifest anyway. Is there any reason to keep it? 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. OK, yes good point :-) |
||
if (AssignProcAddress(shlib_module_, "GetDpiForMonitor", | ||
get_dpi_for_monitor_) && | ||
AssignProcAddress(user32_module_, "MonitorFromWindow", | ||
monitor_from_window_)) { | ||
dpi_for_monitor_supported_ = true; | ||
} | ||
|
||
permonitorv2_supported_ = true; | ||
} | ||
|
||
Win32DpiHelper::~Win32DpiHelper() { | ||
if (user32_module_ != nullptr) { | ||
FreeLibrary(user32_module_); | ||
} | ||
} | ||
|
||
bool Win32DpiHelper::IsPerMonitorV2Available() { | ||
return permonitorv2_supported_; | ||
} | ||
|
||
BOOL Win32DpiHelper::EnableNonClientDpiScaling(HWND hwnd) { | ||
if (!permonitorv2_supported_) { | ||
return false; | ||
} | ||
return enable_non_client_dpi_scaling_(hwnd); | ||
} | ||
|
||
UINT Win32DpiHelper::GetDpiForWindow(HWND hwnd) { | ||
if (!permonitorv2_supported_) { | ||
return false; | ||
if (shlib_module_ != nullptr) { | ||
FreeLibrary(shlib_module_); | ||
} | ||
return get_dpi_for_window_(hwnd); | ||
} | ||
|
||
BOOL Win32DpiHelper::SetProcessDpiAwarenessContext( | ||
DPI_AWARENESS_CONTEXT context) { | ||
if (!permonitorv2_supported_) { | ||
return false; | ||
UINT Win32DpiHelper::GetDpi(HWND hwnd) { | ||
// GetDpiForWindow returns the DPI for any awareness mode. If not available, | ||
// fallback to a per monitor, system, or default DPI. | ||
if (dpi_for_window_supported_) { | ||
return get_dpi_for_window_(hwnd); | ||
} else if (dpi_for_monitor_supported_) { | ||
HMONITOR monitor = monitor_from_window_(hwnd, MONITOR_DEFAULTTONEAREST); | ||
UINT dpi_x = 0, dpi_y = 0; | ||
HRESULT result = | ||
get_dpi_for_monitor_(monitor, MDT_EFFECTIVE_DPI, &dpi_x, &dpi_y); | ||
if (result == S_OK) { | ||
franciscojma86 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return dpi_x; | ||
} | ||
} else { | ||
HDC hdc = GetDC(hwnd); | ||
UINT dpi = GetDeviceCaps(hdc, LOGPIXELSX); | ||
ReleaseDC(hwnd, hdc); | ||
return dpi; | ||
} | ||
return set_process_dpi_awareness_context_(context); | ||
return default_dpi_; | ||
} | ||
|
||
} // namespace flutter |
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
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.
Uh oh!
There was an error while loading. Please reload this page.