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
What Is the Simplest C# Function to Parse a JSON String into an Object
Generically Flatten JSON Using C#
Count Property VS Count() Method
Razor: Declarative HTML Helpers
C# - Capturing the Mouse Cursor Image
Addeventhandler Using Reflection
Can a C# Lambda Expression Have More Than One Statement
Windows Forms Progressbar: Easiest Way to Start/Stop Marquee
Mono Https Webrequest Fails with "The Authentication or Decryption Has Failed"
Obtain Sha-256 String of a String
How to Get Little Endian Data from Big Endian in C# Using Bitconverter.Toint32 Method
Parsing a JSON File with .Net Core 3.0/System.Text.JSON
Get Ssid of the Wireless Network I am Connected to with C# .Net on Windows Vista