C # .NET.NET errors when hiding and replaying a maximized child form programmatically and at maximum magnification, the icon of the child form cannot be changed

I basically have two problems with C # .NET MDI. You can download the VS2010 solution that reproduces the errors here .

1) When programmatically hiding and re-playing the maximized child form, it does not maximize properly and does not become either maximized or normal.

childForm = new Form(); childForm.Text = "Child Form"; childForm.MdiParent = this; ... private void showButton_Click(object sender, EventArgs e) { childForm.Visible = true; } ... private void hideButton_Click(object sender, EventArgs e) { childForm.Visible = false; } 

When the child form is maximized, then programmatically hides and displayed again, it becomes something like this (note that the control window for the child form appears on the menu bar, but the child form is not maximized):

alt text

At this point, the child form cannot be moved. However, I found a workaround for this by simply showing and hiding the mannequin child form, which causes the actual child form to become properly maximized. But this makes the MDI area flicker. Tried the Invalidate, Refresh, Update methods, but they do not help. Perhaps there are other ways to work around this problem in order not to measure MDI areas with a dummy child form?

 private void workaround1Button_Click(object sender, EventArgs e) { dummyForm.Visible = true; dummyForm.Visible = false; } 

2) When the child form is maximized, the child form icon is displayed in the menu bar. However, if you need to change the icon when the child form is maximum, the icon in the menu bar is not updated (see Image above). I found a workaround for this too, which basically hides and shows the menu bar. The icon is updated, but it does everything below the flicker menu bar. Tried the Invalidate, Refresh, Update methods, but they do not help. Is there any other way to make the menu bar refresh the child form icon?

 private void workaround2Button_Click(object sender, EventArgs e) { menuStrip.Visible = false; menuStrip.Visible = true; } 

I also noticed that when the parent form is in the normal window state mode (not maximized) and you change the width or height of the form by 1 pixel, the child form becomes as possible as it should be, and the icon of the child form in the menu bar gets updated properly, and you don’t need another workaround as described above. If I resize the form programmatically, the form flickers by 1 pixel, and I cannot do this when the parent form is maximized. Is there any way in which I could call the repaint / refresh function, which is called when the form is resized and which makes the child form as possible as possible, and the icon in the menu bar is updated?

+8
c # mdi
source share
5 answers

Found a way to get around these errors.

First of all, you need to pause the painting for the form and its children. I found a very useful topic here that describes how to do this.

After pausing the drawing, you need to call the control's UpdateBounds method and increase the width or height of the ClientRectangle Width or Height by one, and then reduce it to the same value as before. This calls the layout functionality, which does everything for updating / redrawing. The last step is to turn on drawing. Not a good solution, I think, but it works.

 StopDrawing(); UpdateBounds(Location.X, Location.Y, Width, Height, ClientRectangle.Width, ClientRectangle.Height + 1); UpdateBounds(Location.X, Location.Y, Width, Height, ClientRectangle.Width, ClientRectangle.Height - 1); StartDrawing(); 

I believe that pausing drawing is very useful not only for working on these two errors, but also in general, to make the graphical interface smoother. I think this can help eliminate any flicker. However, P / Invokes are required for this solution, which should be avoided altogether.

+1
source share

Are you tired of using Hide / Show instead of setting the visible value to true / false?

Try:

 private void showButton_Click(object sender, EventArgs e) { childForm.Show(); } private void hideButton_Click(object sender, EventArgs e) { childForm.Hide(); } 
+1
source share

There is an error in the implementation of the inner class MdiControlStrip, a control that displays the min / max / restore glyphs and glyphs in the parent window. I have not described it yet, the code is not so simple. The classic side effect of a bug is that glyphs double, you have discovered some other side effects. The fix is ​​simple, delaying the creation of child windows until the constructor completes. Like this:

  public MainForm() { InitializeComponent(); } protected override void OnLoad(EventArgs e) { childForm = new Form(); childForm.Text = "Child Form"; childForm.MdiParent = this; dummyForm = new Form(); dummyForm.MdiParent = this; dummyForm.WindowState = FormWindowState.Maximized; base.OnLoad(e); } 
+1
source share

How about this workaround?

 private void showButton_Click(object sender, EventArgs e) { childForm.Visible = true; childForm.WindowState = (FormWindowState)childForm.Tag; } private void hideButton_Click(object sender, EventArgs e) { childForm.Visible = false; childForm.Tag = childForm.WindowState; childForm.WindowState = FormWindowState.Normal; } 

UPDATE

I just gave you an idea of ​​how you could do this. The best solution, using the same idea as above, would be a new basic form that preserves the state of windows. See below. Derive your forms from FixedForm instead of the form:

 public partial class FixedForm : Form { private FormWindowState lastWindowState; public FixedForm() { InitializeComponent(); } protected override void OnVisibleChanged(EventArgs e) { base.OnVisibleChanged(e); if (Visible) { WindowState = lastWindowState; } else { lastWindowState = WindowState; WindowState = FormWindowState.Normal; } } } 
+1
source share

Why not only manually reset the necessary icon in the menuStrip elements after creating the window:

 menuStripMain.Items[0].Image = null; 
0
source share

Source: https://habr.com/ru/post/650084/


All Articles