Skip to content

fix MDI forms lost sub control box - #3461

Merged
RussKie merged 2 commits into
dotnet:masterfrom
RussKie:fix_3029_mdichild_control_box
Jan 14, 2021
Merged

RussKie merged 2 commits into
dotnet:masterfrom
RussKie:fix_3029_mdichild_control_box

Conversation

@RussKie

@RussKie RussKie commented Jun 17, 2020 •

Copy link
Copy Markdown
Contributor

Fixes #3029

Proposed changes

With the deprecation and removal of MainMenu control in #2091 one of MDI scenarios got broken where both parent and child forms don't have MainMenuStrip property set nor have MainMenuStrip instance added to their respective Controls collection.

The fix restores setting an MDI menu via WM_MDISETMENU message to the parent form.

Customer Impact

Customers who use MDI scenarios without main menu in both parent or child forms won't be able to minimise maximised child forms

net472 netcoreapp3.1
image image

Regression?

  • Yes

Risk

  • Minimal.

Screenshots

👉 👉 👉 Click for lots of pretty pictures... 🖼️ 🖼️ 🖼️
Parent Child Screenshot Screenshot
- - image
image
image
image
image
image
Controls.Add(menuStrip), MainMenuStrip = null - image
image
image
image
image
image
Controls.Add(menuStrip), MainMenuStrip = menuStrip - image
image
image
image
image
image
- Controls.Add(menuStrip), MainMenuStrip = null image
image
image
image
image
image
- Controls.Add(menuStrip), MainMenuStrip = menuStrip image
image
image
image
image
image
Controls.Add(menuStrip), MainMenuStrip = null Controls.Add(menuStrip), MainMenuStrip = null/menuStrip image
image
image
image
image
image
Controls.Add(menuStrip), MainMenuStrip = menuStrip Controls.Add(menuStrip), MainMenuStrip = null image
image
image
image
image
image
Controls.Add(menuStrip), MainMenuStrip = menuStrip Controls.Add(menuStrip), MainMenuStrip = menuStrip image
image
image
image
image
image

Test methodology

  • Manual comparison of net472 vs netcoreapp3.1 vs net5.0 (current commit)
Microsoft Reviewers: Open in CodeFlow

@RussKie
RussKie requested a review from a team as a code owner June 17, 2020 07:57
@ghost ghost assigned RussKie Jun 17, 2020
@RussKie

This comment has been minimized.

@JeremyKuhne JeremyKuhne left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

🕐

@ghost ghost added waiting-author-feedback The team requires more information from the author and removed waiting-author-feedback The team requires more information from the author labels Jun 17, 2020

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Can this be removed?

@ghost ghost added the waiting-author-feedback The team requires more information from the author label Jun 18, 2020
JeremyKuhne
JeremyKuhne previously approved these changes Jun 18, 2020

@JeremyKuhne JeremyKuhne left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Clean up the commented out code in the tests, otherwise looks good.

@RussKie
RussKie force-pushed the fix_3029_mdichild_control_box branch from 2eb095c to c9523a4 Compare June 18, 2020 05:31
@ghost ghost removed the waiting-author-feedback The team requires more information from the author label Jun 18, 2020
@codecov

codecov Bot commented Jun 18, 2020

Copy link
Copy Markdown

Codecov Report

Merging #3461 into master will increase coverage by 31.91478%.
The diff coverage is n/a.

@@                 Coverage Diff                  @@
##              master       #3461          +/-   ##
====================================================
+ Coverage   66.58268%   98.49746%   +31.91478%     
====================================================
  Files           1338         443         -895     
  Lines         501686      248046      -253640     
  Branches       40864        4088       -36776     
====================================================
- Hits          334036      244319       -89717     
+ Misses        162104        3015      -159089     
+ Partials        5546         712        -4834     
Flag Coverage Δ
#Debug 98.49746% <ø> (+31.91478%) ⬆️
#production ?
#test 98.49746% <ø> (+0.00011%) ⬆️

@merriemcgaw merriemcgaw left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

More work needed. I'd recommend optimizing for the case of both parent and child having menus that need to merge smoothly.

