2008-11-03 11 views

답변

5

장치는 어떻게 연결됩니까?

장치 도착/제거가 발생할 때마다 Windows는 현재 시스템에 실행중인 모든 응용 프로그램에 WM_DEVICECHANGE라는 메시지를 보냅니다. 그러나이 메시지를 받으려면 응용 프로그램에서 "Windows 프로세스 기능"을 처리해야합니다. C# 응용 프로그램에는이 기능에 대한 기본 지원이 없지만 추가 할 수 있습니다. 폼 클래스를 확장 할 수 있습니다. 그것은 그것을 구현하는 방법을 생각 당신에게 줄 수도

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) 
     { 

     } 
    } 
} 

:

코드

뭔가 같은 것 USB 대용량 저장 장치에 대해이 작업을 수행합니다.

+1

어떻게 이런 식으로 코드를 포맷 할 수 있습니까? 그것은 끔찍합니다. – Behrooz

-2

당신은 모니터/TV에 대해 이야기하고 있다고 생각하십니까?

당신은 큰 버튼

PC에 유일한 연결이 바로 VGA 커넥터의 경우

:-) TV에 '연결된 TV를'때 사용자 스위치를 말하는와의 WinForm 응용 프로그램을 만들 수 있습니다

, 당신은 벗어 운. 원격 포트에서 적외선 신호를 읽으려면 (사용자가 클릭 할 때마다) 직렬 포트에 연결된 적외선 센서를 항상 구현할 수 있습니다.