我知道我可以通过WindowState获取当前状态,但是我想知道当用户尝试最小化表单时是否会触发任何事件。
Answers:
您可以使用Resize事件并检查事件中的Forms.WindowState属性。
private void Form1_Resize ( object sender , EventArgs e )
{
if ( WindowState == FormWindowState.Minimized )
{
// Do some stuff
}
}
要在最小化表单之前进入,您必须加入WndProc过程:
private const int WM_SYSCOMMAND = 0x0112;
private const int SC_MINIMIZE = 0xF020;
[SecurityPermission(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.UnmanagedCode)]
protected override void WndProc(ref Message m)
{
switch(m.Msg)
{
case WM_SYSCOMMAND:
int command = m.WParam.ToInt32() & 0xfff0;
if (command == SC_MINIMIZE)
{
// Do your action
}
// If you don't want to do the default action then break
break;
}
base.WndProc(ref m);
}
在最小化表单后做出反应,Resize
如其他答案所指出的,请挂接到事件中(为完整性起见,请参见此处):
private void Form1_Resize (object sender, EventArgs e)
{
if (WindowState == FormWindowState.Minimized)
{
// Do your action
}
}