MainForm.cs
author StephaneLenclud
Sun, 15 Feb 2015 21:48:07 +0100
changeset 63 4b8b058de215
parent 49 2ec781e51f36
child 65 09f1435bfb94
permissions -rw-r--r--
HidDevice: Adding friendly name property.
     1 using System;
     2 using System.Drawing;
     3 using System.Collections;
     4 using System.ComponentModel;
     5 using System.Windows.Forms;
     6 using System.Data;
     7 using Devices.RemoteControl;
     8 
     9 namespace RemoteControlSample
    10 {
    11 	/// <summary>
    12 	/// Summary description for Form1.
    13 	/// </summary>
    14 	public partial class MainForm : System.Windows.Forms.Form
    15 	{
    16 		/// <summary>
    17 		/// Required designer variable.
    18 		/// </summary>
    19         private System.ComponentModel.Container components = null;
    20         private RemoteControlDevice _remote;
    21         private Label labelButtonName;
    22         private Label labelDeviceName;
    23         private Button buttonClear;
    24         private TabControl tabControl;
    25         private TabPage tabPageMessages;
    26         private ListView listViewEvents;
    27         private ColumnHeader columnHeaderUsages;
    28         private ColumnHeader columnHeaderInputReport;
    29         private ColumnHeader columnHeaderUsagePage;
    30         private ColumnHeader columnHeaderUsageCollection;
    31         private ColumnHeader columnHeaderRepeat;
    32         private ColumnHeader columnHeaderTime;
    33         private TabPage tabPageDevices;
    34         private TreeView treeViewDevices;
    35 		private Timer _timer;
    36 
    37         public delegate void OnHidEventDelegate(object aSender, Hid.HidEvent aHidEvent);
    38 
    39 		public MainForm()
    40 		{
    41 			//
    42 			// Required for Windows Form Designer support
    43 			//
    44 			InitializeComponent();
    45 
    46 			_timer = new Timer();
    47 			_timer.Interval = 3000;
    48 			_timer.Enabled = false;
    49 			_timer.Tick +=new EventHandler(_timer_Tick);            
    50 		}
    51 
    52 		/// <summary>
    53 		/// Clean up any resources being used.
    54 		/// </summary>
    55 		protected override void Dispose( bool disposing )
    56 		{
    57 			if( disposing )
    58 			{
    59 				if (components != null)
    60 				{
    61 					components.Dispose();
    62 				}
    63 			}
    64 			base.Dispose( disposing );
    65 		}
    66 
    67 
    68 		/// <summary>
    69 		/// The main entry point for the application.
    70 		/// </summary>
    71 		[STAThread]
    72 		static void Main()
    73 		{
    74             Application.EnableVisualStyles();
    75 			Application.Run(new MainForm());
    76 		}
    77 
    78         private void MainForm_Load(object sender, System.EventArgs e)
    79 		{
    80             _remote = new RemoteControlDevice(this.Handle);
    81             _remote.ButtonPressed += new Devices.RemoteControl.RemoteControlDevice.RemoteControlDeviceEventHandler(_remote_ButtonPressed);
    82             _remote.iHidHandler.OnHidEvent += HandleHidEventThreadSafe;
    83             
    84             //
    85             Win32.RawInput.PopulateDeviceList(treeViewDevices);
    86 
    87 		}
    88 
    89         public void HandleHidEventThreadSafe(object aSender, Hid.HidEvent aHidEvent)
    90         {
    91             if (aHidEvent.IsStray)
    92             {
    93                 //Stray event just ignore it
    94                 return;
    95             }
    96 
    97             if (this.InvokeRequired)
    98             {
    99                 //Not in the proper thread, invoke ourselves
   100                 OnHidEventDelegate d = new OnHidEventDelegate(HandleHidEventThreadSafe);
   101                 this.Invoke(d, new object[] { aSender, aHidEvent });
   102             }
   103             else
   104             {
   105                 //We are in the proper thread
   106                 listViewEvents.Items.Insert(0, aHidEvent.ToListViewItem());
   107             }
   108         }
   109 
   110 		protected override void WndProc(ref Message message)
   111 		{
   112             if (_remote != null)
   113             {
   114                 _remote.ProcessMessage(message);
   115             }
   116 			base.WndProc(ref message);
   117 		}
   118 
   119 		private bool _remote_ButtonPressed(object sender, RemoteControlEventArgs e)
   120 		{
   121             //Set text from here was disabled because of threading issues
   122             //That whole thing should be removed anyway
   123             bool processed = false;
   124 			_timer.Enabled = false;
   125             if (e.Button != RemoteControlButton.Unknown)
   126             {
   127                 //labelButtonName.Text = e.Button.ToString();
   128                 processed = true;
   129             }
   130             else if (e.MceButton != Hid.UsageTables.WindowsMediaCenterRemoteControl.Null)
   131             {
   132                 //Display MCE button name
   133                 //labelButtonName.Text = e.MceButton.ToString();
   134                 //Check if this is an HP extension
   135                 if (Enum.IsDefined(typeof(Hid.UsageTables.HpWindowsMediaCenterRemoteControl), (ushort)e.MceButton))
   136                 {
   137                     //Also display HP button name
   138                     //labelButtonName.Text += " / HP:" + ((Hid.UsageTables.HpWindowsMediaCenterRemoteControl)e.MceButton).ToString();
   139                 }
   140 
   141                 processed = true;                
   142             }
   143             else if (e.ConsumerControl != Hid.UsageTables.ConsumerControl.Null)
   144             {
   145                 //Display consumer control name
   146                 //labelButtonName.Text = e.ConsumerControl.ToString();
   147                 processed = true;
   148             }
   149             else
   150             {
   151                 //labelButtonName.Text = "Unknown";
   152             }
   153 			//labelDeviceName.Text = e.Device.ToString();
   154 			_timer.Enabled = true;
   155             return processed;
   156 		}
   157 
   158 		private void _timer_Tick(object sender, EventArgs e)
   159 		{
   160 			_timer.Enabled = false;
   161 			labelButtonName.Text = "Ready...";
   162 		}
   163 
   164 		private void buttonClear_Click(object sender, EventArgs e)
   165 		{
   166 			listViewEvents.Items.Clear();
   167 		}
   168 
   169 	}
   170 }