2010-05-16 129 views
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; 
    } 
}; 

回答

0

好,msdn了一些關於不必重寫WndProc()在HwndHost子...