Keyboard WPF HwndHost

The chart area in the screenshot is an HwndHost control that contains its own Win32 window (with its own registered WNDCLASS), implemented in C ++ / CLI and drawn using Direct2D. HwndHost is hosted in WPF Edge Mode.

The problem is that I cannot adjust the keyboard focus to the hosted Win32 window. I want the focus to move to a hosted Win32 window when using clicks on a chart area. I tried calling SetFocus on WM_LBUTTONDOWN, but that dwarfs 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 arrow keys, the tree view will receive them, not the graph window.

How to make a hosted Win32 window receive keyboard input when a user clicks on a chart area until he clicks on another control (for example, on a tree or on a toolbar)?

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

EDIT: here is 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;
    }
};
+2
source share
1 answer

Well, msdn suggests the need to override WndProc()HwndHost in a subclass ...

0
source

Source: https://habr.com/ru/post/1751376/


All Articles