在WPF處理WndProc訊息
在Windows Forms中,處理WndProc訊息是一個簡單的過程。然而,在WPF中,方法略有不同。
利用HwndSource類別
WPF提供了System.Windows.Interop命名空間,其中包含HwndSource類別。此類別允許攔截和處理WndProc訊息。
範例實作
以下程式碼片段提供了一個使用HwndSource類別在WPF中處理WndProc訊息的範例:
<code class="language-c#">using System; using System.Windows; using System.Windows.Interop; namespace WpfApplication1 { public partial class Window1 : Window { public Window1() { InitializeComponent(); } protected override void OnSourceInitialized(EventArgs e) { base.OnSourceInitialized(e); HwndSource source = PresentationSource.FromVisual(this) as HwndSource; source.AddHook(WndProc); } private IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled) { // 在此处处理消息... return IntPtr.Zero; } } }</code>
在這個例子中:
更多資訊
有關此技術的更詳細說明,請參閱Steve Rands關於在WPF應用程式中使用自訂WndProc的優秀部落格文章。
以上是如何在 WPF 中處理 WndProc 訊息?的詳細內容。更多資訊請關注PHP中文網其他相關文章!