| 型 | プロパティ | |
|---|---|---|
| IntPtr | HWnd | Gets or sets the window handle of the message. |
| IntPtr | LParam | Specifies the LParam field of the message. |
| int | Msg | Gets or sets the ID number for the message. |
| IntPtr | Result | Specifies the value that is returned to Windows in response to handling the message. |
| IntPtr | WParam | Gets or sets the WParam field of the message. |
LParamやWParamなどの型がIntPtrとなっていますが、実際のビット数はプラットフォーム依存のため、扱いには注意が必要です。
このインターフェイスをコントロールやフォームに実装することで、それらに送られるメッセージを捕捉できます。IMessageFilter インターフェイス (System.Windows.Forms) | Microsoft Learn
メッセージが送信 (dispatch) される前にフィルタリングできます。
[SecurityPermissionAttribute(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.UnmanagedCode)]
bool PreFilterMessage(
ref Message m
)
IMessageFilter.PreFilterMessage(Message) メソッド (System.Windows.Forms) | Microsoft Learn
このメソッドでメッセージを受け取るには、先にAddMessageFilter()でフィルタを登録する必要があります。メッセージをフィルタリングし、送信を停止するにはtrueを返します。
class MyClass : Form, IMessageFilter
{
protected override void OnHandleCreated(EventArgs e)
{
base.OnHandleCreated(e);
Application.AddMessageFilter(this); // フィルタを追加
}
protected override void OnHandleDestroyed(EventArgs e)
{
Application.RemoveMessageFilter(this); // フィルタを除去
base.OnHandleDestroyed(e);
}
public bool PreFilterMessage(ref Message m)
{
const int WM_KEYDOWN = 0x100;
if (m.Msg == WM_KEYDOWN)
{
Keys keyCode = (Keys)m.WParam & Keys.KeyCode;
if (keyCode == Keys.Escape)
{
//
return true;
}
}
return false;
}
}
| 戻り値の型 | メソッド | |
|---|---|---|
| public | PreProcessControlState | PreProcessControlMessage(ref Message msg) |
| public virtual | bool | PreProcessMessage(ref Message msg) |
| protected virtual | void | DefWndProc(ref Message m) |
| protected virtual | void | OnNotifyMessage(Message m) |
| protected virtual | bool | ProcessCmdKey(ref Message msg, Keys keyData) |
| protected virtual | bool | ProcessKeyEventArgs(ref Message m) |
| protected virtual | bool | ProcessKeyPreview(ref Message m) |
| protected virtual | void | WndProc(ref Message m) |
| protected internal virtual | bool | ProcessKeyMessage(ref Message m) |
| protected static | bool | ReflectMessage(IntPtr hWnd, ref Message m) |
Windowsメッセージを処理できます。
[SecurityPermissionAttribute(SecurityAction.InheritanceDemand, Flags = SecurityPermissionFlag.UnmanagedCode)]
[SecurityPermissionAttribute(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.UnmanagedCode)]
protected virtual void WndProc(
ref Message m
)
Control.WndProc(Message) メソッド (System.Windows.Forms) | Microsoft Learn
これを継承したコントロールでオーバーライドするときには、ハンドルされなかったメッセージを処理するために基本クラスのメソッドを呼び出します。さもなくば実行時に「ウィンドウのハンドルを作成中にエラーが発生しました。」としてSystem.ComponentModel.Win32Exception例外が発生します。Notes to Inheritors - Control.WndProc(Message) Method (System.Windows.Forms) | Microsoft Learn
private const int WM_CREATE = 0x0001;
protected override void WndProc(ref Message m)
{
switch (m.Msg)
{
case WM_CREATE:
break;
}
base.WndProc(ref m);
}
メッセージ ループ内のキーボードや入力メッセージを、ディスパッチ (dispatch) される前に処理できます。
[SecurityPermissionAttribute(SecurityAction.InheritanceDemand, Flags = SecurityPermissionFlag.UnmanagedCode)]
[SecurityPermissionAttribute(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.UnmanagedCode)]
public virtual bool PreProcessMessage(
ref Message msg
)
Control.PreProcessMessage(Message) メソッド (System.Windows.Forms) | Microsoft Learn
msgで処理するメッセージが渡されます、このとき有効な値は下表の4つです。
| メッセージ | |
|---|---|
| WM_KEYDOWN | WM_KEYDOWN message (Windows) | MSDN |
| WM_SYSKEYDOWN | |
| WM_CHAR | |
| WM_SYSCHAR |
コントロールでメッセージを処理したならば、trueを返します。
C++のSendMessage()などを、[DllImport("user32")]を用いて呼びます。
たとえばUnsafeNativeMethodsクラスのSendMessage()では、次のように宣言されています。
[DllImport(ExternDll.User32, CharSet=System.Runtime.InteropServices.CharSet.Auto)] [ResourceExposure(ResourceScope.None)] public static extern IntPtr SendMessage(HandleRef hWnd, int msg, IntPtr wParam, IntPtr lParam);