Maximizing child mdi in limited area - c#

I have a form which is a mdicontainer and has a menu strip at the top. I add a child form to my mdi container and when I maximize the child it maximizes over the menustrip. I want to know how to limit the child to maximize below the menustrip. Any help would be appreciated.

Your child form is being maximized in the way that child forms are supposed to be maximized in MDI. It's not really covering the menu strip of the parent form - it's actually merging its own menu strip with that of the parent form.
To make the child form take up only the available child area in the MDI parent (and not merge its menu with the parent's menu), put something like this code in the child form's Resize event:
if (this.WindowState == FormWindowState.Maximized)
{
this.WindowState = FormWindowState.Normal;
this.Size = this.MdiParent.ClientSize;
this.Location = new Point(0, 0);
}
which will prevent the child window from being actually maximized.
I say "something like this code" because this snippet doesn't work exactly right. The ClientSize property of the parent form gives the overall size of the form, whereas you want to use the size of just the MDI client area. I don't know how to get that, and apparently it's not super-easy. See this question:
Size/Location of Winforms MDI Client Area

You could set the MaximumSize property so that it doesn't fill up the entire container.

I know this an old question, but I just ran into this on an old project I'm working on, so here's an answer for anyone seeing this. Setting the Dock to DockStyle.Fill will give you the behaviour you want.
Just be aware that the window will act/look strange if you try to reposition or resize the window while it has that DockStyle.
To accomplish this, I subscribed to the MDI client window's resize event and if the window had just been maximized, I set its DockStyle to Fill, set the FormBorderStlye to FixedDialog(to prevent resizing), and set the window state to normal to prevent the maximization from occurring.
To prevent the user from moving the window while it is in this "maximized state" I simply overrode the WndProc method and handled when the window was being moved(SC_MOVE) and placed a return to prevent the action from taking effect.

Related

Do it yourself MDI

I have created an MDI application that I want to add a ToolStripContainer to. My first try was to just add one and dock it on Top. Problem I got here was moving ToolStrips inside the container's ToolStripPanels would create an extra Row which was invisible.
Then I saw this question and tried to implement my do-it-yourself MDI interface, using the ToolStripContainer's ContentPanel as where the windows go. This way, I can have my ToolStrips anywhere around the MDI.
The code I have is as follows:
Form f = new Form();
f.TopLevel = false;
tsContainer.ContentPanel.Controls.Add(f);
It all works remarkably well, but I am wondering if this is smart, would I not run into problems later on? Also, if this is possible, why have the entire MDI functionality in Windows?
By setting the TopLevel property to false, the form essentially turns into a UserControl. Do note that you have to explicitly set the Visible property to true, it isn't automatic.
But yes, there certainly are problems and it doesn't behave like an MDI child window at all. The biggest issue should be readily visible, the form can no longer be activated. Very noticeable from its caption bar, it will always be rendered with the colors for a non-active window. It however still works like a caption bar, the user can drag the window and move it around. Which has the clear failure mode that the window is going to get clipped without scrollbars. Not only that, the window can still be maximized and minimized by double-clicking the caption. An MDI child does this very differently. And the visual style for the frame will be wrong in Windows 8.x, looking like a Win7 frame instead, a quirk that has no known workaround.
Realistically, you have to set the FormBorderStyle property to None so it behaves more like a true child window. Or write a bunch of fairly nasty code in the WndProc() override to tame the window, you'll be doomed to re-invent an MDI child, imperfectly.
Which is otherwise a fine way to embed a form. The only remaining quirk after fixing the border is that the form still gets added to the Application.OpenForms collection.

Add White Layer on top of Main Window

I need to add a white layer on top of a Main form when a modal window appears on top of it so that it gives prominent to the modal window and kind of make the background transparent.
Add 75% Opaque white background for example...
Can it be done in Win forms?
Thanks!
The easiest way to do this might be to disable the main container control in your main form. That usually gives a faded look to everything in it.
You can set the parent form enabled property to false after you open the child form, which will make the parent form read only and darker, so it's obvious the parent form is not active.
form1.Enabed = false;

how to change the location of a minimized mdichild form?

In my MDI application i have changed the size of its MDI client to avoid the scrollbars that appear when a portion of an MDI child form is moved out of the view of the client area of the MDI parent form (i made the MDI client size bigger than the size of the screen).
I know i can use an API using ShowScrollBar to hide these scrollBars but it flickers and i prefere not to use an API.
Now i have a problem that when minimizing any of the MDI child Forms its location is set by default to the bottom of the MDI client (which its size is bigger than the size of the screen) so the minimized MDI child form doesn't appear.
So how can i change the location of a minimized mdichild form?
Thanks in advance.
Try to read the ClientRectangle from the parent and apply the location to the child accordingly before minimizing. I think you ca implement the Form Minimized or minimizing events.

How can I keep an MDI child window from becoming "trapped" underneath a toolbar?

I have an MDI application with child windows. It's possible for the user to move the child window's title bar up high enough so that it is placed underneath the toolbar for the application, and the control box for the window is obscured, making it impossible to close. There is sometimes so little room to "grab" the window and pull it back down that the user has to restart the application altogether. How can I limit the positioning of an MDI child window so that it stops when it bumps up against the bottom of the MDI parent's toolbar?
If that's too confusing, here's the simplified version: how can I constrain the movement of an MDI child form to a certain portion of the screen or parent form?
Register to the MDI child's Move event and make sure that the Top is greater than 0.
By the way, when I move a MDI child too high, I get a scrollbar letting me scroll up.

MDI Child Form Start Position Problem

I am having this strange problem with the start position of the MDI child Form.
Here is my code for the start position, I wanted it to be the middle left of
the MDI Parent Form.
//show the task bar
ChildForm c1 = new ChildForm();
c1.MdiParent = this;
c1.StartPosition = FormStartPosition.Manual;
c1.Location = new Point(0, this.Height/2);
Strange thing is every time I start my Mdi parent form, the vertical scrollbar show up
and the mdi child form is position lower than half of the MDI parent form's height.
Then if I scroll the scroll bar down to see the whole mdi child form, the scrollbar disappear.
After this point, no matter how I change the size of the mdi parent form, the scrollbar will
not show up again.
Does Anybody knows Why?
Also, I was wondering if it is possible to hide a mdi child form with only a handle-like control left
in the mdi parent form. If the user click that handle, then the form show up again. If the user
click somewhere else, the form hide. It is kind of like Window taskbar.
I tried to use form.hide(), but it hides the whole mdi child form.
Thanks
Alex
Is is possible that your calculation is taking into account the windows bar at the top.
You can have a look at PointToClient or PointToScreen
you sholud minuse child form height and width like this
c1.Location = new Point(0, (this.Height - c1.Height)/2);

Categories

Resources