首页 / C# / .Net(C#)检测是否连接了电视
.Net(C#)检测是否连接了电视
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了.Net(C#)检测是否连接了电视,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2809字,纯文字阅读大概需要5分钟。
内容图文
有人知道如何使用c#来检测电视当前是否已连接到PC吗?
干杯
解决方法:
设备如何连接?
每当设备到达/卸下时,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>
/// 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)
{
}
}
}
它可能会给您一个实现方法的想法.
内容总结
以上是互联网集市为您收集整理的.Net(C#)检测是否连接了电视全部内容,希望文章能够帮你解决.Net(C#)检测是否连接了电视所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。