MainForm.cs
author StephaneLenclud
Sun, 22 Feb 2015 20:56:08 +0100
changeset 68 bbe61b1021bb
parent 66 3461ee6800e3
child 72 471b1d45c46a
permissions -rw-r--r--
Adding HidP_GetUsageValue function declaration.
     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                 toolStripStatusLabelDevice.Text = aHidEvent.Device.FriendlyName;
   108             }
   109         }
   110 
   111 		protected override void WndProc(ref Message message)
   112 		{
   113             if (_remote != null)
   114             {
   115                 _remote.ProcessMessage(message);
   116             }
   117 			base.WndProc(ref message);
   118 		}
   119 
   120 		private bool _remote_ButtonPressed(object sender, RemoteControlEventArgs e)
   121 		{
   122             //Set text from here was disabled because of threading issues
   123             //That whole thing should be removed anyway
   124             bool processed = false;
   125 			_timer.Enabled = false;
   126             if (e.Button != RemoteControlButton.Unknown)
   127             {
   128                 //labelButtonName.Text = e.Button.ToString();
   129                 processed = true;
   130             }
   131             else if (e.MceButton != Hid.Usage.WindowsMediaCenterRemoteControl.Null)
   132             {
   133                 //Display MCE button name
   134                 //labelButtonName.Text = e.MceButton.ToString();
   135                 //Check if this is an HP extension
   136                 if (Enum.IsDefined(typeof(Hid.Usage.HpWindowsMediaCenterRemoteControl), (ushort)e.MceButton))
   137                 {
   138                     //Also display HP button name
   139                     //labelButtonName.Text += " / HP:" + ((Hid.UsageTables.HpWindowsMediaCenterRemoteControl)e.MceButton).ToString();
   140                 }
   141 
   142                 processed = true;                
   143             }
   144             else if (e.ConsumerControl != Hid.Usage.ConsumerControl.Null)
   145             {
   146                 //Display consumer control name
   147                 //labelButtonName.Text = e.ConsumerControl.ToString();
   148                 processed = true;
   149             }
   150             else
   151             {
   152                 //labelButtonName.Text = "Unknown";
   153             }
   154 			//labelDeviceName.Text = e.Device.ToString();
   155 			_timer.Enabled = true;
   156             return processed;
   157 		}
   158 
   159 		private void _timer_Tick(object sender, EventArgs e)
   160 		{
   161 			_timer.Enabled = false;
   162 			labelButtonName.Text = "Ready...";
   163 		}
   164 
   165 		private void buttonClear_Click(object sender, EventArgs e)
   166 		{
   167 			listViewEvents.Items.Clear();
   168 		}
   169 
   170         private void buttonTreeViewCollapseAll_Click(object sender, EventArgs e)
   171         {
   172             treeViewDevices.CollapseAll();            
   173         }
   174 
   175         private void buttonTreeViewExpandAll_Click(object sender, EventArgs e)
   176         {
   177             treeViewDevices.ExpandAll();
   178         }
   179 
   180 	}
   181 }