简体   繁体   中英

How to make an application running in a wpf window ignore clicks or mouse events

Currently I am trying to run an unreal 4 application in a wpf window. However I only want the users to see the window not click it or use it. I have tried all sorts of workarounds for this but none seem to actually work. Any help would be greatly appreciated. Thank you. Below is the code I'm using to run application in wpf window.

    [DllImport("user32.dll", EntryPoint="GetWindowThreadProcessId",  SetLastError=true,
         CharSet=CharSet.Unicode, ExactSpelling=true,
         CallingConvention=CallingConvention.StdCall)]
    private static extern long GetWindowThreadProcessId(long hWnd, long lpdwProcessId); 

    [DllImport("user32.dll", SetLastError=true)]
    private static extern IntPtr FindWindow (string lpClassName, string lpWindowName);

    [DllImport("user32.dll", SetLastError=true)]
    private static extern long SetParent (IntPtr hWndChild, IntPtr hWndNewParent);

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

    [DllImport("user32.dll", EntryPoint="SetWindowLongA", SetLastError=true)]
    public static extern int SetWindowLongA([System.Runtime.InteropServices.InAttribute()] System.IntPtr hWnd, int nIndex, int dwNewLong);

    [DllImport("user32.dll", SetLastError=true)]
    private static extern long SetWindowPos(IntPtr hwnd, long hWndInsertAfter, long x, long y, long cx, long cy, long wFlags);

    [DllImport("user32.dll", SetLastError=true)]
    private static extern bool MoveWindow(IntPtr hwnd, int x, int y, int cx, int cy, bool repaint);

    private const int SWP_NOOWNERZORDER = 0x200;
    private const int SWP_NOREDRAW = 0x8;
    private const int SWP_NOZORDER = 0x4;
    private const int SWP_SHOWWINDOW = 0x0040;
    private const int WS_EX_MDICHILD = 0x40;
    private const int SWP_FRAMECHANGED = 0x20;
    private const int SWP_NOACTIVATE = 0x10;
    private const int SWP_ASYNCWINDOWPOS = 0x4000;
    private const int SWP_NOMOVE = 0x2;
    private const int SWP_NOSIZE = 0x1;
    private const int GWL_STYLE = (-16);
    private const int WS_VISIBLE = 0x10000000;
    private const int WS_CHILD = 0x40000000;


    /// <summary>
    /// Force redraw of control when size changes
    /// </summary>
    /// <param name="e">Not used</param>
    protected void OnSizeChanged(object s, SizeChangedEventArgs e)
    {
        this.InvalidateVisual();

       this.Width =  WpfUserAppControl.Width = 200;
       this.Height =  WpfUserAppControl.Height = 200;

        //Application.Current.MainWindow.Width = Window.GetWindow(this.AppContainer).Width;


        MoveWindow(_appWin, 100, 100, (int)WpfUserAppControl.Height, (int)WpfUserAppControl.Width, true);
    }


    /// <summary>
    /// Create control when visibility changes
    /// </summary>
    /// <param name="e">Not used</param>
    protected void OnVisibleChanged(object s, RoutedEventArgs e)
    {
        // If control needs to be initialized/created
        if (_iscreated == false)
        {

            // Mark that control is created
            _iscreated = true;

            // Initialize handle value to invalid
            _appWin = IntPtr.Zero;

            try
            {
                var procInfo = new System.Diagnostics.ProcessStartInfo(this.exeName);
                procInfo.WorkingDirectory = System.IO.Path.GetDirectoryName(this.exeName);

                _childp = System.Diagnostics.Process.Start(procInfo);



                _childp.WaitForInputIdle();



                while (_childp.MainWindowHandle == IntPtr.Zero)
                {
                System.Threading.Thread.Sleep(10);

                }


                _appWin = _childp.MainWindowHandle;
            }
            catch (Exception ex)
            {
                Debug.Print(ex.Message + "Error");
            }

            // Put it into this form
            var helper = new WindowInteropHelper(Window.GetWindow(this.AppContainer));
            SetParent(_appWin, helper.Handle);
            AppControl.
            // Remove border and whatnot
           SetWindowLongA(_appWin, GWL_STYLE, WS_VISIBLE);

            // Move the window to overlay it on this window
            MoveWindow(_appWin, 100, 100, (int)WpfUserAppControl.Height, (int)WpfUserAppControl.Width, true);
           _childp.Exited += _childp_Exited;

           SetWindowPos(_appWin, -2, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOACTIVATE);
        }
    }

到目前为止,我发现的最佳解决方案是在加载透明的虚幻引擎之后运行另一个窗口应用程序。

Use IsHitTestVisible property from UIElement base object. Set False value to Window.IsHitTestVisible .

IsHitTestVisible = "False"

I hope it helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM