7

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

HwndSource.FromHwnd(_windowHandle).AddHook(HwndSourceHookHandler);
Davide Piras
  • 43,118
  • 10
  • 90
  • 143
Chris
  • 2,088
  • 8
  • 29
  • 50

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
  • 539,124
  • 75
  • 1,126
  • 1,354
  • 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,007
  • 5
  • 45
  • 81