C# 获取 WPF 窗口的最小化框点击
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/938924/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get the minimize box click of a WPF window
提问by Sauron
How to get the minimize box click event of a WPF window?
如何获取 WPF 窗口的最小化框单击事件?
采纳答案by ChrisF
There's an event called StateChanged
which (from the help) looks like it might do what you want.
有一个名为StateChanged
which (来自帮助)的事件看起来可能会做你想做的事。
Occurs when the window's WindowState property changes.
当窗口的 WindowState 属性更改时发生。
The help says it's only supported in .NET 3.0 and 3.5 under Vista, but I've just tried it on XP and it fires when the window is minimized, maximized and restored. However, from my testing, it fires afterthe state has changed, so if you want to do something before the window minimized this might not be the approach you need.
帮助说它仅在 Vista 下的 .NET 3.0 和 3.5 中受支持,但我刚刚在 XP 上尝试过它,当窗口最小化、最大化和恢复时它会触发。但是,根据我的测试,它会在状态更改后触发,因此如果您想在窗口最小化之前执行某些操作,这可能不是您需要的方法。
You'll have to check the actual state to make sure it's correct.
您必须检查实际状态以确保它是正确的。
private void Window_StateChanged(object sender, EventArgs e)
{
switch (this.WindowState)
{
case WindowState.Maximized:
MessageBox.Show("Maximized");
break;
case WindowState.Minimized:
MessageBox.Show("Minimized");
break;
case WindowState.Normal:
MessageBox.Show("Normal");
break;
}
}
Obviously if I was just printing out the state I'd use this.WindowState.ToString()
;)
显然,如果我只是打印出我会使用的状态this.WindowState.ToString()
;)
The following should get added to the XAML defintion of your window by Visual Studio:
Visual Studio 应将以下内容添加到窗口的 XAML 定义中:
StateChanged="Window_StateChanged"
回答by Shoban
Sorry this is not about WPF as I have not worked much with WPF. But one more thing you can do is check for Windowstate property during Resized event of Form. And if it is equal to FormWindowState.Minimized then minimize button is clicked (?) ;-)
抱歉,这与 WPF 无关,因为我对 WPF 的工作不多。但是您可以做的另一件事是在 Form 的 Resized 事件期间检查 Windowsstate 属性。如果它等于 FormWindowState.Minimized 然后点击最小化按钮(?);-)
回答by Prashant Cholachagudda
In addition to Shoban's answer...
除了Shoban的回答......
You can make use of Window's Deactivated event, you can even use StateChange Event as below
您可以使用 Window 的 Deactivated 事件,您甚至可以使用如下所示的 StateChange 事件
private void Window_Deactivated(object sender, EventArgs e)
{
if(this.WindowState== WindowState.Minimized)
// Do your stuff
}
it would help....
它会有所帮助....