Windows 7 中有时无法检测到 HDMI 电视?可以在代码中调用检测吗?

发布于 2024-11-29 23:30:24 字数 125 浏览 2 评论 0原文

我有一台独立的公共信息亭电脑,每天都会自动启动。连接到高清电视后有时无法检测到。我必须亲自去电脑,转到屏幕分辨率并按检测它的工作原理。

我的问题是我如何知道我想要它显示的监视器是否在代码中正确连接?

谢谢

i have a standalone public kiosk pc that startups automatically everyday. It is connected to a HD TV and sometimes it is not detected. i have to personally go down to the PC, go to Screen Resolution and press Detect which it works.

my question is how do i know if the monitor i want it to display is connected properly in code?

thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

随遇而安 2024-12-06 23:30:24

我确实设法在 MSDN 站点上找到了一些代码,其中用户使用代码来确定连接的监视器类型。我希望这对您来说是一个很好的起点。

using System;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Interop;

namespace CRT
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {
        public class NativeMethods

        {
            [DllImport("user32.dll", EntryPoint = "MonitorFromWindow", SetLastError = true)]
            public static extern IntPtr MonitorFromWindow(
                [In] IntPtr hwnd, uint dwFlags);

            [DllImport("dxva2.dll", EntryPoint = "GetNumberOfPhysicalMonitorsFromHMONITOR", SetLastError = true)]
            [return: MarshalAs(UnmanagedType.Bool)]
            public static extern bool GetNumberOfPhysicalMonitorsFromHMONITOR(
                IntPtr hMonitor, ref uint pdwNumberOfPhysicalMonitors);

            [DllImport("dxva2.dll", EntryPoint = "GetPhysicalMonitorsFromHMONITOR", SetLastError = true)]
            [return: MarshalAs(UnmanagedType.Bool)]
            public static extern bool GetPhysicalMonitorsFromHMONITOR(
                IntPtr hMonitor,
                uint dwPhysicalMonitorArraySize,
                [Out] NativeStructures.PHYSICAL_MONITOR[] pPhysicalMonitorArray);

            [DllImport("dxva2.dll", EntryPoint = "DestroyPhysicalMonitors", SetLastError = true)]
            [return: MarshalAs(UnmanagedType.Bool)]
            public static extern bool DestroyPhysicalMonitors(
                uint dwPhysicalMonitorArraySize, [Out] NativeStructures.PHYSICAL_MONITOR[] pPhysicalMonitorArray);

            [DllImport("dxva2.dll", EntryPoint = "GetMonitorTechnologyType", SetLastError = true)]
            [return: MarshalAs(UnmanagedType.Bool)]
            public static extern bool GetMonitorTechnologyType(
                IntPtr hMonitor, ref NativeStructures.MC_DISPLAY_TECHNOLOGY_TYPE pdtyDisplayTechnologyType);

            [DllImport("dxva2.dll", EntryPoint = "GetMonitorCapabilities", SetLastError = true)]
            [return: MarshalAs(UnmanagedType.Bool)]
            public static extern bool GetMonitorCapabilities(
                IntPtr hMonitor, ref uint pdwMonitorCapabilities, ref uint pdwSupportedColorTemperatures);
        }

        public class NativeConstants
        {
            public const int MONITOR_DEFAULTTOPRIMARY = 1;

            public const int MONITOR_DEFAULTTONEAREST = 2;

            public const int MONITOR_DEFAULTTONULL = 0;
        }

        public class NativeStructures
        {
            [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
            public struct PHYSICAL_MONITOR
            {
                public IntPtr hPhysicalMonitor;

                [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)] public string szPhysicalMonitorDescription;
            }

            public enum MC_DISPLAY_TECHNOLOGY_TYPE
            {
                MC_SHADOW_MASK_CATHODE_RAY_TUBE,

                MC_APERTURE_GRILL_CATHODE_RAY_TUBE,

                MC_THIN_FILM_TRANSISTOR,

                MC_LIQUID_CRYSTAL_ON_SILICON,

                MC_PLASMA,

                MC_ORGANIC_LIGHT_EMITTING_DIODE,

                MC_ELECTROLUMINESCENT,

                MC_MICROELECTROMECHANICAL,

                MC_FIELD_EMISSION_DEVICE,
            }
        }

        public Window1() { InitializeComponent(); }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            WindowInteropHelper helper = new WindowInteropHelper(this);

            IntPtr hMonitor = NativeMethods.MonitorFromWindow(helper.Handle, NativeConstants.MONITOR_DEFAULTTOPRIMARY);
            int lastWin32Error = Marshal.GetLastWin32Error();

            uint pdwNumberOfPhysicalMonitors = 0u;
            bool numberOfPhysicalMonitorsFromHmonitor = NativeMethods.GetNumberOfPhysicalMonitorsFromHMONITOR(
                hMonitor, ref pdwNumberOfPhysicalMonitors);
            lastWin32Error = Marshal.GetLastWin32Error();

            NativeStructures.PHYSICAL_MONITOR[] pPhysicalMonitorArray =
                new NativeStructures.PHYSICAL_MONITOR[pdwNumberOfPhysicalMonitors];
            bool physicalMonitorsFromHmonitor = NativeMethods.GetPhysicalMonitorsFromHMONITOR(
                hMonitor, pdwNumberOfPhysicalMonitors, pPhysicalMonitorArray);
            lastWin32Error = Marshal.GetLastWin32Error();

            uint pdwMonitorCapabilities = 0u;
            uint pdwSupportedColorTemperatures = 0u;
            var monitorCapabilities = NativeMethods.GetMonitorCapabilities(
                pPhysicalMonitorArray[0].hPhysicalMonitor, ref pdwMonitorCapabilities, ref pdwSupportedColorTemperatures);
            lastWin32Error = Marshal.GetLastWin32Error();

            NativeStructures.MC_DISPLAY_TECHNOLOGY_TYPE type =
                NativeStructures.MC_DISPLAY_TECHNOLOGY_TYPE.MC_SHADOW_MASK_CATHODE_RAY_TUBE;
            var monitorTechnologyType = NativeMethods.GetMonitorTechnologyType(
                pPhysicalMonitorArray[0].hPhysicalMonitor, ref type);
            lastWin32Error = Marshal.GetLastWin32Error();

            var destroyPhysicalMonitors = NativeMethods.DestroyPhysicalMonitors(
                pdwNumberOfPhysicalMonitors, pPhysicalMonitorArray);
            lastWin32Error = Marshal.GetLastWin32Error();

            this.lbl.Content = type;
        }
    }
}

