开发者

Detecting HDMI cable event within .NET?

I'm trying to detect the event when a HDMI cable is plugged into the HDMI port of my laptop from within the .NET framework. I know that there is an event being triggered, because when the cable is plugged in, Windows makes the obvious "new hardware" sound and changes screen resolution to something more suitable.

I'm getting a bit tired of having th开发者_JAVA百科e change my audio device to HDMI output by hand so want to write a small app to do it for me.


Have you tried WM_DEVICECHANGE? I don't have a way to test it myself.

If you're using Winforms you can override the Control.WndProc method to deal with Windows messages that the framework doesn't wrap.


I'm very surprised that noone here mentioned about the DisplaySettingsChanging or DisplaySettingsChanged events. When you plug-in or out an HDMI cable, o/s detects it for you and renumerates the screens. You can catch that events.

An example piece of code would be:

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)        
        {

            Microsoft.Win32.SystemEvents.DisplaySettingsChanged += SystemEvents_DisplaySettingsChanged;

            Microsoft.Win32.SystemEvents.DisplaySettingsChanging += SystemEvents_DisplaySettingsChanging;

            Console.Read();
        }

        static void SystemEvents_DisplaySettingsChanged(object sender, EventArgs e)
        {
            Console.WriteLine("Display settings have been changed.");
        }

        static void SystemEvents_DisplaySettingsChanging(object sender, EventArgs e)
        {
            Console.WriteLine("Display settings are changing now...");
        }

    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