-
Notifications
You must be signed in to change notification settings - Fork 1k
Improve Message Box in WinForms #13440
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
base: main
Are you sure you want to change the base?
Conversation
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #13440 +/- ##
===================================================
+ Coverage 75.43847% 76.58368% +1.14521%
===================================================
Files 3230 3230
Lines 639097 639238 +141
Branches 47289 47308 +19
===================================================
+ Hits 482125 489552 +7427
+ Misses 147952 146112 -1840
+ Partials 9020 3574 -5446
Flags with carried forward coverage won't be shown. Click here to find out more. 🚀 New features to boost your workflow:
|
src/System.Windows.Forms/System/Windows/Forms/Dialogs/MessageBox.cs
Outdated
Show resolved
Hide resolved
src/System.Windows.Forms/System/Windows/Forms/Dialogs/MessageBox.cs
Outdated
Show resolved
Hide resolved
src/System.Windows.Forms/System/Windows/Forms/Dialogs/MessageBox.cs
Outdated
Show resolved
Hide resolved
src/System.Windows.Forms/System/Windows/Forms/Dialogs/MessageBox.cs
Outdated
Show resolved
Hide resolved
src/System.Windows.Forms/System/Windows/Forms/Dialogs/MessageBox.cs
Outdated
Show resolved
Hide resolved
src/System.Windows.Forms/System/Windows/Forms/Dialogs/MessageBox.cs
Outdated
Show resolved
Hide resolved
src/System.Windows.Forms/System/Windows/Forms/Dialogs/MessageBox.cs
Outdated
Show resolved
Hide resolved
src/System.Windows.Forms/System/Windows/Forms/Dialogs/MessageBox.cs
Outdated
Show resolved
Hide resolved
src/System.Windows.Forms/System/Windows/Forms/Dialogs/MessageBox.cs
Outdated
Show resolved
Hide resolved
src/System.Windows.Forms/System/Windows/Forms/Dialogs/MessageBox.cs
Outdated
Show resolved
Hide resolved
src/System.Windows.Forms/System/Windows/Forms/Dialogs/MessageBox.cs
Outdated
Show resolved
Hide resolved
src/System.Windows.Forms/System/Windows/Forms/Dialogs/MessageBox.cs
Outdated
Show resolved
Hide resolved
src/System.Windows.Forms/System/Windows/Forms/Dialogs/MessageBox.cs
Outdated
Show resolved
Hide resolved
src/System.Windows.Forms/System/Windows/Forms/Dialogs/MessageBox.cs
Outdated
Show resolved
Hide resolved
src/System.Windows.Forms/System/Windows/Forms/Dialogs/MessageBox.cs
Outdated
Show resolved
Hide resolved
…hen DarkMode is enabeld
Hey guys, so, I am a bit confused. So, it's not exactly clear to me, what we want to do here. But we will also not have an additional MessageBox, which would do essentially do the same as the old one. We said that we want the TaskDialog to move forward, and I am looking actively at what options we might have, to have that one adapted to dark mode. Yes, it's not ideal that we do not have dark mode support for the native MessageBox-es as it is. The only way to get the old MessageBox display acceptable in dark mode would probably be, to completely reimplement it in WinForms based on what we're actually supporting in terms of dark mode support. That's something I had considered, and I have actually a prototype for testing, and would bring TO a team meeting if time allows during the Preview 6-time frame. But even that is not really likely to make the bar. We need to discuss internally at the next team meeting, and I will follow up, but I do not see a good chance this being taken. Klaus |
I think installing a hook and uninstalling the hook inside of
Are both beyond the scope of MessageBox. However, hooking into it's WndProc to override it into dark mode should be alright as long as the person who opened this PR makes it to where that is not part of the public API surface (to ensure existing code does not break) while also allowing dark mode out-of-box when the developer enables dark mode in their application. The reason for this is that I see a way that this can be done as an implementation detail without exposing any new public API's. |
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.
A few changes I feel can be done. Other than that LGTM. Alternatively the hook step for MessageBox
could be split into a separate MessageBox.WndHook.cs
file and the hook part from ShowCore
into a new method named HookMessageBoxPInvoke
basically calling PInvoke.MessageBox
right after it hooks the messagebox and inside of it's own try
block, and then in the finally
block have it unhook it. And then in ShowCore
have it call HookMessageBoxPInvoke
with no need to cast the result to DialogResult
as the HookMessageBoxPInvoke
method would cast and return the DialogResult
directly.
A benefit to this is then the hook stuff would go into it's own individual file for easier maintainability, but at the cost of making the MessageBox
class partial
.
#pragma warning disable WFO5001 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed. | ||
if (Application.IsDarkModeEnabled) | ||
{ | ||
InstallHook(); |
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.
Alternatively InstallHook
could check if if (!Application.IsDarkModeEnabled)
and instantly return
if dark mode is not enabled to have minimal changes to this method. Same can be done inside of UninstallHook
as well.
|
||
private static void UninstallHook() | ||
{ | ||
lock (s_lock) |
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.
As said below add in this:
lock (s_lock) | |
if (!Application.IsDarkModeEnabled) | |
{ | |
return; | |
} | |
lock (s_lock) |
|
||
private static unsafe void InstallHook() | ||
{ | ||
lock (s_lock) |
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.
As said below add in this:
lock (s_lock) | |
if (!Application.IsDarkModeEnabled) | |
{ | |
return; | |
} | |
lock (s_lock) |
if (Application.IsDarkModeEnabled) | ||
{ | ||
InstallHook(); | ||
} |
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.
if (Application.IsDarkModeEnabled) | |
{ | |
InstallHook(); | |
} | |
InstallHook(); |
if (Application.IsDarkModeEnabled) | ||
{ | ||
UninstallHook(); | ||
} |
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.
if (Application.IsDarkModeEnabled) | |
{ | |
UninstallHook(); | |
} | |
UninstallHook(); |
@@ -149,6 +150,7 @@ GetCurrentThread | |||
GetCursor | |||
GetCursorPos | |||
GetDIBits | |||
GetDlgCtrlID |
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.
GetDlgCtrlID |
This can be removed as it looks like it is not used.
Fixes #12044
Proposed changes
Screenshots
Before
After
Test methodology
Manually tested via scratch project.
Microsoft Reviewers: Open in CodeFlow