1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
|
using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using OpenNETCF.Windows.Forms;
using System.Windows.Forms;
namespace WindowHookWrapper
{
public struct HTCTOUCH_WPARAM
{
public byte Up; //0=KeyDown,1=KeyUp
public byte Where; //Where the click occurs (left pane, wheel, right pane)
//Pos for the Right Pane click
public byte xPosRP;
public byte yPosRP;
}
class WindowHooker : IDisposable
{
const int GW_WNDPROC = -4;
delegate int WndProcHandler(IntPtr hwnd, int message, int wParam, int lParam);
[DllImport("coredll")]
extern static IntPtr SetWindowLong(IntPtr hWnd, int dwId, IntPtr newLong);
[DllImport("coredll")]
extern static int CallWindowProc(IntPtr lpPrevWndFunc, IntPtr hWnd, int Msg, int wParam, int lParam);
[DllImport("coredll")]
extern static bool IsWindow(IntPtr hWnd);
IntPtr pOldProc;
IntPtr pHandle;
WndProcHandler WndProcH;
private int WndProc(IntPtr hWnd, int uMsg, int wParam, int lParam)
{
return CallWindowProc(pOldProc, hWnd, uMsg, wParam, lParam);
}
public WindowHooker(IntPtr handle)
{
pOldProc = IntPtr.Zero;
pHandle = IntPtr.Zero;
WndProcH = new WndProcHandler(WndProc);
if (handle != IntPtr.Zero)
{
HookWindow(handle);
}
}
public void UnHookWindow()
{
if( pOldProc != IntPtr.Zero && pHandle != IntPtr.Zero )
{
if( IsWindow( pHandle ) )
{
try
{
SetWindowLong(pHandle, GW_WNDPROC, pOldProc);
}
catch
{
// Unable to setwindowlong...
}
}
}
pOldProc = IntPtr.Zero;
pHandle = IntPtr.Zero;
}
public void HookWindow(IntPtr handle)
{
if ((pOldProc == IntPtr.Zero) && (pHandle == IntPtr.Zero))
{
if (IsWindow(handle))
{
pOldProc = SetWindowLong(handle, GW_WNDPROC, Marshal.GetFunctionPointerForDelegate(WndProcH));
pHandle = handle;
}
}
else
{
UnHookWindow();
HookWindow(handle);
}
}
public void Dispose()
{
}
}
} |