Q: How can I catch an event of minimize or maximize button pressin

G

Guest

hi,

Now, I'm creating window based applicaton.
In this application, I want to put some traps(code) before form minimizing
or maximizing event, but I only can get events after form minimized or
maximized.

I tried to test these 2 codes below:

1.
Form f = new Form();
f.SizeChanged += new EventHandler(SizeChangedDelegate);

Result of this code is MessageBox showed after form minimized or maximized.


2.
Form f = new Form();
f.ResizeBegin += new EventHandler(SizeChangedDelegate);
f.Resize += new EventHandler(SizeChangedDelegate);
f.ResizeEnd += new EventHandler(SizeChangedDelegate);

Result of this code is
a) ResizeBegin event did not handled while minimizing or maximizing.
b) Resize event handled after minimizing or maximizing.
c) ResizeEnd event did not handled while minimizing or maximizing.

NOTICE: delegate method of SizeChangedDelegate is like below for sample:
void SizeChangedDelegate(object sender, EventArgs args) {
MessageBox.Show("Handling AdditionalDelegateOfSizeChanged");
}

Now, I do not know how to solve this, and also do not know possible or not.
I cannot find the answer in MSDN documentation.

Would someone help me, please?

Thanks,

Hide
 
N

Nicholas Paldino [.NET/C# MVP]

Hide,

In order to do this, you will have to override the WndProc method on
your Form class. In it, you would check the Msg property of the Message
parameter m passed into the method. If that value is 0x46, which
corresponds to the WM_WINDOWPOSCHANGING message, then the form is being
maximized or minimized.

At that point, you will have to marshal the WINDOWPOS structure (it
should be defined on http://www.pinvoke.net) from the pointer in the lParam
property on the Message structure. Then, based on the cx and cy parameters,
you can tell if the window is maximizing or minimizing.

Make sure when you are done you call the base implementation of WndProc.

Unfortunately, there no more specific way (AFAIK) to indicate when a
window is minimizing or maximizing.

Hope this helps.
 
N

Nicholas Paldino [.NET/C# MVP]

Hide,

In addition to what I said, if you are only interested in capturing when
the min/max button is pressed (or selected from the system menu), then you
can capture the WM_SYSCOMMAND message in the WndProc override, looking for
the SC_MINIMIZE or SC_MAXIMIZE commands. However, these are not going to be
passed to you when the WindowState property on the Form class is set to
Maximized.
 

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