开发者

C++/CLI : Interop window is not properly configured

I'm trying to load a WPF control in a C++/CLI application, using the HwndSource class.

UBOOL MyWindowWrapper::Init(const HWND InParentWindowHandle) {
    Interop::HwndSourceParameters sourceParams( "WindowName" );
    sourceParams.PositionX = 0;
    sourceParams.PositionY = 0;
    sourceParams.ParentWindow = (IntPtr)InParentWindowHandle;
    sourceParams.WindowStyle = (WS_VISIBLE | WS_CHILD);
    sourceParams.HwndSourceHook = nullptr;

    InteropWindow = gcnew Interop::HwndSource(sourceParams);

    Control = gcnew MyWPFUserControl();
    InteropWindow->RootVisual = Control;

    InteropWindow->AddHook( gcnew Interop::HwndSourceHook(
        this, &MyWindowWrapper::MessageHookFunction ) );
    return TRUE;
}

And I define a Hook function so the keyboard events are passed to the window :

IntPtr MyWindowWrapper::MessageHookFunction( IntPtr HWnd, int Msg,
        IntPtr WParam, IntPtr LParam, bool% OutHandled ) {
    IntPtr Result = (IntPtr)0;
    OutHandled = false;

    if( Msg == WM_GETDLGCODE ) {
        OutHandled = true;

        // This tells Windows that we'll need keyboard events for this control
        Result = IntPtr( DLGC_WANTALLKEYS | DLGC_WANTCHARS | DLGC_WANTMESSAGE );
    }
    return Result;
}

And here are my problems :

  • The window title is empty (so the "WindowName" parameter is not taken in account)
  • Only some keyboard events are transferred : space, control, arrows are ok, but I can't type any character in all the text boxes

What am I doing wrong开发者_如何学Python?


Regarding the empty title - this is because the name of the HwndSource window doesn't have anything to do with the window title. The HwndSource window that the code creates above is a child of the window represented by InParentWindowHandle. It is this window whose text is used for the title bar. You should call SetWindowText() using that HWND in order to set the title of the window.

As for the typing issue, are you sure that you need to add a hook? I've only created small test apps with WPF/Win32 integration, but I don't recall having to do anything special to ensure that the WPF controls received all keyboard input.


There is a known incompatibility in WPF interop within a native, modal dialogue.

Summarizing:

Due to implementation details, WM_CHAR messages are swallowed by a modal dialog's message pump in the IsDialogMessage() function before they get to your control. You can force yourself to retrieve these by handling WM_GETDLGCODE in your hook function and returning DLGC_WANTCHARS.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