.Net (C#) 检测电视是否已连接

发布于 2024-07-07 23:08:12 字数 48 浏览 5 评论 0原文

有人知道如何用 C# 检测电视当前是否连接到 PC 吗?

干杯

Anyone know how to detect if a television is currently connected to a PC in c#?

Cheers

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

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

发布评论

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

评论(2

坚持沉默 2024-07-14 23:08:12

设备是如何连接的?

每当发生设备到达/移除时,Windows 都会向系统中当前运行的所有应用程序发送一条名为 WM_DEVICECHANGE 的消息。 但要接收此消息,我们的应用程序应该处理“Windows Process 函数”。 C# 应用程序不会默认支持此功能,但可以添加它。 您可以扩展表单类。

对于 USB 大容量存储设备执行此操作的代码类似于:

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Runtime.InteropServices;
namespace WindowsApplication
{
    /// <summary>
    /// Summary description for Form1.
    /// </summary>
    public class Form1 : System.Windows.Forms.Form
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.Container components = null;

        public Form1()
        {
            //
            // Required for Windows Form Designer support
            //
            InitializeComponent();

            //
            // TODO: Add any constructor code after InitializeComponent call
            //
        }

        [StructLayout(LayoutKind.Sequential)] 
            public struct DEV_BROADCAST_VOLUME 
        { 
            public int dbcv_size; 
            public int dbcv_devicetype; 
            public int dbcv_reserved; 
            public int dbcv_unitmask; 
        } 

        protected override void WndProc(ref Message m) 
        { 
            //you may find these definitions in dbt.h and winuser.h 
            const int WM_DEVICECHANGE = 0x0219; 
            const int DBT_DEVICEARRIVAL = 0x8000;  // system detected a new device 
            const int DBT_DEVICEREMOVECOMPLETE = 0x8001;  // system detected a new device 
            const int DBT_DEVTYP_VOLUME = 0x00000002;  // logical volume 
            switch(m.Msg)
            {
                case WM_DEVICECHANGE:
                switch(m.WParam.ToInt32())
                {
                    case DBT_DEVICEARRIVAL:
                        { 
                            int devType = Marshal.ReadInt32(m.LParam,4); 
                            if(devType == DBT_DEVTYP_VOLUME) 
                            { 
                                DEV_BROADCAST_VOLUME vol; 
                                vol = (DEV_BROADCAST_VOLUME) 
                                    Marshal.PtrToStructure(m.LParam,typeof(DEV_BROADCAST_VOLUME)); 
                                MessageBox.Show(vol.dbcv_unitmask.ToString("x")); 
                            } 
                        } 
                        break;
                    case DBT_DEVICEREMOVECOMPLETE:
                        MessageBox.Show("Removal");
                        break;
                }

                    break;
            }
            //we detect the media arrival event 
            base.WndProc (ref m); 


        } 
        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        protected override void Dispose( bool disposing )
        {
            if( disposing )
            {
                if (components != null) 
                {
                    components.Dispose();
                }
            }
            base.Dispose( disposing );
        }


        #region Windows Form Designer generated code
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            // 
            // Form1
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.ClientSize = new System.Drawing.Size(292, 273);
            this.Name = "Form1";
            this.Text = "Form1";
            this.Load += new System.EventHandler(this.Form1_Load);

        }
        #endregion

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main() 
        {
            Application.Run(new Form1());
        }

        private void Form1_Load(object sender, System.EventArgs e)
        {

        }
    }
}

它可能会让您了解如何实现它。

How is the device attached?

Whenever a device arrival/removal happens, Windows sends a message called WM_DEVICECHANGE to all the applications running currently in the system. But to receive this message our application should handle the "Windows Process function". C# applications will not have default support for this function, but it's possible to add it. You could extend the form class.

The code to do this for a usb mass storage device would be something like:

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Runtime.InteropServices;
namespace WindowsApplication
{
    /// <summary>
    /// Summary description for Form1.
    /// </summary>
    public class Form1 : System.Windows.Forms.Form
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.Container components = null;

        public Form1()
        {
            //
            // Required for Windows Form Designer support
            //
            InitializeComponent();

            //
            // TODO: Add any constructor code after InitializeComponent call
            //
        }

        [StructLayout(LayoutKind.Sequential)] 
            public struct DEV_BROADCAST_VOLUME 
        { 
            public int dbcv_size; 
            public int dbcv_devicetype; 
            public int dbcv_reserved; 
            public int dbcv_unitmask; 
        } 

        protected override void WndProc(ref Message m) 
        { 
            //you may find these definitions in dbt.h and winuser.h 
            const int WM_DEVICECHANGE = 0x0219; 
            const int DBT_DEVICEARRIVAL = 0x8000;  // system detected a new device 
            const int DBT_DEVICEREMOVECOMPLETE = 0x8001;  // system detected a new device 
            const int DBT_DEVTYP_VOLUME = 0x00000002;  // logical volume 
            switch(m.Msg)
            {
                case WM_DEVICECHANGE:
                switch(m.WParam.ToInt32())
                {
                    case DBT_DEVICEARRIVAL:
                        { 
                            int devType = Marshal.ReadInt32(m.LParam,4); 
                            if(devType == DBT_DEVTYP_VOLUME) 
                            { 
                                DEV_BROADCAST_VOLUME vol; 
                                vol = (DEV_BROADCAST_VOLUME) 
                                    Marshal.PtrToStructure(m.LParam,typeof(DEV_BROADCAST_VOLUME)); 
                                MessageBox.Show(vol.dbcv_unitmask.ToString("x")); 
                            } 
                        } 
                        break;
                    case DBT_DEVICEREMOVECOMPLETE:
                        MessageBox.Show("Removal");
                        break;
                }

                    break;
            }
            //we detect the media arrival event 
            base.WndProc (ref m); 


        } 
        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        protected override void Dispose( bool disposing )
        {
            if( disposing )
            {
                if (components != null) 
                {
                    components.Dispose();
                }
            }
            base.Dispose( disposing );
        }


        #region Windows Form Designer generated code
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            // 
            // Form1
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.ClientSize = new System.Drawing.Size(292, 273);
            this.Name = "Form1";
            this.Text = "Form1";
            this.Load += new System.EventHandler(this.Form1_Load);

        }
        #endregion

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main() 
        {
            Application.Run(new Form1());
        }

        private void Form1_Load(object sender, System.EventArgs e)
        {

        }
    }
}

It might give you an idea how to implement it.

苯莒 2024-07-14 23:08:12

我想你在谈论显示器/电视?

您可以制作一个 winform 应用程序,其中有一个大按钮,当用户切换到电视时显示“TV CONNECTED”:-)

如果与 PC 的唯一连接只是 VGA 连接器,那么您就不走运了。 您始终可以实现连接到串行端口的红外传感器,以从遥控器读取红外信号(每当用户单击它时)。

I suppose you are talking about monitors/TV's ?

You can make a winform application with a big button saying 'TV CONNECTED' when the user switches to the TV :-)

If the only connection to the PC is just the VGA connector, you are out of luck. You could always implement an infrared sensor connected to a serial port to read infrared signals from the remote control (whenever the user clicks it).

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