当前位置:  开发笔记 > 编程语言 > 正文

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

如何解决《.Net(C#)检测电视是否已连接》经验,为你挑选了1个好方法。

任何人都知道如何检测电视当前是否连接到c#中的PC?

干杯



1> Kristian..:

设备是如何连接的?

每当设备到达/移除时,Windows都会向系统中当前运行的所有应用程序发送一条名为WM_DEVICECHANGE的消息.但是要接收此消息,我们的应用程序应该处理"Windows进程功能".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 description for Form1.
    /// 
    public class Form1 : System.Windows.Forms.Form
    {
        /// 
        /// Required designer variable.
        /// 
        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); 


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


        #region Windows Form Designer generated code
        /// 
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// 
        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

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

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

        }
    }
}

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

推荐阅读
linjiabin43
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有