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