Form Message Question

S

Scott Abel

Hello,

I am trying to create a new form class that has a new property
LastWindowState. I need this in my MDI app because I have a non-modal child
form that I only want opened once. If the user selects the option from the
menu I want to restore the form to its last known state, if it is minimized.
To do that I need a new property etc. Anyway, I have been able to capture
the min/max/restore by overriding the WndProc() method and looking for
message WM_SYSCOMMAND and looking at the value for WParam to see if it is
SC_MINIMIZE, SC_MAXIMIXE, or SC_RESTORE. All works ok. Here is the issue
though.

When other programmers use this class they might use the WindowState
property to change the window state. The WndProc() does not capture those
messages and the LastWindowState is left in a confused state :)

What is the message, wparam, and lParam I could use in WndProc() to grab the
current window state before passing to the message on to base.WndProc()?

I have tried overriding the WindowState with the following code but got an
error when compiling saying:
cannot override inherited member 'System.Windows.Forms.Form.WindowState.get'
because it is not marked virtual, abstract, or override

public override FormWindowState WindowState
{
get
{
WindowState = base.WindowState;
}
set
{
MessageBox.Show("Current Window State = " +
this.WindowState);
base.WindowState = value;
}
}

I have tried using protected keyword but I get a different message saying I
can't change access modifiers when overriding a public member.

Any Ideas?
Thanks
Scott
 
C

Claes Bergefall

Looks like the window styles are affected
Handle WM_STYLECHANGED and check the styles
WS_MAXIMIZE and WS_MINIMIZE

/claes
 
S

Scott Abel

Never mind. I was trying to create a workaround for the following on an MDI
child form:
1. click menu in parent to open form (if form is open and minimized
restore it)
2. MAX form
3. MIN form
4. click menu in parent to open form (form is open and minimized so
restore it w/WindowState)
5. Form is now maximized (last state/size i think)
6. Click the forms restore button in the title bar.
7. Form is restored to normal but sized as a minimized from.

If you used the buttons in the title bar it worked fine, so I decided that I
could just send the form a message to restore itself. I hope that helps
others who may have run into this also.

Scott

using System.Runtime.InteropServices;

//
// 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;

//
// This is the menu click event to display the form.
//
//
private void mnuReportsContinShipment_Click(object sender,
System.EventArgs e)
{
//
// If it is already loaded, set it, otherwise set it to null
// so we can load it next.
//
FContinShipmentRpt f = (FContinShipmentRpt)
GOBI_Test.ChildFormIsOpen(this, "FContinShipmentRpt");

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

else
{
if (f.WindowState == FormWindowState.Minimized)
{
//
// It it has been loaded and is minimized then send
message to restore it.
//
// I couldn't just use f.WindowState =
FormWindowState.Normal because if
// you loaded the form, then MAX the form, then MIN the
form and them click
// this menu again the form would appear as maximized
but if you clicked the
// form restore button in the title bar it would
restore the form to the same
// size as a minimized form.
//
SendMessage(f.Handle,WM_SYSCOMMAND, SC_RESTORE, 0);
}
f.Activate();
}

}
 

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