MDI child, Overriden WndProc function and hidden child form

S

sshuangw

Hello:

I am encountering a very weird issue with MDI child, Overriden WndProc
function and hidden form.

Basically, the application has two forms, Form1(parent form),
Form2(Child form), Form2's WndProc method is overriden, if the message
is CLOSE message, just hide the form. I first initialize and show the
child form, then close it by clicking the Close(X) button, the
overriden WndProc method gets invoked, and the form is hidden. Then
from Form1(parent form), I explcitly call Form2.Close(), this time, the
overriden WndProc method is not called, instead, the flow goes to
Form2.Dispose() methid directly and clear all the resource. However, I
am expecting the overriden WndProc method should be invoked as well
when calling Form2.Close().

Interesting observation is that if I set form2.Visible = true before
calling Form2.Close(), this way, the overriden ProdWnc process would be
invoked.

COuld anyone shed a light on this issue? I am so frustrated with this
one.

Thanks a lot.

Below is the sample code for Form1 and Form2.


//
// FORM2
//

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Threading;

namespace WindowsApplication1
{
/// <summary>
/// Summary description for Form2.
/// </summary>
public class Form2 : System.Windows.Forms.Form
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;

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

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

protected override void WndProc(ref Message m_)
{
if (m_.Msg == Win32.WM_CLOSE) // 0x0010
{
this.Hide();
}
else
{
base.WndProc(ref m_);
}
}

/// <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.components = new System.ComponentModel.Container();
this.Size = new System.Drawing.Size(300,300);
this.Text = "Form2";
// this.Closing += new
System.ComponentModel.CancelEventHandler(this.Form_Closing);
}
#endregion

}
}


//
// FORM1
//
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Diagnostics;
using System.Windows.Forms;

namespace WindowsApplication1
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
private System.Windows.Forms.Button button2;
private Form2 frm2;

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.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(128, 80);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(136, 56);
this.button1.TabIndex = 0;
this.button1.Text = "CloseForm2";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(120, 184);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(144, 40);
this.button2.TabIndex = 1;
this.button2.Text = "Initialize";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 266);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);

}
#endregion

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

// protected override void WndProc(ref Message m_)
// {
// if (m_.Msg == Win32.WM_CLOSE) // 0x0010
// {
// this.Visible = false;
// MessageBox.Show("I am here");
// }
// else
// {
// base.WndProc(ref m_);
// }
// }

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

private void Form1_Load(object sender, System.EventArgs e)
{
this.IsMdiContainer = true;
}

private void button2_Click(object sender, System.EventArgs e)
{
if(frm2 == null)
{
frm2 = new Form2();
}

frm2.Text = "TestChild";
frm2.MdiParent = this;

frm2.Show();
frm2.Activate();
frm2.BringToFront();

}

}
}
 

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