WinForms等效于WPF WindowInteropHelper,HwndSource,HwndSourceHook [英] WinForms equivalent of WPF WindowInteropHelper, HwndSource, HwndSourceHook
问题描述
我有一段代码,例如:
IntPtr hWnd = new WindowInteropHelper(this).Handle;
HwndSource source = HwndSource.FromHwnd(hWnd);
source.AddHook(new HwndSourceHook(WndProc));
NativeMethods.PostMessage((IntPtr)NativeMethods.HWND_BROADCAST, NativeMethods.WM_CALL, IntPtr.Zero, IntPtr.Zero);
这最初是在WPF应用程序中.但是,我需要在WinForms应用程序中复制功能.另外,NativeMethods.PostMessage只是映射到user32.dll PostMessage:
This was originally in a WPF application. However, I need to replicate the functionality in a WinForms application. Also, NativeMethods.PostMessage just maps to user32.dll PostMessage:
[DllImport("user32")]
public static extern bool PostMessage(IntPtr hwnd, int msg, IntPtr wparam, IntPtr lparam);
我可以在WinForms应用程序中使用1到1等效的WindowInteropHelper/HwndSource/HwndSourceHook
吗?
Are there a 1 to 1 equivalents of WindowInteropHelper/HwndSource/HwndSourceHook
that I can use in my WinForms applications?
推荐答案
基本要点是:除了源代码中的AddHook
外,您不需要任何东西.每个WinForm都有一个方法GetHandle()
,它将为您提供Window/Form的句柄(您已经自己找到了PostMessage
).
The basic point is: you don't need anything except AddHook
from your source. Each WinForm has a method GetHandle()
which will give you the handle of the Window/Form (and you found PostMessage
already by yourself).
太翻译了AddHook
,您要么编写自己的类来实现IMessageFilter
(1),要么重写WndProc()
(2).
(1)会接收应用程序范围内的消息,无论您以哪种形式发送它们,而(2)仅接收覆盖该方法的特定表单的消息.
Too translate AddHook
you either write your own class implementing IMessageFilter
(1) or you override WndProc()
(2).
(1) will receive messages application-wide, regardless to which form you send them while (2) only receives messages for the specific form overriding the method.
我找不到与WM_CALL
有关的任何内容,因为您必须将窗口消息指定为整数(通常为十六进制),所以这取决于您.
I could'nt find anything regarding WM_CALL
, as you have to specify the window message as an integer (usually in hex), so this is up to you.
(1):
using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;
public partial class Form1 : Form
{
[DllImport("user32")]
public static extern bool PostMessage(IntPtr hwnd, int msg, IntPtr wparam, IntPtr lparam);
//private const int WM_xxx = 0x0;
//you have to know for which event you wanna register
public Form1()
{
InitializeComponent();
IntPtr hWnd = this.Handle;
Application.AddMessageFilter(new MyMessageFilter());
PostMessage(hWnd, WM_xxx, IntPtr.Zero, IntPtr.Zero);
}
}
class MyMessageFilter : IMessageFilter
{
//private const int WM_xxx = 0x0;
public bool PreFilterMessage(ref Message m)
{
if (m.Msg == WM_xxx)
{
//code to handle the message
}
return false;
}
}
(2):
using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;
public partial class Form 1 {
[DllImport("user32")]
public static extern bool PostMessage(IntPtr hwnd, int msg, IntPtr wparam, IntPtr lparam);
//private const int WM_xxx = 0x0;
//you have to know for which event you wanna register
public Form1()
{
InitializeComponent();
IntPtr hWnd = this.Handle;
PostMessage(hWnd, WM_xxx, IntPtr.Zero, IntPtr.Zero);
}
protected override void WndProc(ref Message m)
{
if (m.Msg == WMK_xxx)
{
//code to handle the message
}
}
}
这篇关于WinForms等效于WPF WindowInteropHelper,HwndSource,HwndSourceHook的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!