[英]How to detect when a windows form is being minimized?
我知道我可以通过WindowState获取当前状态,但我想知道当用户尝试最小化表单时是否会发生任何事件。
You can use the Resize event and check the Forms.WindowState Property in the event. 您可以使用Resize事件并检查事件中的Forms.WindowState属性。
private void Form1_Resize ( object sender , EventArgs e )
{
if ( WindowState == FormWindowState.Minimized )
{
// Do some stuff
}
}
To get in before the form has been minimised you'll have to hook into the WndProc procedure: 要在表单最小化之前进入,您必须挂钩到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);
}
To react after the form has been minimised hook into the Resize
event as the other answers point out (included here for completeness): 在表单被最小化后做出反应,挂钩进入
Resize
事件,其他答案指出(包含在这里是为了完整性):
private void Form1_Resize (object sender, EventArgs e)
{
if (WindowState == FormWindowState.Minimized)
{
// Do your action
}
}
我不知道具体事件,但是当窗体最小化时会触发Resize
事件,您可以在该事件中检查FormWindowState.Minimized
For people who search for WPF windows minimizing event : 对于搜索最小化事件的WPF窗口的人:
It's a bit different. 这有点不同。 For the callback use WindowState :
对于回调使用WindowState:
private void Form1_Resize(object sender, EventArgs e)
{
if (WindowState == FormWindowState.Minimized)
{
// Do some stuff
}
}
The event to use is StateChanged (instead Resize): 要使用的事件是StateChanged(而不是Resize):
public Main()
{
InitializeComponent();
this.StateChanged += Form1_Resize;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.