WPF--How to handle WndProc messages in WPF?
来源:互联网 发布:java如何显示行数 编辑:程序博客网 时间:2024/04/28 04:12
How to handle WndProc messages in WPF?
Finding WPF a steep learning curve.
In good ol' Windows Forms, I'd just override WndProc, and start handling messages as they came in.
Can someone show me an example of how to acheive the same thing in WPF?
9 Answers
Actually, as far as I understand such a thing is indeed possible in WPF using HwndSource
and HwndSourceHook
. See this thread on MSDN as an example. (The code posted works fine - the question is about something slightly specialised.)
Now, I'm not quite sure why you'd want to handle Windows Messaging messages in a WPF application (unless it's the most obvious form of interop for working with another WinForms app). The design ideology and the nature of the API is very different in WPF from WinForms, so I would suggest you just familiarise yourself with WPF more to see exactly why there is no equivalent of WndProc.
You can do this via the System.Windows.Interop
namespace which contains a class named HwndSource
.
Example of using this
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)
{
// Handle messages...
return IntPtr.Zero;
}
}
}
Completely taken from the excellent blog post: Using a custom WndProc in WPF apps by Steve Rands
HwndSource src = HwndSource.FromHwnd(new WindowInteropHelper(this).Handle); src.AddHook(new HwndSourceHook(WndProc));
.......
public IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled) {
if(msg == THEMESSAGEIMLOOKINGFOR) { //Do something here }
return IntPtr.Zero; }
No disrespect to the other posters - but the world doesn't actually run on WPF and a lot of third party apps, especially C/C++ ones, use Windows Messaging as a way to broadcast data, or to add special hardware events. It's nice to say 'you don't need it with WPF', but that's both being shortsighted and not actually answering the question asked - especially if you don't actually explain WHY you don't need it in the context of the question asked.
You're thinking 'you don't need it', but to me, it reads 'WPF has a major defect'.
The short answer is you can't. WndProc works by passing messages to a HWND on a Win32 level. WPF windows have no HWND and hence can't participate in WndProc messages. The base WPF message loop does sit on top of WndProc but it abstracts them away from core WPF logic.
You can use a HWndHost and get at a WndProc for it. However this is almost certainly not what you want to do. For the majority of purposes, WPF does not operate on HWND and WndProc. Your solution almost certainly relies on making a change in WPF not in WndProc.
You can find another explanation for attaching to WndProc here.
There are ways to handle messages with a WndProc in WPF (e.g. using a HwndSource, etc.), but generally those techniques are reserved for interop with messages that can't directly be handled through WPF. Most WPF controls aren't even windows in the Win32 (and by extension Windows.Forms) sense, so they won't have WndProcs.
WPF doesn't operate on WinForms type wndprocs
You can host an HWndHost in an appropriate WPF element then override the Hwndhost's wndproc, but AFAIK that's as close as you're going to get.
http://msdn.microsoft.com/en-us/library/ms742522.aspx
http://blogs.msdn.com/nickkramer/archive/2006/03/18/554235.aspx
I have tried use the code on the MSDN thread example you gave only that I used WM_QUERYENDSESION. I was trying to imitate the behavior where user shutsdown/restart/logoff, my application propts for "Do you want to save changes?" then close the file i'm using. However nothing happens when I click yes/no, my application does not close. and if I try to close it again using the close button, I receive an error? why is this so? is it because of the added Hook??
- WPF--How to handle WndProc messages in WPF?
- How To Create Alternate Background In WPF?
- Wpf WndProc 消息处理
- How to Navigate, Group, Sort and Filter Data in WPF
- How to use App.Config in WPF applications
- WPF Window 中实现WndProc
- Enter-to-Tab in WPF
- How To Handle Row Selection in UITableView
- WPF 窗口句柄handle
- How to choose WPF container controls
- How to programmatically click a WPF button
- How to place all WPF commands and related events centrally in a static class
- How to automatically select all text on focus in WPF TextBox?
- How to close (auto hide) WPF window after 10 sec using a timer in C#?
- WinForm 和 WPF 里的 WndProc
- How to temporarily suppress infolog messages in Dynamics Ax (deux)
- How to with 7bit encoding in concatenated sms messages
- To check how many messages are in a MSMQ Queue
- Common Oracle RDBMS Misconceptions Datafiles aren't written during backup mode
- winsock IO 模型---重叠IO之事件通知 example code
- 因子分析
- restlet jax-rs配置
- 2012-01-13 关于时间那点事①
- WPF--How to handle WndProc messages in WPF?
- expr命令使用方法(转)
- 2012-01-13 关于时间那点事②
- USB 过程
- U-boot
- Exception(0x80028018)
- ASP.NET 2.0中轻松实现网站换肤
- Java线程stop和suspend的废弃
- Eclipse插件