How to Detect When a Windows Form Is Being Minimized

What Event is used for Maximizing and Minimizing?

This is Tested code

 private void Form1_Resize(object sender, EventArgs e)
{
if (WindowState == FormWindowState.Minimized)
{
MessageBox.Show("Minimize");
}
else if (WindowState == FormWindowState.Maximized)
{
MessageBox.Show("Maximize");
}

}

Detect all situations where form is minimized

As explained here, How to detect when the form is being maximized?, listen to the WM_SIZE messages.

Declare in your form:

procedure WMSize(var Msg: TMessage); message WM_SIZE;

And implementation:

procedure TForm1.WMSize(var Msg: TMessage);
begin
Inherited;
if Msg.WParam = SIZE_MINIMIZED then
ShowMessage('Minimized');
end;

Update

See also the answer by @bummi where there is a solution when Application.MainFormOnTaskbar = false.

Check if currently minimized window was in maximized or normal state at the time of minimization

WinForms does not expose any WindowStateChanged event then you have to track it by yourself. Windows will send a WM_SYSCOMMAND when form state changes:

partial class MyForm : Form
{
public MyForm()
{
InitializeComponent();

_isMaximized = WindowState == FormWindowState.Maximized;
}

protected override void WndProc(ref Message m)
{
if (m.Msg == WM_SYSCOMMAND)
{
int wparam = m.WParam.ToInt32() & 0xfff0;

if (wparam == SC_MAXIMIZE)
_isMaximized = true;
else if (wparam == SC_RESTORE)
_isMaximized = false;
}

base.WndProc(ref m);
}

private const int WM_SYSCOMMAND = 0x0112;
private const int SC_MAXIMIZE = 0xf030;
private const int SC_RESTORE = 0xf120;
private bool _isMaximized;
}

C# WinForms minimize frm.Show() when main form is minimized

You could create a recursive function that will minimize all child forms.

public void MinimizeAllChildForms(Form parent)
{
foreach(Form f in parent.OwnedForms)
{
f.WindowState = FormWindowState.Minimized;
MinimizeAllChildForms(f);
}
}

Then in your main form, call it from the Resize event.

void MainForm_Resize(object sender, EventArgs e)
{
if (WindowState == FormWindowState.Minimized)
MinimizeAllChildForms(this);
}

I assume you want frmPleaseWait to also minimize when frmOverlay is minimized. If you don't, then you can simply set its parent to the main form instead so that it automatically minimizes when the main form does.

frmPleaseWait = new frmPleaseWait(_text); 
frmPleaseWait.Show(MainForm);

C# Windows Form start up always minimized

try to add this code in form load event and test

this.WindowState = FormWindowState.Normal;

detect keypression when minimized and trayicon

Your form will only receive keypress events when it has focus, to receive other keypress events you would need to register a global hotkey.

http://www.dreamincode.net/forums/topic/180436-global-hotkeys/

How can I tell when a user minimizes the form?

MinimumSizeChanged has nothing to do with the form getting minimized. MinimumSizeChanged has to deal with when the MinimumSize properties of the form are changed.

You want to check the Resize event of the form.



Related Topics



Leave a reply



Submit