using System;
using System.Collections.Generic;
using System.Windows.Forms;
namespace AddMessageFilterApp
{
static class Program
{
class MsgFilter : IMessageFilter
{
public boo…
[/Quote]
谁说的不支持?
楼主可以参考 :
http://download.csdn.net/source/484702
这有一个实例,是将键盘输入的记录到一个文本文件
using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.ComponentModel;
namespace Example
{
/// <summary>
/// The KeyboardListener is a static class that allows registering a number
/// of event handlers that you want to get called in case some keyboard key is pressed
/// or released. The nice thing is that this KeyboardListener is also active in case
/// the parent application is running in the back.
/// </summary>
[System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand, Name="SkipVerification")]
public class KeyboardListener
{
#region Private declarations
/// <summary>
/// The Window that intercepts Keyboard messages
/// </summary>
private static ListeningWindow s_Listener;
#endregion
#region Private methods
/// <summary>
/// The function that will handle all keyboard activity signaled by the ListeningWindow.
/// In this context handling means calling all registered subscribers for every key pressed / released.
/// </summary>
/// <remarks>
/// Inside this method the events could also be fired by calling
/// s_KeyEventHandler(null,new KeyEventArgs(key,msg)) However, in case one of the registered
/// subscribers throws an exception, execution of the non-executed subscribers is cancelled.
/// </remarks>
/// <param name="key"></param>
/// <param name="msg"></param>
private static void KeyHandler(ushort key, uint msg)
{
if(s_KeyEventHandler != null)
{
Delegate[] delegates = s_KeyEventHandler.GetInvocationList();
foreach(Delegate del in delegates)
{
EventHandler sink = (EventHandler)del;
try
{
// This is a static class, therefore null is passed as the object reference
sink(null,new UniversalKeyEventArgs(key,msg));
}
// You can add some meaningful code to this catch block.
catch{};
}
}
}
#endregion
#region Public declarations
/// <summary>
/// An instance of this class is passed when Keyboard events are fired by the KeyboardListener.
/// </summary>
public class UniversalKeyEventArgs : KeyEventArgs
{
public readonly uint m_Msg;
public readonly ushort m_Key;
/// <summary>
/// For every application thread that is interested in keyboard events
/// an EventHandler can be added to this variable
/// </summary>
public static event EventHandler s_KeyEventHandler;
#endregion
#region Public methods
static KeyboardListener()
{
ListeningWindow.KeyDelegate aKeyDelegate = new ListeningWindow.KeyDelegate(KeyHandler);
s_Listener = new ListeningWindow(aKeyDelegate);
}
#endregion
#region Definition ListeningWindow class
/// <summary>
/// A ListeningWindow object is a Window that intercepts Keyboard events.
/// </summary>
private class ListeningWindow : NativeWindow
{
#region Declarations
public delegate void KeyDelegate( ushort key, uint msg );
#region Unsafe types
internal unsafe struct RAWINPUTDEV
{
public ushort usUsagePage;
public ushort usUsage;
public uint dwFlags;
public void* hwndTarget;
};
internal unsafe struct RAWINPUTHEADER
{
public uint dwType;
public uint dwSize;
public void* hDevice;
public void* wParam;
};
internal unsafe struct RAWINPUTHKEYBOARD
{
public RAWINPUTHEADER header;
public ushort MakeCode;
public ushort Flags;
public ushort Reserved;
public ushort VKey;
public uint Message;
public uint ExtraInformation;
};
#endregion
public ListeningWindow(KeyDelegate keyHandlerFunction)
{
m_KeyHandler = keyHandlerFunction;
// Find out the size of the buffer we have to provide
int res = GetRawInputData(m.LParam.ToPointer(), RID_INPUT, null, &dwSize, sizeof_RAWINPUTHEADER);
if(res == 0)
{
// Allocate a buffer and ...
byte* lpb = stackalloc byte[(int)dwSize];
// ... get the data
receivedBytes = (uint)GetRawInputData((RAWINPUTHKEYBOARD*)(m.LParam.ToPointer()), RID_INPUT, lpb, &dwSize, sizeof_RAWINPUTHEADER);
if ( receivedBytes == dwSize )
{
RAWINPUTHKEYBOARD* keybData = (RAWINPUTHKEYBOARD*)lpb;
// Call the delegate in case data satisfies
m_KeyHandler(keybData->VKey,keybData->Message);
}
}
}
else
{
string errMsg = string.Format("WndProc::GetRawInputData (2) received {0} bytes while expected {1} bytes", receivedBytes, dwSize);
throw new Exception(errMsg);
}
}
else
{
string errMsg = string.Format("WndProc::GetRawInputData (1) returned non zero value ({0})", res);
throw new Exception(errMsg);
}
}
}
catch {throw;}
}
break;
}
// In case you forget this you will run into problems
base.WndProc(ref m);
}