WPF HwndHost keyboard focus
The chart area in the screenshot is a HwndHost control which hosts a native Win32 window (with it's own registered WNDCLASS) implemented in C++/CLI and drawn with Direct2D. The HwndHost is hosted in a WPF Border control.
The problem I have is that I can't set the keyboard focus to the hosted Win32 window. I want the focus to move to the hosted Win32 window when the used clicks on the chart area. I tried calling SetFocus on WM_LBUTTONDOWN, but that screws up the focus in the rest of the application.
Currently, even if I click on the Win32 window, the focus remains on the tree-view on the left, and if I press the up/down cursor keys, the tree-view will get them, not the chart window.
How do I make the hosted Win32 window receive keyboard input from when the user clicks on the chart area, until it clicks on another control (like the tree-view, or the toolbar)?
alt text http://dl.dropbox.com/u/190212/public/wpf_hwndhost.png
EDIT: Here's the C++/CLI code for the window host:
template <typename T>
inline T intPtrToPtr(IntPtr value)
{
return reinterpret_cast<T>(static_cast<void*>(value));
}
public ref class ChartWindowHost : public HwndHost, IKeyboardInputSink
{
private:
ChartWindow* chartWindow; // this is a C++ class doing the actual work
protected:
virtual HandleRef BuildWindowCore(HandleRef parent) override
{
chartWindow = new ChartWindow;
const HINSTANCE hInstance = intPtrToPtr<HINSTANCE>(Marshal::GetHINSTANCE(Assembly::GetExecutingAssembly()->GetModules()[0]));
const HWND parentWindow = intPtrToPtr<HWND>(parent.Handle);
chartWindow->Create(hInstance, parentWindow);
return HandleRef(this, IntPtr(chartWindow->GetHandle()));
}
virtual void DestroyWindowCore(HandleRef /*window*/) override
{
chartWin开发者_高级运维dow->Destroy();
delete chartWindow;
chartWindow = NULL;
}
};
Well, msdn says something about having to override WndProc()
on the HwndHost subclass...
精彩评论