I am looking for event handler to get the information when HDMI is connected or disconnected using c#/WPF like how myNetworkAvailabilityChangeHandler is used to detect when internet gots disconnected or connected.
CodePudding user response:
There is no direct answer to this but you can use the SystemEvents to achieve this.
Code in C# :
main()
{
SystemEvents.DisplaySettingsChanged = new
EventHandler(SystemEvents_DisplaySettingsChanged);
}
private void SystemEvents_DisplaySettingsChanged(object sender, EventArgs e)
{
int HDMI_Monitors = 0;
ManagementClass mClass = new ManagementClass(@"\\localhost\ROOT\WMI:WmiMonitorConnectionParams");
foreach (ManagementObject mObject in mClass.GetInstances())
{
var ss = mObject["VideoOutputTechnology"];
if(ss.ToString().StartsWith("5"))
{
int HDMIport = Convert.ToInt32(ss);
if (HDMIport == 5)
{
HDMI_Monitors = 1;
}
}
}
}
You can use the model class to keep on updating the HDMI status. So every time your HDMI will connect or disconnect SystemEvents_DisplaySettingsChanged will trigger and then it will check the HDMI connection.