[Roam-commit] SF.net SVN: roam: [94] Trunk/Source/Roam
Status: Alpha
Brought to you by:
jonfroehlich
|
From: <jon...@us...> - 2007-09-15 07:41:20
|
Revision: 94
http://roam.svn.sourceforge.net/roam/?rev=94&view=rev
Author: jonfroehlich
Date: 2007-09-15 00:41:18 -0700 (Sat, 15 Sep 2007)
Log Message:
-----------
added filtering to Hook
updated WindowsMessagingUtils and WindowsConstants to provide more functionality for HotKeys
Modified Paths:
--------------
Trunk/Source/Roam/Utils/WindowsConstants.cs
Trunk/Source/Roam.Test/Utils/HookTest.Designer.cs
Trunk/Source/Roam.Test/Utils/HookTest.cs
Trunk/Source/Roam.UI/Utils/Hook.cs
Trunk/Source/Roam.UI/Utils/WindowsMessagingUtils.cs
Modified: Trunk/Source/Roam/Utils/WindowsConstants.cs
===================================================================
--- Trunk/Source/Roam/Utils/WindowsConstants.cs 2007-09-13 08:31:36 UTC (rev 93)
+++ Trunk/Source/Roam/Utils/WindowsConstants.cs 2007-09-15 07:41:18 UTC (rev 94)
@@ -181,5 +181,6 @@
public static int MOD_SHIFT = 0x0004;
public static int MOD_WIN = 0x0008;
public static int MOD_KEYUP = 0x1000;
+ public static int MOD_KEYDOWN = 0x0000;
}
}
Modified: Trunk/Source/Roam.Test/Utils/HookTest.Designer.cs
===================================================================
--- Trunk/Source/Roam.Test/Utils/HookTest.Designer.cs 2007-09-13 08:31:36 UTC (rev 93)
+++ Trunk/Source/Roam.Test/Utils/HookTest.Designer.cs 2007-09-15 07:41:18 UTC (rev 94)
@@ -30,10 +30,14 @@
private void InitializeComponent()
{
this.mainMenu1 = new System.Windows.Forms.MainMenu();
- this.scrollableLabel1 = new Roam.UI.Controls.ScrollableLabel();
this.menuItem1 = new System.Windows.Forms.MenuItem();
this.menuItem2 = new System.Windows.Forms.MenuItem();
this.menuItem3 = new System.Windows.Forms.MenuItem();
+ this.menuItem4 = new System.Windows.Forms.MenuItem();
+ this.textBox2 = new Roam.UI.Controls.TextBox();
+ this.textBox1 = new Roam.UI.Controls.TextBox();
+ this.scrollableLabel1 = new Roam.UI.Controls.ScrollableLabel();
+ this._menuItemFilterAllExceptBackKey = new System.Windows.Forms.MenuItem();
this.SuspendLayout();
//
// mainMenu1
@@ -41,16 +45,6 @@
this.mainMenu1.MenuItems.Add(this.menuItem1);
this.mainMenu1.MenuItems.Add(this.menuItem2);
//
- // scrollableLabel1
- //
- this.scrollableLabel1.Dock = System.Windows.Forms.DockStyle.Fill;
- this.scrollableLabel1.Font = new System.Drawing.Font("Segoe Condensed", 7F, System.Drawing.FontStyle.Regular);
- this.scrollableLabel1.Location = new System.Drawing.Point(0, 0);
- this.scrollableLabel1.Name = "scrollableLabel1";
- this.scrollableLabel1.Size = new System.Drawing.Size(240, 266);
- this.scrollableLabel1.TabIndex = 1;
- this.scrollableLabel1.Text = "scrollableLabel1";
- //
// menuItem1
//
this.menuItem1.Text = "Exit";
@@ -59,6 +53,8 @@
// menuItem2
//
this.menuItem2.MenuItems.Add(this.menuItem3);
+ this.menuItem2.MenuItems.Add(this.menuItem4);
+ this.menuItem2.MenuItems.Add(this._menuItemFilterAllExceptBackKey);
this.menuItem2.Text = "Menu";
//
// menuItem3
@@ -66,12 +62,49 @@
this.menuItem3.Text = "Clear";
this.menuItem3.Click += new System.EventHandler(this.menuItem3_Click);
//
+ // menuItem4
+ //
+ this.menuItem4.Text = "Capture for 10 Seconds";
+ this.menuItem4.Click += new System.EventHandler(this.menuItem4_Click);
+ //
+ // textBox2
+ //
+ this.textBox2.Location = new System.Drawing.Point(3, 39);
+ this.textBox2.Name = "textBox2";
+ this.textBox2.Size = new System.Drawing.Size(234, 30);
+ this.textBox2.TabIndex = 6;
+ this.textBox2.Text = "textBox2";
+ //
+ // textBox1
+ //
+ this.textBox1.Location = new System.Drawing.Point(3, 3);
+ this.textBox1.Name = "textBox1";
+ this.textBox1.Size = new System.Drawing.Size(234, 30);
+ this.textBox1.TabIndex = 5;
+ this.textBox1.Text = "textBox1";
+ //
+ // scrollableLabel1
+ //
+ this.scrollableLabel1.Font = new System.Drawing.Font("Segoe Condensed", 7F, System.Drawing.FontStyle.Regular);
+ this.scrollableLabel1.Location = new System.Drawing.Point(0, 122);
+ this.scrollableLabel1.Name = "scrollableLabel1";
+ this.scrollableLabel1.Size = new System.Drawing.Size(240, 144);
+ this.scrollableLabel1.TabIndex = 4;
+ this.scrollableLabel1.Text = "scrollableLabel1";
+ //
+ // _menuItemFilterAllExceptBackKey
+ //
+ this._menuItemFilterAllExceptBackKey.Text = "Filter All Except BackKey";
+ this._menuItemFilterAllExceptBackKey.Click += new System.EventHandler(this.menuItem5_Click);
+ //
// HookTest
//
this.AutoScaleDimensions = new System.Drawing.SizeF(131F, 131F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Dpi;
this.AutoScroll = true;
this.ClientSize = new System.Drawing.Size(240, 266);
+ this.Controls.Add(this.textBox2);
+ this.Controls.Add(this.textBox1);
this.Controls.Add(this.scrollableLabel1);
this.Menu = this.mainMenu1;
this.Name = "HookTest";
@@ -86,5 +119,9 @@
private System.Windows.Forms.MenuItem menuItem1;
private System.Windows.Forms.MenuItem menuItem2;
private System.Windows.Forms.MenuItem menuItem3;
+ private System.Windows.Forms.MenuItem menuItem4;
+ private Roam.UI.Controls.TextBox textBox1;
+ private Roam.UI.Controls.TextBox textBox2;
+ private System.Windows.Forms.MenuItem _menuItemFilterAllExceptBackKey;
}
}
\ No newline at end of file
Modified: Trunk/Source/Roam.Test/Utils/HookTest.cs
===================================================================
--- Trunk/Source/Roam.Test/Utils/HookTest.cs 2007-09-13 08:31:36 UTC (rev 93)
+++ Trunk/Source/Roam.Test/Utils/HookTest.cs 2007-09-15 07:41:18 UTC (rev 94)
@@ -6,12 +6,15 @@
using System.Text;
using System.Windows.Forms;
using Roam.UI.Utils;
+using Microsoft.WindowsCE.Forms;
namespace Roam.Test.Utils
{
public partial class HookTest : Form
{
private Hook _hook = new Hook();
+ private EscapeKeyFilter _escapeKeyFilter = new EscapeKeyFilter();
+
public HookTest()
{
InitializeComponent();
@@ -20,10 +23,26 @@
private void _hook_WndProcEvent(object sender, WndProcEventArgs e)
{
- e.Handled = true;
- scrollableLabel1.AddLine(
- string.Format("AttachedHwnd={0} OldWndProc={1} HWnd={2} Msg={3} WParam={4} LParam={5}",
- e.AttachedHwnd, e.OldWndProc, e.HWnd, e.Msg, e.WParam, e.LParam));
+ e.Handled = _captureAllEvents;
+
+ Message m = e.Message;
+ bool isHotKey = WindowsMessagingUtils.IsHotKeyMessage(m);
+ bool isInput = WindowsMessagingUtils.IsInputMessage(m);
+ bool isHotKeyUp = WindowsMessagingUtils.IsHotKeyUp(m);
+ bool isHotKeyDown = WindowsMessagingUtils.IsHotKeyDown(m);
+ Keys extractedKey = WindowsMessagingUtils.ExtractKey(m);
+
+ if (isHotKey)
+ {
+ scrollableLabel1.AddLine("HotKey message detected. ExtractedKey={0} HWnd={1} Msg={2} WParam={3} LParam={4}",
+ extractedKey, e.HWnd, e.Msg, e.WParam, e.LParam);
+ }
+ else
+ {
+ scrollableLabel1.AddLine(
+ string.Format("AttachedHwnd={0} OldWndProc={1} HWnd={2} Msg={3} WParam={4} LParam={5}",
+ e.AttachedHwnd, e.OldWndProc, e.HWnd, e.Msg, e.WParam, e.LParam));
+ }
}
protected override void OnHandleCreated(EventArgs e)
@@ -41,5 +60,44 @@
{
scrollableLabel1.Clear();
}
+
+ private Timer _timer;
+ private void menuItem4_Click(object sender, EventArgs e)
+ {
+ if (_timer != null)
+ {
+ _timer.Dispose();
+ _timer = null;
+ }
+
+ scrollableLabel1.AddLine("Now capturing WndProc events for next 10 secs");
+ _captureAllEvents = true;
+ _timer = new Timer();
+ _timer.Tick += new EventHandler(_timer_Tick);
+ _timer.Interval = 10000;
+ _timer.Enabled = true;
+ }
+
+ private bool _captureAllEvents = false;
+ void _timer_Tick(object sender, EventArgs e)
+ {
+ _timer.Enabled = false;
+ _captureAllEvents = false;
+ scrollableLabel1.AddLine("Finished capturing WndProc events");
+ }
+
+ private void menuItem5_Click(object sender, EventArgs e)
+ {
+ _menuItemFilterAllExceptBackKey.Checked = !_menuItemFilterAllExceptBackKey.Checked;
+
+ if (_menuItemFilterAllExceptBackKey.Checked)
+ {
+ _hook.MessageFilter = _escapeKeyFilter;
+ }
+ else
+ {
+ _hook.MessageFilter = null;
+ }
+ }
}
}
\ No newline at end of file
Modified: Trunk/Source/Roam.UI/Utils/Hook.cs
===================================================================
--- Trunk/Source/Roam.UI/Utils/Hook.cs 2007-09-13 08:31:36 UTC (rev 93)
+++ Trunk/Source/Roam.UI/Utils/Hook.cs 2007-09-15 07:41:18 UTC (rev 94)
@@ -8,6 +8,8 @@
using System.Windows.Forms;
using System.ComponentModel;
using System.Runtime.InteropServices;
+using Microsoft.WindowsCE.Forms;
+using Roam.Common.Utils;
namespace Roam.UI.Utils
{
@@ -21,9 +23,11 @@
public class Hook
{
#region Fields
+ private const int GWL_WNDPROC = -4;
private IntPtr _oldWndProc;
private IntPtr _hwnd;
private WndProcDelegate _newWndProc;
+ private IFilter<Message> _messageFilter;
public event EventHandler<WndProcEventArgs> WndProcEvent;
#endregion Fields
@@ -32,8 +36,23 @@
{
_newWndProc = new WndProcDelegate(WndProc);
}
+
+ public Hook(IFilter<Message> messageFilter)
+ : this()
+ {
+ _messageFilter = messageFilter;
+ }
+
#endregion Constructors
+ #region Properties
+ public IFilter<Message> MessageFilter
+ {
+ get { return _messageFilter; }
+ set { _messageFilter = value; }
+ }
+ #endregion Properties
+
#region Methods
public void Attach(Control c)
{
@@ -42,6 +61,10 @@
public void Attach(IntPtr hwnd)
{
+ if (_hwnd != IntPtr.Zero)
+ {
+ Detach();
+ }
_hwnd = hwnd;
_oldWndProc = WindowUtils.GetWindowLong(hwnd, GWL_WNDPROC);
if (_oldWndProc == IntPtr.Zero) throw new Win32Exception();
@@ -52,29 +75,55 @@
public void Detach()
{
int r = WindowUtils.SetWindowLong(_hwnd, GWL_WNDPROC, _oldWndProc);
+ _hwnd = IntPtr.Zero;
if (r == 0) throw new Win32Exception();
}
protected virtual int WndProc(IntPtr hWnd, uint msg, IntPtr wparam, IntPtr lparam)
{
- WndProcEventArgs wndProcEventArgs = new WndProcEventArgs(_hwnd, _oldWndProc, hWnd, msg, wparam, lparam);
- if (WndProcEvent != null)
+ Message message = Message.Create(hWnd, (int)msg, wparam, lparam);
+
+ if(_messageFilter == null || !_messageFilter.Filter(message))
{
- WndProcEvent(this, wndProcEventArgs);
+ WndProcEventArgs wndProcEventArgs = new WndProcEventArgs(_hwnd, _oldWndProc, message);
+ if (WndProcEvent != null)
+ {
+ WndProcEvent(this, wndProcEventArgs);
+ }
+
+ if (wndProcEventArgs.Handled == false)
+ {
+ return WindowUtils.CallWindowProc(_oldWndProc, hWnd, msg, wparam, lparam);
+ }
+ else
+ {
+ return 0;
+ }
}
- if (wndProcEventArgs.Handled == false)
+ return WindowUtils.CallWindowProc(_oldWndProc, hWnd, msg, wparam, lparam);
+ }
+ #endregion Methods
+ }
+
+ public class EscapeKeyFilter : IFilter<Message>
+ {
+ #region IFilter<Message> Members
+ /// <summary>
+ /// Filter the message if it's not a HotKey Keys.Back message
+ /// </summary>
+ /// <param name="message"></param>
+ /// <returns></returns>
+ public bool Filter(Message message)
+ {
+ if (WindowsMessagingUtils.IsHotKeyMessage(message))
{
- return WindowUtils.CallWindowProc(_oldWndProc, hWnd, msg, wparam, lparam);
+ Keys key = WindowsMessagingUtils.ExtractKey(message);
+ return key != Keys.Escape;
}
- else
- {
- return 0;
- }
+ return true;
}
- #endregion Methods
-
- const int GWL_WNDPROC = -4;
+ #endregion
}
public class WndProcEventArgs : EventArgs
@@ -82,29 +131,29 @@
#region Fields
private IntPtr _attachedHWnd;
private IntPtr _oldWndProc;
- private IntPtr _hWnd;
- private uint _msg;
- private IntPtr _wparam;
- private IntPtr _lparam;
+ private Message _message;
private bool _handled = false;
#endregion Fields
- public WndProcEventArgs(IntPtr attachedHwnd, IntPtr oldWndProc, IntPtr hWnd, uint msg, IntPtr wParam, IntPtr lParam)
+ public WndProcEventArgs(IntPtr attachedHwnd, IntPtr oldWndProc, Message message)
{
_attachedHWnd = attachedHwnd;
_oldWndProc = oldWndProc;
- _hWnd = hWnd;
- _msg = msg;
- _wparam = wParam;
- _lparam = lParam;
+ _message = message;
}
+ public WndProcEventArgs(IntPtr attachedHwnd, IntPtr oldWndProc, IntPtr hWnd, uint msg, IntPtr wParam, IntPtr lParam)
+ : this(attachedHwnd, oldWndProc, Message.Create(hWnd, (int)msg, wParam, lParam))
+ {
+ }
+
public IntPtr AttachedHwnd { get { return _attachedHWnd; } }
public IntPtr OldWndProc { get { return _oldWndProc; } }
- public IntPtr HWnd { get { return _hWnd; } }
- public uint Msg { get { return _msg; } }
- public IntPtr WParam { get { return _wparam; } }
- public IntPtr LParam { get { return _lparam; } }
+ public IntPtr HWnd { get { return _message.HWnd; } }
+ public int Msg { get { return _message.Msg; } }
+ public IntPtr WParam { get { return _message.WParam; } }
+ public IntPtr LParam { get { return _message.LParam; } }
+ public Message Message { get { return _message; } }
public bool Handled { get { return _handled; } set { _handled = value; } }
}
Modified: Trunk/Source/Roam.UI/Utils/WindowsMessagingUtils.cs
===================================================================
--- Trunk/Source/Roam.UI/Utils/WindowsMessagingUtils.cs 2007-09-13 08:31:36 UTC (rev 93)
+++ Trunk/Source/Roam.UI/Utils/WindowsMessagingUtils.cs 2007-09-15 07:41:18 UTC (rev 94)
@@ -17,6 +17,7 @@
using System.Text;
using Microsoft.WindowsCE.Forms;
using Roam.Utils;
+using System.Windows.Forms;
namespace Roam.UI.Utils
{
@@ -38,16 +39,50 @@
m.Msg == WindowsConstants.WM_HOTKEY);
}
+ public static bool IsKeyMessage(Message m)
+ {
+ return (m.Msg == WindowsConstants.WM_KEYDOWN ||
+ m.Msg == WindowsConstants.WM_KEYUP ||
+ m.Msg == WindowsConstants.WM_HOTKEY);
+ }
+
+ public static bool IsHotKeyMessage(Message m)
+ {
+ return m.Msg == WindowsConstants.WM_HOTKEY;
+ }
+
public static bool IsHotKeyUp(Message m)
{
if (m.Msg != WindowsConstants.WM_HOTKEY)
{
- throw new ArgumentException(String.Format("Message m ({0}) must be a WM_HOTKEY message",m.Msg));
+ return false;
}
int lparam = m.LParam.ToInt32();
int mod = lparam & 0x0000FFFF;
return (WindowsConstants.MOD_KEYUP & mod) == WindowsConstants.MOD_KEYUP;
}
+
+ public static bool IsHotKeyDown(Message m)
+ {
+ if (m.Msg != WindowsConstants.WM_HOTKEY)
+ {
+ return false;
+ }
+
+ int lparam = m.LParam.ToInt32();
+ int mod = lparam & 0x0000FFFF;
+ return mod == WindowsConstants.MOD_KEYDOWN;
+ }
+
+ public static Keys ExtractKey(Message m)
+ {
+ if (!IsKeyMessage(m)) { return Keys.None; }
+
+ int lparam = m.LParam.ToInt32();
+ int mod = (int)(lparam & 0xFFFF0000);
+ int key = mod >> 16;
+ return (Keys)key;
+ }
}
}
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|