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.
(Yes, I know this is not clear
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 workaround in my application.
However, I've run into a strange problem:
My application has several buttons on the main form (the MDI parent)
that are positioned over a toolbar strip, but are NORMAL buttons, NOT
toolbar buttons. (the guy who wrote the original program didn't like the way
the toolbar buttons looked, so he did it this way).
These buttons can open the form, change it's 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
appended example).
this is very strange!
I've reproduced the problem in a small application (the application is
appended to the end of the post)
If anybody have any ideas why this happens or how to fix it, please let me
know.
Thanks,
Nadav
PS, I can fix this problem by changing the buttons to toolbar buttons,
but this would change the way the application looks (which I would rather
not do)
and anyway I'm really curious about this strange bug.
=============== 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();
}
}
}
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.
(Yes, I know this is not clear
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 workaround in my application.
However, I've run into a strange problem:
My application has several buttons on the main form (the MDI parent)
that are positioned over a toolbar strip, but are NORMAL buttons, NOT
toolbar buttons. (the guy who wrote the original program didn't like the way
the toolbar buttons looked, so he did it this way).
These buttons can open the form, change it's 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
appended example).
this is very strange!
I've reproduced the problem in a small application (the application is
appended to the end of the post)
If anybody have any ideas why this happens or how to fix it, please let me
know.
Thanks,
Nadav
PS, I can fix this problem by changing the buttons to toolbar buttons,
but this would change the way the application looks (which I would rather
not do)
and anyway I'm really curious about this strange bug.
=============== 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();
}
}
}