Reputation: 1272
I'm looking for an event from a form that called after a window is maximized or minimized.
As far as I know there is such event as SizeChanged or WndProc that can handle maximized window, but it's called right away after user try to maximize the window, and it's not called after the window is fully maximized.
I'm looking for event like ResizeEnd, but maybe this one is called MaximizedEnd or MinimizedEnd
Is there anyway to do that?
Upvotes: 6
Views: 17001
Reputation: 889
I know this is an old thread but maybe someone will find this when they are looking for answers.
You could create your own custom event to do exactly what you want.
Public Event Maximized(sender As Object, e As EventArgs)
Private Sub frmMain_ResizeEnd(sender As Object, e As EventArgs) Handles Me.ResizeEnd
If Me.WindowState = FormWindowState.Maximized Then RaiseEvent Maximized(sender, Nothing)
End Sub
Private Sub frmMain_Maximized(sender As Object, e As EventArgs) Handles Me.Maximized
End Sub
or
Private Sub frmMain_Load(sender As Object, e As EventArgs) Handles MyBase.Load
AddHandler Me.Maximized, AddressOf MaximizedEventMethod
End Sub
Private Sub MaximizedEventMethod(sender As Object, e As EventArgs)
'add your code here
End Sub
Upvotes: 0
Reputation: 45
Using resize, resizeBegin, and resizeEnd event is good for doing something after winform is resized.
private bool resize_flag = true;
private void Form1_Resize(object sender, EventArgs e)
{
if (!resize_flag) return;
//your code here
resize_flag = true;
}
private void Form1_ResizeBegin(object sender, EventArgs e)
{
resize_flag = false;
}
private void Form1_ResizeEnd(object sender, EventArgs e)
{
//your code here
resize_flag = true;
}
this code is simple but it works ! resizeEnd is for resizing by mouse drag and resize is for changing windowState such as maximize or restore. I think overriding WndProc() is good but it's fired before the winform is resized so...
Upvotes: 2
Reputation: 26
I think we should override the wndProc and get the WM_SYSCOMMAND event fired.
protected override void WndProc(ref Message m)
{
if ((UInt32)m.Msg == Constant.WM_SYSCOMMAND)
{
switch ((UInt32)m.WParam)
{
case Constant.SC_MAXIMIZE:
case Constant.SC_RESTORE:
default:
break;
}
}
base.WndProc(ref m);
}
Upvotes: 0
Reputation: 1999
This is what Gabriel's solution would look like in detail. I don't think there is an event for WindoStateChanged either.
I just tested this solution out and it is working when you click the maximize button. It appears to be getting fired 3 times though. I would maybe do a little debugging and figure out on exactly what m.Msg you want to intercept to check if the state has changed. I found a quick reference of some of those WM_ messages here http://www.autohotkey.com/docs/misc/SendMessageList.htm.
protected override void WndProc(ref Message m)
{
FormWindowState previousWindowState = this.WindowState;
base.WndProc(ref m);
FormWindowState currentWindowState = this.WindowState;
if (previousWindowState != currentWindowState && currentWindowState == FormWindowState.Maximized)
{
// TODO: Do something the window has been maximized
}
}
As stated the above code gets fired 3 times at least while I have tested. The below code only gets fired once. It is a bit more lengthy but also may be more intuitive and more fully addresses your question of how to fire an event. Thanks to Yore for in his comment to your question for this idea.
public Form1()
{
InitializeComponent();
this.SizeChanged +=new EventHandler(Form1_SizeChanged);
FormMaximized += new EventHandler(Form1_FormMaximized);
_CurrentWindowState = this.WindowState;
if (_CurrentWindowState == FormWindowState.Maximized)
{
FireFormMaximized();
}
}
public event EventHandler FormMaximized;
private void FireFormMaximized()
{
if (FormMaximized != null)
{
FormMaximized(this, EventArgs.Empty);
}
}
private FormWindowState _CurrentWindowState;
private void Form1_SizeChanged(object sender, EventArgs e)
{
if (this.WindowState == FormWindowState.Maximized && _CurrentWindowState != FormWindowState.Maximized)
{
FireFormMaximized();
}
_CurrentWindowState = this.WindowState;
}
void Form1_FormMaximized(object sender, EventArgs e)
{
//TODO Put you're code here
}
Upvotes: 1
Reputation: 81610
I think it's as simple as this:
protected override void OnSizeChanged(EventArgs e) {
if (this.WindowState == FormWindowState.Maximized) {
MessageBox.Show("Max!");
}
base.OnSizeChanged(e);
}
Not sure what you mean by after the window is sized. This might work, too:
protected override void OnSizeChanged(EventArgs e) {
if (this.WindowState == FormWindowState.Maximized) {
this.BeginInvoke(new MethodInvoker(delegate { MessageBox.Show("Maxed"); }));
}
base.OnSizeChanged(e);
}
Replace the MessageBox.Show(...)
with your code.
Upvotes: 7
Reputation: 1785
This may not be the answer you're looking for but there are no defined events for the Windows Form that are called after the window is maximized. If there are any events to find, you'll have to go into the message loop yourself. Generally if I wanted to know if a user maximized the window but didn't care about the size being changed I would save the windowstate
and if it changes on SizedChanged
I could say that the window was maximized.
Upvotes: 0