problems with restoring minimized MDI child form

G

Guest

Hi Everybody,
I've got a strange problem:

While searching the newsgroups for something else,
I've come across a post that discusses a bug with restoring MDI child forms.
(for any body who is interested, the post is in
microsoft.public.dotnet.framework.windowsforms
by Scott Abel, the title is 'Form Message Question', from 2003-08-12
09:43:42 PST )

Briefly, the problem is that if you use Form.WindowStyle to restore a
minimized MDI child that was maximized when it was minimized, the form will
be restored as maximized (as it should) but then when you click on the
restore button of the from it will be restored to the minimized size.
That post also included a workaround for this bug (to use SendMessage to
send a SC_RESTORE message to the form instead of using Form.WindowState).

I have implemented this in my application.
However, I've run into a strange problem:
I have several child forms in my application.
Some are opened using a menu, and some have buttons in a toolbar to open them.
For some reason, this work around works when the code is called from a menu,
but does NOT work when the code is called from the event handler of a button
in the toolbar.
I even created a menu item that calls the event handler of the toolbar button.
When clicking on the menu item the form is restored, when clicking on the
button it is NOT restored.

Later on, I will try to reproduce this in a small application and will post
the code.

If anybody have any ideas why this happens or how to fix it, please let me
know.

Thanks
Nadav
 
G

Guest

OK, I've reproduced the problem in a small application (the application is
appended to the end of the post) and then took a closer look at the original
application.

The problem is NOT with a toolbar button, as I originally thought, it's with
a button that is a child of the main form (the MDI container).
this button can open the form, change it WindowState (if you uncomment that
statement) , but it seems like the SendMessage() function does not work
properly if called from the click event handler of that Button.
the SendMessage() does work if it is called from the event handler of the
menu or from the click event handle of a toolbar button (NOT included in the
appened example).

this is very strange.

Nadav

=============== appended code =============


using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Runtime.InteropServices;

namespace test_MDI_bug
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.MainMenu mainMenu1;
private System.Windows.Forms.MenuItem menuItem1;
private System.Windows.Forms.MenuItem menuItem2;
private System.Windows.Forms.Button button1;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;

public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

//
// TODO: Add any constructor code after InitializeComponent call
//
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.mainMenu1 = new System.Windows.Forms.MainMenu();
this.menuItem1 = new System.Windows.Forms.MenuItem();
this.menuItem2 = new System.Windows.Forms.MenuItem();
this.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// mainMenu1
//
this.mainMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuItem1});
//
// menuItem1
//
this.menuItem1.Index = 0;
this.menuItem1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuItem2});
this.menuItem1.Text = "test";
//
// menuItem2
//
this.menuItem2.Index = 0;
this.menuItem2.Text = "test1";
this.menuItem2.Click += new System.EventHandler(this.menuItem2_Click);
//
// button1
//
this.button1.Location = new System.Drawing.Point(48, 8);
this.button1.Name = "button1";
this.button1.TabIndex = 2;
this.button1.Text = "button1";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 272);
this.Controls.Add(this.button1);
this.IsMdiContainer = true;
this.Menu = this.mainMenu1;
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);

}
#endregion

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}


//
// In MDI Parent form class declaration
//
[DllImport("User32")]
private static extern bool SendMessage(IntPtr hWnd, int msg, int
wParam, int lParam);

private const int SC_RESTORE = 0xF120;
private const int WM_SYSCOMMAND = 0x0112;

Form f=null;
//
// This is the menu click event to display the form.
//
//
private void Restore() {
//
// If it is already loaded, set it, otherwise set it to null
// so we can load it next.
//

if (f == null) {
f = new Form();
f.MdiParent = this;
f.Show();
}

else {
if (f.WindowState == FormWindowState.Minimized) {
SendMessage(f.Handle,WM_SYSCOMMAND, SC_RESTORE, 0);
//f.WindowState=FormWindowState.Normal;
}
f.Activate();
}

}

private void menuItem2_Click(object sender, System.EventArgs e) {
Restore();
}

private void toolBar1_ButtonClick(object sender,
System.Windows.Forms.ToolBarButtonClickEventArgs e) {
Restore();
}

private void button1_Click(object sender, System.EventArgs e) {
Restore();
}
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top