Focus Management and mouse wheel hooking on WinForms by using WPF
Someone asked very interesting question in MSDN forums. I was sure, that that’s kind of “I forgot something small”, however, I built small repro.
public partial class Window1 : System.Windows.Window { public Window1() { InitializeComponent(); UserControl1 uc = new UserControl1(); WindowsFormsHost wfh = new WindowsFormsHost(); wfh.Child = (System.Windows.Forms.Control)uc; ((Grid)this.Content).Children.Add(wfh); }public partial class UserControl1 : UserControl
{
public UserControl1()
{
InitializeComponent();
}const int WM_MOUSEWHEEL = 0x20A;
protected override void WndProc(ref Message m)
{
base.WndProc(ref m);
if (m.Msg == WM_MOUSEWHEEL)
label1.Text = (int)m.WParam > 0 ? “Scrolling up” : “Scrolling down”;
}
However, I noticed, that hosted WinForms control receives mouse notification only after it was clicked. What’s the ****, I thought. It was not reasonable for me, so I hooked into WindowsFormsHost.Loaded and UserControl.Load event to set focus manually by code.
....uc.Load += new EventHandler(uc_Load);....wfh.Loaded += new RoutedEventHandler(wfh_Loaded);void wfh_Loaded(object sender, RoutedEventArgs e) { FocusManager.SetFocusedElement((DependencyObject)sender, (IInputElement)sender); } void uc_Load(object sender, EventArgs e) { ((UserControl1)sender).Focus(); }
No effect. The user control inside WindowsFormsHost got not focus. Digging deeper, I found interesting article about focus management in WPF Interop application. So, using System.Windows.Interop.IKeyboardInputSink.TabInto(System.Windows.Input.TraversalRequest) solves the problem. Just one line of code and Interop control, hosted in WPF got the focus, that it requests.
((System.Windows.Interop.IKeyboardInputSink)sender).TabInto(new System.Windows.Input.TraversalRequest(FocusNavigationDirection.First));
Well, next time, I advice to MSDN doc team, to write small bullet and link to such simple solution, that has to occur (as for me) automatically, when I set focus to WindowsFormsHost WPF element.
October 8th, 2007 · Comments (7)
7 Responses to “Focus Management and mouse wheel hooking on WinForms by using WPF”
Leave a Reply
Discover other tags
My tools
- .NET Framework Detector
- Duplicate images finder
- Exchange Security Policy for Windows Mobile Devices Fix
- Gas Price Windows Vista SideBar gadget
- Israel Traffic Information Windows Vista SideBar gadget
- Localization fix for SAP ES Explorer for Visual Studio
- LocTester
- RTL and LTR in Windows Live Writer
- Silverlight controls library
- Snipping tool integration plugin for WLW
- USB FM receiver library
- Vista Battery Saver
- WebCam control for WPF
- Windows Live SkyDrive attachment for Windows Live Writer
- Wireless Migrator
- WPF Virtual Keyboard





January 1st, 2009 at 12:24 am
I have solved my problems!
January 1st, 2009 at 12:24 am
Tamir,
Your this blog is helpful to understand this technology. I also stuck at similar situation where I need to Use WinForms inside WPF application and need to handle MouseWheel for WinForm. I am able to get the call back into WinForm implemention by echanism that you mentioned in your blog but now I need the event information such as event position i.e. like Point position = e.GetPosition(this); where e is MouseWheelEventArgs. But here, on mouseWheel controls come at "protected override void WndProc(ref System.Windows.Forms.Message m)", where MouseWheelEventArgs is missing.
Any idea, how to get that?
January 1st, 2009 at 12:24 am
I have solved my problems!
Thanks for your article!
P.S. Do u want to see some magic ?
January 1st, 2009 at 12:24 am
I have done what mentioned above, but nothing works.
Please send me full sources at pizdetsblya@gmail.com.
I need help.
Thank you in advance.
January 1st, 2009 at 12:24 am
Someone asked very interesting question in MSDN forums. I was sure, that that's kind of "I forgot
January 14th, 2010 at 4:57 pm
Amazing, discovered your site on digg.Happy I finally tested it out. Unsure if its my Chrome browser,but sometimes when I visit your site, the fonts are really tiny? Anyway, love your post and will return.Bye
March 5th, 2010 at 6:52 am
[..] A little unrelated, but I quite simply liked this webpage post [..]