检测分辨率的变化 c# WinForms
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/442337/
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
Detect change of resolution c# WinForms
提问by Matze
is there an easy way to hook to an event that is triggered on change of global screen resolution?
是否有一种简单的方法可以挂钩在全局屏幕分辨率更改时触发的事件?
采纳答案by Frederick The Fool
Handle the following event:
处理以下事件:
Microsoft.Win32.SystemEvents.DisplaySettingsChanged
You may refer to thispage for more details.
您可以参考此页面了解更多详情。
You may also wanna see the msdn articleon SystemEvents class.
您可能还想查看有关 SystemEvents 类的msdn 文章。
回答by Radoslav Hristov
There are two events - SystemEvents.DisplaySettingsChanged
and SystemEvents.DisplayedSettingsChanging
which you can handle.
Note that both events are static and you will need to detach your handlers before exiting from your program.
有两个事件-SystemEvents.DisplaySettingsChanged
并且SystemEvents.DisplayedSettingsChanging
,你可以处理。请注意,这两个事件都是静态的,您需要在退出程序之前分离处理程序。
回答by Jan Romell
Sure you don't have to unsubscribe to static events (or any events) if your program (process) is dying. The OS will take care of releasing all memory of your process to the OS. However, if you subscribe to a static event or to any event pointing to an object with a longer lifetime than your object subscribing, and you want that object to be eligible for GC - you need to unsubscribe (-=) to the event.
如果您的程序(进程)快要死了,当然您不必取消订阅静态事件(或任何事件)。操作系统将负责将进程的所有内存释放给操作系统。但是,如果您订阅了一个静态事件或指向一个对象的生命周期比您订阅的对象更长的任何事件,并且您希望该对象有资格进行 GC - 您需要取消订阅 (-=) 事件。
AND it is always good practice to always unsubscribe to all events. You never know when the lifetime of your objects are changed (by someone else) during the lifespan of your source code / product...
并且始终取消订阅所有事件始终是一种好习惯。在源代码/产品的生命周期中,您永远不知道对象的生命周期何时发生更改(由其他人)...
回答by Ramgy Borja
try this simple code
试试这个简单的代码
using Microsoft.Win32;
SystemEvents.DisplaySettingsChanged += new EventHandler(SystemEvents_DisplaySettingsChanged);
static void SystemEvents_DisplaySettingsChanged(object sender, EventArgs e)
{
MessageBox.Show("Resolution Change.");
}
and don't forget this line using Microsoft.Win32;
不要忘记这一行 using Microsoft.Win32;