2
屏幕截图中的图表区域是一个HwndHost控件,该控件承载了一个在C++/CLI中实现并使用Direct2D绘制的本机Win32窗口(使用它自己的注册WNDCLASS)。 HwndHost位于WPF边框控件中。WPF HwndHost键盘焦点
我遇到的问题是我无法将键盘焦点设置到托管的Win32窗口。我希望焦点转移到托管的Win32窗口,当使用点击图表区域时。我试图在WM_LBUTTONDOWN上调用SetFocus,但是这会在应用程序的其他部分中关注焦点。
目前,即使我点击Win32窗口,焦点仍保留在左侧的树形视图中,如果按下上/下光标键,树形视图将获取它们,而不是图表窗口。
如何使托管的Win32窗口从用户单击图表区域时开始接收键盘输入,直到它点击另一个控件(如树视图或工具栏)?
alt text http://dl.dropbox.com/u/190212/public/wpf_hwndhost.png
编辑:下面是窗口主机C++/CLI代码:
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
{
chartWindow->Destroy();
delete chartWindow;
chartWindow = NULL;
}
};