@ghost ghost added the waiting-author-feedback The team requires more information from the author label Jun 18, 2020
@RussKie
RussKie marked this pull request as draft August 4, 2020 00:22
@ghost ghost removed the waiting-author-feedback The team requires more information from the author label Aug 4, 2020
@RussKie RussKie added this to the 6.0 milestone Dec 18, 2020
@RussKie
RussKie force-pushed the fix_3029_mdichild_control_box branch from c9523a4 to 8557999 Compare December 22, 2020 05:29
@RussKie

RussKie commented Dec 22, 2020

Copy link
Copy Markdown
Contributor Author

@dotnet dotnet deleted a comment from codecov Bot Dec 22, 2020
@codecov

codecov Bot commented Dec 22, 2020 •

Copy link
Copy Markdown

Codecov Report

Merging #3461 (8d9b1e6) into master (8db1903) will not change coverage.
The diff coverage is n/a.

@@              Coverage Diff              @@
##              master       #3461   +/-   ##
=============================================
  Coverage   97.96398%   97.96398%           
=============================================
  Files            538         538           
  Lines         262669      262669           
  Branches        4912        4912           
=============================================
  Hits          257321      257321           
  Misses          4468        4468           
  Partials         880         880           
Flag Coverage Δ
Debug 97.96398% <ø> (ø)
production 100.00000% <ø> (?)
test 97.96398% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

@weltkante

Copy link
Copy Markdown
Contributor

See issue discussion of #3029, the code before the WM_MDISETMENU regression had a few code paths through (a removed) InvalidateMergedMenu which mostly mostly did nothing when there was no classic menu, but indirectly triggered another call of WM_MDISETMENU which no longer happens. Without these additional refreshes there are duplicate menu items that get fixed by restoring the refreshing logic. I've been playing around here with restoring missing code paths but didn't have time for testing it with this branch yet. Just a FYI if you want to go ahead before I get around to it.

@RussKie

This comment has been minimized.

@weltkante

Copy link
Copy Markdown
Contributor

The parent's menu doesn't want to amalgamate into the child's controlbox.

But it does on .NET Core 3.1 ? Or are you using different code there ?

@RussKie

This comment has been minimized.

@RussKie

RussKie commented Jan 7, 2021

Copy link
Copy Markdown
Contributor Author

Today I went all the way back to .NET Core 3.0 branch, and started cleaning from scratch. I managed to rid the Form off dependencies on the Menu and the MainMenu, and it is still appears to be working as expected 🎉
Here's the WIP branch: release/3.0...RussKie:Investigate_3.0

Now I need to project the same change on to the master branch, and see what we've missed.

@RussKie

RussKie commented Jan 8, 2021

Copy link
Copy Markdown
Contributor Author

I copied the change from release/3.0...RussKie:Investigate_3.0 on to the master branch and..... it didn't work as I expected. The icons are still missing.
So either I missed something while porting, or the issue is somewhere else. Continuing on.

@RussKie
RussKie force-pushed the fix_3029_mdichild_control_box branch 2 times, most recently from 85348a5 to dd99bb7 Compare January 11, 2021 23:23
@RussKie
RussKie marked this pull request as ready for review January 11, 2021 23:25
@RussKie RussKie added waiting-for-testing The PR is awaiting manual testing by the primary team; no action is yet required from the author(s) and removed waiting-for-testing The PR is awaiting manual testing by the primary team; no action is yet required from the author(s) labels Jan 12, 2021
@RussKie

RussKie commented Jan 12, 2021

Copy link
Copy Markdown
Contributor Author

CTI confirmed the fix

merriemcgaw
merriemcgaw previously approved these changes Jan 12, 2021

@weltkante weltkante left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Besides some whitespace issues I'm missing the refreshes I identified here as having been removed incorrectly in the initial cleanup PR.

In particular the MdiParentInternal setter and SetVisibleCore would reapply WM_MDISETMENU on the form. This refresh from MDI child to parent is missing otherwise (i.e. when the child is made visible or attached at a later point the parent needs to refresh its menu)

