HwndSource win32 integration with Ribbons and KeyTips
My situation is rather complex, but I've seen other people do this. Unfortunately, there is no mention of my specific problem. I am in the process of trying to integrate a WPF UserControl including a Ribbon into a Win32 host application. So far, everything has been working pretty neat (to my surprise, even). I am using HwndSource to create a raw Win32 child-window and embed this into a Win32 host top-level window. I have tried TabControls, Buttons, RadioButtons, WebBrowsers and everything inside the UserControl and they all are usable from within my Win32 host application. And, of course, there is a Ribbon control at the top row of the UserControl's Grid.
Here's basically what I am doing:
mHwnd = new HwndSource (
0, 0x40000000 | 0x10000000,
0,
left,
top,
width,
height,
"CIntegrationTest",
开发者_C百科 parentHandle);
mUserCtrl = new UserControl1 ();
mHwnd.RootVisual = mUserCtrl;
This works rather good. I have some other controls (like an edit and a few buttons) in UserControl1 below the Ribbon. When the keyboard focus is inside one of these controls, I can use Alt+ to access the KeyTips of the RibbonTabs and it's elements. But when the focus is not inside of the WPF UserControl, but some control from the Win32 host, the hotkeys do not work.
I had a look at the implementation of the hotkeys and found out that a class called KeyTipService (found in RibbonControlsLibrary\Microsoft\Windows\Controls\KeyTipService.cs) uses InputManager.Current.PostProcessInput and InputManager.Current.PreProcessInput to handle the KeyTip hotkeys.
Unfortunately, I'm totally out of ideas how I could forward the necessary key events from the Win32 application to the Ribbon. I don't even know how I could identify the messages that need to be forwarded.
Maybe someone here ran into a similar problem or has some more clue about WPF message processing and can help me or give some hints.
P.S.: I'm running Visual Studio 2010 Premium on Windows XP SP3 here and am using the Microsoft Ribbons for WPF from October 2010.
You should be able to pass unhandled key events from the Windows form into the WPF layer. Override OnKeyDown, OnKeyUp, and any other you might need. If not any of your Form1 controls handle the event, the Handled property of the KeyEventArgs will be false.
You can then convert the Forms.KeyEventArgs into a Controls.KeyEventArgs and pass it into the WPF layer. Make sure not to copy the Handled boolean, or else your WPF part will not handle it at all. :)
I have attached some sample code that illustrates the idea.
public partial class Form1 : System.Windows.Forms.Form
{
public Form1()
{
InitializeComponent();
}
protected override void OnKeyDown(System.Windows.Forms.KeyEventArgs e)
{
base.OnKeyDown(e);
if (!e.Handled)
{
mUserCtrl.OnKeyDown(/* convert e into a Controls.KeyEventArgs and pass it */);
}
}
}
精彩评论