0

私はまだ C# と WPF に慣れていません。学習演習として、アンマネージの win32 C++ アプリケーションで WPF の単純なラッパーを作成しています。HWNDHOST現在、WPF コントロールを使用してホストされているアンマネージド アプリケーションがありWM_INPUT、マウス入力のメッセージを受信して​​いますが、キーボード入力WM_KEYUP/DOWNに関しては、キーボードのメッセージしか受信していませんが、メッセージは受信していませんWM_INPUT

残念ながら、ホストされたアプリケーションは排他的に使用するため、キーボード入力システムが機能するにRawInputはメッセージが必要です。WM_INPUT

アプリをホストするための C# コードは次のとおりです。

class EngineHost : HwndHost
{
    #region Win32
    private const int SWP_NOZORDER = 0x0004;
    private const int SWP_NOACTIVATE = 0x0010;
    private const int GWL_STYLE = -16;
    private const int WS_CAPTION = 0x00C00000;
    private const int WS_THICKFRAME = 0x00040000;
    private const int WS_CHILD = 0x40000000;

    [DllImport("user32.dll")]
    private static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);

    [DllImport("user32.dll", SetLastError = true)]
    private static extern int GetWindowLong(IntPtr hWnd, int nIndex);

    [DllImport("user32")]
    private static extern IntPtr SetParent(IntPtr hWnd, IntPtr hWndParent);
    #endregion

    #region Members
    private Process _process;
    #endregion

    #region Implementation
    protected override HandleRef BuildWindowCore(HandleRef hwndParent)
    {
        ProcessStartInfo psi = new ProcessStartInfo("Kruger.exe");
        psi.WindowStyle = ProcessWindowStyle.Minimized;
        _process = Process.Start(psi);
        _process.WaitForInputIdle();

        // The main window handle may be unavailable for a while, just wait for it
        while (_process.MainWindowHandle == IntPtr.Zero)
        {
            Thread.Yield();
        }

        IntPtr engineHandle = _process.MainWindowHandle;

        int style = GetWindowLong(engineHandle, GWL_STYLE);
        style = style & ~((int)WS_CAPTION) & ~((int)WS_THICKFRAME); // Removes Caption bar and the sizing border
        style |= ((int)WS_CHILD); // Must be a child window to be hosted
        SetWindowLong(engineHandle, GWL_STYLE, style);

        SetParent(engineHandle, hwndParent.Handle);
        return new HandleRef(this, engineHandle);
    }

    protected override void DestroyWindowCore(HandleRef hwnd)
    {
        if (_process != null)
        {
            _process.CloseMainWindow();
            _process.WaitForExit(5000);

            if (_process.HasExited == false)
            {
                _process.Kill();
            }

            _process.Close();
            _process.Dispose();
            _process = null;
        }
    }
    #endregion
}

WM_INPUTホストされているアプリケーションに対してキーボード メッセージが生成されない理由と、それを修正する方法/修正方法を誰か教えてもらえますか?

4

0 に答える 0