I did manage to find some code on the MSDN site where a user is using code to determine which type of monitor is connected. I hope this may be a good starting point for you.

using System;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Interop;

namespace CRT
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {
        public class NativeMethods

        {
            [DllImport("user32.dll", EntryPoint = "MonitorFromWindow", SetLastError = true)]
            public static extern IntPtr MonitorFromWindow(
                [In] IntPtr hwnd, uint dwFlags);

            [DllImport("dxva2.dll", EntryPoint = "GetNumberOfPhysicalMonitorsFromHMONITOR", SetLastError = true)]
            [return: MarshalAs(UnmanagedType.Bool)]
            public static extern bool GetNumberOfPhysicalMonitorsFromHMONITOR(
                IntPtr hMonitor, ref uint pdwNumberOfPhysicalMonitors);

            [DllImport("dxva2.dll", EntryPoint = "GetPhysicalMonitorsFromHMONITOR", SetLastError = true)]
            [return: MarshalAs(UnmanagedType.Bool)]
            public static extern bool GetPhysicalMonitorsFromHMONITOR(
                IntPtr hMonitor,
                uint dwPhysicalMonitorArraySize,
                [Out] NativeStructures.PHYSICAL_MONITOR[] pPhysicalMonitorArray);

            [DllImport("dxva2.dll", EntryPoint = "DestroyPhysicalMonitors", SetLastError = true)]
            [return: MarshalAs(UnmanagedType.Bool)]
            public static extern bool DestroyPhysicalMonitors(
                uint dwPhysicalMonitorArraySize, [Out] NativeStructures.PHYSICAL_MONITOR[] pPhysicalMonitorArray);

            [DllImport("dxva2.dll", EntryPoint = "GetMonitorTechnologyType", SetLastError = true)]
            [return: MarshalAs(UnmanagedType.Bool)]
            public static extern bool GetMonitorTechnologyType(
                IntPtr hMonitor, ref NativeStructures.MC_DISPLAY_TECHNOLOGY_TYPE pdtyDisplayTechnologyType);

            [DllImport("dxva2.dll", EntryPoint = "GetMonitorCapabilities", SetLastError = true)]
            [return: MarshalAs(UnmanagedType.Bool)]
            public static extern bool GetMonitorCapabilities(
                IntPtr hMonitor, ref uint pdwMonitorCapabilities, ref uint pdwSupportedColorTemperatures);
        }

        public class NativeConstants
        {
            public const int MONITOR_DEFAULTTOPRIMARY = 1;

            public const int MONITOR_DEFAULTTONEAREST = 2;

            public const int MONITOR_DEFAULTTONULL = 0;
        }

        public class NativeStructures
        {
            [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
            public struct PHYSICAL_MONITOR
            {
                public IntPtr hPhysicalMonitor;

                [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)] public string szPhysicalMonitorDescription;
            }

            public enum MC_DISPLAY_TECHNOLOGY_TYPE
            {
                MC_SHADOW_MASK_CATHODE_RAY_TUBE,

                MC_APERTURE_GRILL_CATHODE_RAY_TUBE,

                MC_THIN_FILM_TRANSISTOR,

                MC_LIQUID_CRYSTAL_ON_SILICON,

                MC_PLASMA,

                MC_ORGANIC_LIGHT_EMITTING_DIODE,

                MC_ELECTROLUMINESCENT,

                MC_MICROELECTROMECHANICAL,

                MC_FIELD_EMISSION_DEVICE,
            }
        }

        public Window1() { InitializeComponent(); }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            WindowInteropHelper helper = new WindowInteropHelper(this);

            IntPtr hMonitor = NativeMethods.MonitorFromWindow(helper.Handle, NativeConstants.MONITOR_DEFAULTTOPRIMARY);
            int lastWin32Error = Marshal.GetLastWin32Error();

            uint pdwNumberOfPhysicalMonitors = 0u;
            bool numberOfPhysicalMonitorsFromHmonitor = NativeMethods.GetNumberOfPhysicalMonitorsFromHMONITOR(
                hMonitor, ref pdwNumberOfPhysicalMonitors);
            lastWin32Error = Marshal.GetLastWin32Error();

            NativeStructures.PHYSICAL_MONITOR[] pPhysicalMonitorArray =
                new NativeStructures.PHYSICAL_MONITOR[pdwNumberOfPhysicalMonitors];
            bool physicalMonitorsFromHmonitor = NativeMethods.GetPhysicalMonitorsFromHMONITOR(
                hMonitor, pdwNumberOfPhysicalMonitors, pPhysicalMonitorArray);
            lastWin32Error = Marshal.GetLastWin32Error();

            uint pdwMonitorCapabilities = 0u;
            uint pdwSupportedColorTemperatures = 0u;
            var monitorCapabilities = NativeMethods.GetMonitorCapabilities(
                pPhysicalMonitorArray[0].hPhysicalMonitor, ref pdwMonitorCapabilities, ref pdwSupportedColorTemperatures);
            lastWin32Error = Marshal.GetLastWin32Error();

            NativeStructures.MC_DISPLAY_TECHNOLOGY_TYPE type =
                NativeStructures.MC_DISPLAY_TECHNOLOGY_TYPE.MC_SHADOW_MASK_CATHODE_RAY_TUBE;
            var monitorTechnologyType = NativeMethods.GetMonitorTechnologyType(
                pPhysicalMonitorArray[0].hPhysicalMonitor, ref type);
            lastWin32Error = Marshal.GetLastWin32Error();

            var destroyPhysicalMonitors = NativeMethods.DestroyPhysicalMonitors(
                pdwNumberOfPhysicalMonitors, pPhysicalMonitorArray);
            lastWin32Error = Marshal.GetLastWin32Error();

            this.lbl.Content = type;
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文