WPF、Winform嵌套Unity

36 篇文章 1 订阅
9 篇文章 0 订阅

 

嵌套了两个Unity程序,修复焦点丢失的问题,本文代码只演示嵌套一个,如果需要两个复制代码即可。

前端代码:

winform端直接用panel即可

wpf用法如下

<WindowsFormsHost Grid.Row="1">
      <form:Panel x:Name="left_unityHost" Resize="left_unityHost_Resize" AutoSizeMode="GrowAndShrink"></form:Panel>
</WindowsFormsHost>

后端核心代码

主要功能:

打开程序、设置大小位置、设置焦点

        /// <summary>
        /// 开启
        /// </summary>
        void left_Start()
        {
            KillProcess(left_exeName);
            left_process = new Process();
            left_process.StartInfo.FileName = left_ExePath;
            left_process.StartInfo.Arguments = "-parentHWND " + left_unityHost.Handle.ToInt32() + " " + Environment.CommandLine;
            left_process.StartInfo.UseShellExecute = true;
            left_process.StartInfo.CreateNoWindow = true;

            left_process.Start();

            left_process.WaitForInputIdle();
            EnumChildWindows(left_unityHost.Handle, left_WindowEnum, IntPtr.Zero);
        }

        private int left_WindowEnum(IntPtr hwnd, IntPtr lparam)
        {
            left_unityHWND = hwnd;
            ActivateUnityWindow(hwnd);
            return 0;
        }
        
        private void left_unityHost_Resize(object sender, EventArgs e)
        {
            MoveWindow(left_unityHWND, 0, 0, left_unityHost.Width, left_unityHost.Height, true);
        }

        private void ActivateUnityWindow(IntPtr hwnd)
        {
            SendMessage(hwnd, WM_ACTIVATE, WA_ACTIVE, IntPtr.Zero);
        }


 用到的api

        #region win接口

        [DllImport("User32.dll")]
        static extern bool MoveWindow(IntPtr handle, int x, int y, int width, int height, bool redraw);

        internal delegate int WindowEnumProc(IntPtr hwnd, IntPtr lparam);
        [DllImport("user32.dll")]
        internal static extern bool EnumChildWindows(IntPtr hwnd, WindowEnumProc func, IntPtr lParam);

        [DllImport("user32.dll")]
        static extern int SendMessage(IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam);

        private const int WM_ACTIVATE = 0x0006;
        private readonly IntPtr WA_ACTIVE = new IntPtr(1);
        private readonly IntPtr WA_INACTIVE = new IntPtr(0);


        /// <summary>   
        /// 获取鼠标的坐标   
        /// </summary>   
        /// <param name="lpPoint">传址参数,坐标point类型</param>   
        /// <returns>获取成功返回真</returns>   
        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        public static extern bool GetCursorPos(out System.Drawing.Point pt);
        [DllImport("User32", EntryPoint = "ClientToScreen", SetLastError = true, ExactSpelling = true, CharSet = CharSet.Auto)]
        [return: MarshalAs(UnmanagedType.Bool)]
        private static extern bool ClientToScreen(IntPtr hWnd, ref System.Drawing.Point pt);

        const int leftDown = 0x0001;
        [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
        private static extern short GetKeyState(int vKey);
        #endregion

修复鼠标焦点丢失

        private async void Update()
        {
            while (true)
            {
                short val = GetKeyState(leftDown);
                if (val < 0 || val == -127 || val == -128)
                {
                    System.Drawing.Point mouse_point;
                    GetCursorPos(out mouse_point);
                    Point mousePoint = new Point(mouse_point.X, mouse_point.Y);

                    System.Drawing.Point left_point = new System.Drawing.Point();
                    ClientToScreen(left_unityHWND, ref left_point);
                    Size left_size = new Size(left_unityHost.Width, left_unityHost.Height);
                    Rect left_rect = new Rect(new Point(left_point.X, left_point.Y), left_size);

                    System.Drawing.Point right_point = new System.Drawing.Point();
                    ClientToScreen(right_unityHWND, ref right_point);
                    Size right_size = new Size(right_unityHost.Width, right_unityHost.Height);
                    Rect right_rect = new Rect(new Point(right_point.X, right_point.Y), right_size);

                    if (left_rect.Contains(mousePoint))
                    {
                        //left_log.Text = "鼠标在左侧";
                        ActivateUnityWindow(left_unityHWND);
                    }
                    if (right_rect.Contains(mousePoint))
                    {
                        //left_log.Text = "鼠标在右侧";
                        ActivateUnityWindow(right_unityHWND);
                    }
                }

                await Task.Delay(10);
            }
        }

  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

呼呼突突

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值