using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.Win32;
using System.Runtime.InteropServices;
using Microsoft.WindowsCE.Forms;
namespace MyApplicationCe
{
public partial class Form1 : Form
{
public const int USER = 0x0400;
public const int TEST1 = USER + 1;
//api
[DllImport("coredll.dll")]
private static extern int SendMessage
(
IntPtr hWnd, // handle to destination window
uint Msg, // message
uint wParam, // first message parameter
uint lParam // second message parameter
);
class WndProcHooker
{
public delegate int WndProcCallback(
IntPtr hwnd, uint msg, uint wParam, int lParam, ref bool handled);
private static Dictionary hwndDict =
new Dictionary();
private static Dictionary ctlDict =
new Dictionary();
public static void HookWndProc(
Control ctl, WndProcCallback callback, uint msg)
{
HookedProcInformation hpi = null;
if (ctlDict.ContainsKey(ctl))
hpi = ctlDict[ctl];
else if (hwndDict.ContainsKey(ctl.Handle))
hpi = hwndDict[ctl.Handle];
if (hpi == null)
{
hpi = new HookedProcInformation(ctl,
new Win32.WndProc(WndProcHooker.WindowProc));
ctl.HandleCreated += new EventHandler(ctl_HandleCreated);
ctl.HandleDestroyed += new EventHandler(ctl_HandleDestroyed);
ctl.Disposed += new EventHandler(ctl_Disposed);
if (ctl.Handle != IntPtr.Zero)
hpi.SetHook();
}
// stick hpi into the correct dictionary
if (ctl.Handle == IntPtr.Zero)
ctlDict[ctl] = hpi;
else
hwndDict[ctl.Handle] = hpi;
// add the message/callback into the message map
hpi.messageMap[msg] = callback;
}
static void ctl_Disposed(object sender, EventArgs e)
{
Control ctl = sender as Control;
if (ctlDict.ContainsKey(ctl))
ctlDict.Remove(ctl);
else
System.Diagnostics.Debug.Assert(false);
}
static void ctl_HandleDestroyed(object sender, EventArgs e)
{
// When the handle for a control is destroyed, we want to
// unhook its wndproc and update our lists
Control ctl = sender as Control;
if (hwndDict.ContainsKey(ctl.Handle))
{
HookedProcInformation hpi = hwndDict[ctl.Handle];
UnhookWndProc(ctl, false);
}
else
System.Diagnostics.Debug.Assert(false);
}
static void ctl_HandleCreated(object sender, EventArgs e)
{
Control ctl = sender as Control;
if (ctlDict.ContainsKey(ctl))
{
HookedProcInformation hpi = ctlDict[ctl];
hwndDict[ctl.Handle] = hpi;
ctlDict.Remove(ctl);
hpi.SetHook();
}
else
System.Diagnostics.Debug.Assert(false);
}
private static int WindowProc(
IntPtr hwnd, uint msg, uint wParam, int lParam)
{
if (hwndDict.ContainsKey(hwnd))
{
HookedProcInformation hpi = hwndDict[hwnd];
if (hpi.messageMap.ContainsKey(msg))
{
WndProcCallback callback = hpi.messageMap[msg];
bool handled = false;
int retval = callback(hwnd, msg, wParam, lParam, ref handled);
if (handled)
return retval;
}
return hpi.CallOldWindowProc(hwnd, msg, wParam, lParam);
}
System.Diagnostics.Debug.Assert(
false, "WindowProc called for hwnd we don't know about");
return Win32.DefWindowProc(hwnd, msg, wParam, lParam);
}
public static void UnhookWndProc(Control ctl, uint msg)
{
// look for the HookedProcInformation in the control and hwnd
// dictionaries
HookedProcInformation hpi = null;
if (ctlDict.ContainsKey(ctl))
hpi = ctlDict[ctl];
else if (hwndDict.ContainsKey(ctl.Handle))
hpi = hwndDict[ctl.Handle];
// if we couldn't find a HookedProcInformation, throw
if (hpi == null)
throw new ArgumentException("No hook exists for this control");
// look for the message we are removing in the messageMap
if (hpi.messageMap.ContainsKey(msg))
hpi.messageMap.Remove(msg);
else
// if we couldn't find the message, throw
throw new ArgumentException(
string.Format(
"No hook exists for message ({0}) on this control",
msg));
}
public static void UnhookWndProc(Control ctl, bool disposing)
{
HookedProcInformation hpi = null;
if (ctlDict.ContainsKey(ctl))
hpi = ctlDict[ctl];
else if (hwndDict.ContainsKey(ctl.Handle))
hpi = hwndDict[ctl.Handle];
if (hpi == null)
throw new ArgumentException("No hook exists for this control");
// If we found our HookedProcInformation in ctlDict and we are
// disposing remove it from ctlDict
if (ctlDict.ContainsKey(ctl) && disposing)
ctlDict.Remove(ctl);
// If we found our HookedProcInformation in hwndDict, remove it
// and if we are not disposing stick it in ctlDict
if (hwndDict.ContainsKey(ctl.Handle))
{
hpi.Unhook();
hwndDict.Remove(ctl.Handle);
if (!disposing)
ctlDict[ctl] = hpi;
}
}
class HookedProcInformation
{
///
/// The message map for the window
///
public Dictionary messageMap;
///
/// The old window procedure for the window
///
private IntPtr oldWndProc;
///
/// The delegate that gets called in place of this window's
/// wndproc.
///
private Win32.WndProc newWndProc;
///
/// Control whose wndproc we are hooking
///
private Control control;
///
/// Constructs a new HookedProcInformation object
///
/// The handle to the window being hooked
/// The window procedure to replace the
/// original one for the control
public HookedProcInformation(Control ctl, Win32.WndProc wndproc)
{
control = ctl;
newWndProc = wndproc;
messageMap = new Dictionary();
}
///
/// Replaces the windows procedure for control with the
/// one specified in the constructor.
///
public void SetHook()
{
IntPtr hwnd = control.Handle;
if (hwnd == IntPtr.Zero)
throw new InvalidOperationException(
"Handle for control has not been created");
oldWndProc = Win32.SetWindowLong(hwnd, Win32.GWL_WNDPROC,
Marshal.GetFunctionPointerForDelegate(newWndProc));
}
///
/// Restores the original window procedure for the control.
///
public void Unhook()
{
IntPtr hwnd = control.Handle;
if (hwnd == IntPtr.Zero)
throw new InvalidOperationException(
"Handle for control has not been created");
Win32.SetWindowLong(hwnd, Win32.GWL_WNDPROC, oldWndProc);
}
public int CallOldWindowProc(
IntPtr hwnd, uint msg, uint wParam, int lParam)
{
return Win32.CallWindowProc(
oldWndProc, hwnd, msg, wParam, lParam);
}
}
}