WPF HwndHost键盘焦点 [英] WPF HwndHost keyboard focus

查看:477
本文介绍了WPF HwndHost键盘焦点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

截图中的图表区域是一个HwndHost控件,该控件承载了一个在C ++ / CLI中实现并使用Direct2D绘制的原生Win32窗口(具有自己的注册WNDCLASS)。 HwndHost被托管在一个WPF边框控件中。

我遇到的问题是我无法将键盘焦点设置到托管的Win32窗口。当用户点击图表区域时,我希望焦点转移到托管的Win32窗口。我试着在WM_LBUTTONDOWN上调用SetFocus,但是在应用程序的其余部分中,这会把焦点放在一边。



目前,即使我点击Win32窗口,焦点仍然在在左边的树视图,如果我按上/下光标键,树视图将得到他们,而不是图表窗口。



我如何使托管的Win32窗口接收从用户点击图表区域的键盘输入,直到它点击另一个控件(如树视图或工具栏)?

alt text http://dl.dropbox.com/u/190212/public /wpf_hwndhost.png

编辑:这是窗口主机的C ++ / CLI代码:

 模板< 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; //这是一个C ++类,做实际的工作

protected:
virtual HandleRef BuildWindowCore(HandleRef parent)覆盖
{
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()));

$ b $ virtual void DestroyWindowCore(HandleRef / * window * /)覆盖
{
chartWindow-> Destroy();
删除chartWindow;
chartWindow = NULL;
}
};


msdn 指出必须覆盖 WndProc()在HwndHost子类上...


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
    {
        chartWindow->Destroy();
        delete chartWindow;
        chartWindow = NULL;
    }
};

解决方案

Well, msdn says something about having to override WndProc() on the HwndHost subclass...

这篇关于WPF HwndHost键盘焦点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