I have a form class called MDIParent
that has IsMdiContainer = true
.
The form load handler enumerates this.Controls
to find the MdiClient
container window and assign it to the clientWindow
class variable. The handler also disables the AutoScroll
property in an attempt to preserve the scroll bars once I manually update them with this code:
// Some abstraction of API constants SB_HORZ, SB_VERT, SB_BOTH.
public enum ScrollBarOptions
{
AffectHorizontal = 0,
AffectVertical = 1,
AffectBoth = 3
}
[DllImport("user32.dll")]
private static extern int ShowScrollBar(IntPtr hWnd, int wBar, int bShow);
/// <summary>
/// Set scroll bar visiblity on the MdiClient.
/// </summary>
private void ShowScrollBar(bool visible, ScrollBarOptions options)
{
int show = (int)(visible ? 1 : 0);
if (this.clientWindow == null) {
throw new InvalidStateException("Could not determine the MdiClient.");
}
// Apply the changes.
ShowScrollBar(clientWindow.Handle, Convert.ToInt32(options), show);
}
/// <summary>
/// Update the MdiClient scroll-bars' visibility based on
/// whether active window dimensions exceed MdiClient dimensions.
/// </summary>
private void UpdateScrollBars(System.Object sender, EventArgs e)
{
// Exit if no child form is active.
// Calling ShowScrollBar() with null clientWindow will yield an exception.
if ((this.ActiveMdiChild == null) || (this.clientWindow == null)) {
return;
}
// If auto scroll is enabled, then throw exception; cannot update
// while the MDI container is actively managing the scroll bars.
if (this.AutoScroll) {
throw new InvalidStateException("Cannot update vertical/horizontal scroll states while AutoScroll is true.");
}
// Set state of horizontal/vertical scroll-bars.
ShowScrollBar(Convert.ToBoolean(this.ActiveMdiChild.MinimumSize.Width > clientWindow.Width), ScrollBarOptions.AffectHorizontal);
ShowScrollBar(Convert.ToBoolean(this.ActiveMdiChild.MinimumSize.Height > clientWindow.Height), ScrollBarOptions.AffectVertical);
}
My problem is that the scroll-bars are correctly rendered within the MdiClient
container, but disappear as soon as the user uses them to scroll.
How can I prevent this behavior?