7

What is the WinForms equivalent to the following line of WPF code?

HwndSource.FromHwnd(_windowHandle).AddHook(HwndSourceHookHandler);
Davide Piras
  • 43,984
  • 10
  • 98
  • 147
Chris
  • 2,148
  • 8
  • 31
  • 52

2 Answers2

6

In WinForms, you'd typically override WndProc in the control/Form in question. Since every control is effectively a HWND, you don't need the HwndSource style of hooking.


If you want to setup a Hook in C#, you can follow the guidelines in How to set a Windows hook in Visual C# .NET.

Reed Copsey
  • 554,122
  • 78
  • 1,158
  • 1,373
  • Thanks - I appreciate your input. I am looking to have a class, separate from the respective form, handle the registering of messages etc., where the constructor will take in an instance of the Window/Form, at which point I need to know how, using the window's/form's handle, to register the messages. Apologies if my initial post was not clear. – Chris Aug 29 '11 at 17:39
  • @Chris: Are you after how to setup a Windows Hook, ie: http://support.microsoft.com/kb/318804 – Reed Copsey Aug 29 '11 at 17:40
  • Also see: http://msdn.microsoft.com/en-us/library/ms632589(VS.85).aspx for full details on Hooks – Reed Copsey Aug 29 '11 at 17:41
  • Reed - yes - specifically curious about how to setup a Windows Hook. – Chris Aug 29 '11 at 17:46
  • @Chris: Okay - that first link (edited my answer to include it) walks through how to do it ;) – Reed Copsey Aug 29 '11 at 17:48
1

Inside your own process, you can use the Application.AddMessageFilter method to listen to specific events before they are dispatched anywhere. This doesn't work between processes.

I found this while searching for an answer to one of my questions about handling of mouse events between parent and child controls.

Community
  • 1
  • 1
Paul Williams
  • 16,585
  • 5
  • 47
  • 82