Event When a Window Gets Maximized/Un-Maximized

Event when a window gets maximized/un-maximized

You can do this by overriding WndProc:

protected override void WndProc( ref Message m )
{
if( m.Msg == 0x0112 ) // WM_SYSCOMMAND
{
// Check your window state here
if (m.WParam == new IntPtr( 0xF030 ) ) // Maximize event - SC_MAXIMIZE from Winuser.h
{
// THe window is being maximized
}
}
base.WndProc(ref m);
}

This should handle the event on any window. SC_RESTORE is 0xF120, and SC_MINIMIZE is 0XF020, if you need those constants, too.

Event called after windows maximized

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
}

Obtain non-maximized window position/size when window is maximized

The best way to solve this I found is to use the RestoreBounds structure. When a window is maximized, RestoreBounds will refer to the old (non-maximized) size and position. Here is the code to find out these values. Simply save these values upon closing, and then when the program is reopened, you can set the Width, Height, Location.X and Location.Y of the form back to these saved values.

bool b = WindowState == FormWindowState.Maximized;
int xpos = !b? Location.X : RestoreBounds.X;
int ypos = !b? Location.Y : RestoreBounds.Y;
int width = !b? Width : RestoreBounds.Width;
int height = !b? Height : RestoreBounds.Height;

Is there an event in Powerpoint when the window is minimized maximized or changed in size?

Found an alternative, Though i can't find the minimize/maximize etc, i at least have an event which fires when the size of the window is changed.

Just use the SizeChanged Event inside the UserControl which was binded to the CustomTaskPane you created in Powerpoint.

this.SizeChanged += Event_SizeChanged;
private void Event_SizeChanged(object sender, EventArgs e)
{
//Your code here
}

How to trigger Control.MouseMove event when maximized/un-maximized form in winforms C#

Events can only be triggered by the implementing class per definition.
But you can target the MouseMove, Maximize and Minimize event to the same non anonymous delegate created at the same scope as your original MouseMove delegate to retain usage of your local variables.

You will need to create Maximized and Minimized events in your form yourself though, because they aren't provided in winforms (see Event when a window gets maximized/un-maximized)

public event Action<object> Maximized;
public event Action<object> Minimized;
protected override void WndProc(ref Message m) {
if (m.Msg == 0x0112) { // WM_SYSCOMMAND
// Check your window state here
if (m.WParam == new IntPtr(0xF030) && Maximized != null) Maximized(this);// Maximize event - SC_MAXIMIZE from Winuser.h
if (m.WParam == new IntPtr(0XF020) && Minimized != null) Minimized(this);// Minimize event - SC_MINIMIZE from Winuser.h
}
base.WndProc(ref m);
}

Your previous code could then be reworked into this:

var MMove = new Action<Point>(mousePosition =>
{
if (Dragging)
{
if (direction != Direction.Vertical)
container.Left = Math.Max(0, mousePosition.X + container.Left - DragStart.X);
if (direction != Direction.Horizontal)
container.Top = Math.Max(0, mousePosition.Y + container.Top - DragStart.Y);
}
});
this.MouseMove += (sender,e) => MMove(e.Location);
this.Maximized += (sender) => MMove(MousePosition);
this.Minimized += (sender) => MMove(MousePosition);

Electron/Javascript: Detect when window is un/maximized

maximize / unmaximized event is availble for browserwindow. https://github.com/electron/electron/blob/master/docs/api/browser-window.md#event-maximize

Prevent MaxSized C# WinForm from moving when being maximized

Override the WndProc function. Check for the maximize event. Set your preferred max size manually and return from the function. That way you are overriding the predefined maximize behaviour:

protected override void WndProc(ref Message m)
{
if (m.Msg == 0x0112) // WM_SYSCOMMAND
{
if (m.WParam == new IntPtr(0xF030)) // Maximize event
{
Size = MaximumSize; //Set size manually and return
return;
}
}
base.WndProc(ref m);
}

This will prevent the maximize button from changing into a normalize button. Using the following code won't prevent the button from changing:

struct MinMaxInfo
{
public Point ptReserved;
public Point ptMaxSize;
public Point ptMaxPosition;
public Point ptMinTrackSize;
public Point ptMaxTrackSize;
}

protected override void WndProc(ref Message m)
{
base.WndProc(ref m); //do that first: "'Who is the boss' applies. You'd typically want to be the one that has the last say in this case."
if (m.Msg == 0x0024) //WM_GETMINMAXINFO
{
MinMaxInfo minMaxInfo = (MinMaxInfo)m.GetLParam(typeof(MinMaxInfo));
minMaxInfo.ptMaxSize.X = MaximumSize.Width; //Set size manually
minMaxInfo.ptMaxSize.Y = MaximumSize.Height;
minMaxInfo.ptMaxPosition.X = Location.X; //Stay at current position
minMaxInfo.ptMaxPosition.Y = Location.Y;
Marshal.StructureToPtr(minMaxInfo, m.LParam, true);
}
}


Related Topics



Leave a reply



Submit