In my local tests there were scenarios where these refreshes were necessary to correctly show the menus as they used to before. Does the test code at the end of my branch work correctly for you? On Desktop Framework it will show the MDI menu bar, on .NET Core it is missing because the MDI child is loaded after the parent and no refresh happens.

@RussKie
RussKie force-pushed the fix_3029_mdichild_control_box branch 2 times, most recently from 41e9574 to 718ea61 Compare January 13, 2021 00:26
@RussKie

RussKie commented Jan 13, 2021

Copy link
Copy Markdown
Contributor Author

Thank you @weltkante, I'm going to retest it again. In my previous tests InvalidateMergedMenu led to multiple calls to UpdateMenuHandles, but didn't observe anything else.

@RussKie

RussKie commented Jan 13, 2021

Copy link
Copy Markdown
Contributor Author

In my local tests there were scenarios where these refreshes were necessary to correctly show the menus as they used to before. Does the test code at the end of my branch work correctly for you? On Desktop Framework it will show the MDI menu bar, on .NET Core it is missing because the MDI child is loaded after the parent and no refresh happens.

Running the test app on the latest master with the comparable change to your sample

diff --git a/src/System.Windows.Forms/tests/IntegrationTests/WinformsControlsTest/MdiParent.cs b/src/System.Windows.Forms/tests/IntegrationTests/WinformsControlsTest/MdiParent.cs
index 63c1cc57a..4dd8f550f 100644
--- a/src/System.Windows.Forms/tests/IntegrationTests/WinformsControlsTest/MdiParent.cs
+++ b/src/System.Windows.Forms/tests/IntegrationTests/WinformsControlsTest/MdiParent.cs
@@ -30,6 +39,7 @@ namespace WinformsControlsTest
 
             MdiChild frm = new MdiChild();
             frm.MdiParent = this;
+            frm.WindowState = FormWindowState.Maximized;
             frm.Show();
         }
     }

produces this:
image

Adding the missing plumbing fixes the issue:
image

Though instead of this:
image

I was able to achieve it with a smaller change:

+        /// <summary>
+        ///  Invalidates the merged menu, forcing the menu to be recreated if
+        ///  needed again.
+        /// </summary>
+        private void InvalidateMergedMenu() => ParentForm?.UpdateMenuHandles();
+

In your version we invoke MenuChanged on a parent, then the code within MenuChanged is looking for a parent of the original parent, and I couldn't get it to execute. The only thing that matter, it appears, is the call to UpdateMenuHandles.
Is there some other use case that I'm overlooking?

@RussKie
RussKie requested a review from dreddy-work January 13, 2021 01:46
weltkante
weltkante previously approved these changes Jan 13, 2021

@weltkante weltkante left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

In your version we invoke MenuChanged on a parent, then the code within MenuChanged is looking for a parent of the original parent, and I couldn't get it to execute. The only thing that matter, it appears, is the call to UpdateMenuHandles.
Is there some other use case that I'm overlooking?

Thats just a straight port of the pre-PR version, had to remove lots of redundant stuff and might have missed this. No I can't think of any purpose that branch may serve. On Desktop Framework there are more HMENU-related callers and it may be used by those.

PR looks good to me.

@RussKie

RussKie commented Jan 13, 2021

Copy link
Copy Markdown
Contributor Author

Thank you!

With the deprecation and removal of `MainMenu` control in dotnet#2091 one of MDI
scenarios got broken where both parent and child forms don't have `MainMenuStrip`
property set nor have `MainMenuStrip` instance added to their respective
`Controls` collection.

The fix restores setting an MDI menu via `WM_MDISETMENU` message to the
parent form.

Fixes dotnet#3029
@RussKie
RussKie force-pushed the fix_3029_mdichild_control_box branch from a1d3703 to 8d9b1e6 Compare January 13, 2021 22:42
@RussKie
RussKie merged commit 2979aad into dotnet:master Jan 14, 2021
@RussKie
RussKie deleted the fix_3029_mdichild_control_box branch January 14, 2021 01:44
@ghost ghost modified the milestones: 6.0, 6.0 Preview1 Jan 14, 2021
@ghost ghost locked as resolved and limited conversation to collaborators Jan 31, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

MDI forms lost sub control box

4 participants