WPF HwndHost keyboard focus

有些话、适合烂在心里 提交于 2019-12-21 21:58:08

问题


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;
    }
};

回答1:


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



来源:https://stackoverflow.com/questions/2844165/wpf-hwndhost-keyboard-focus

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!