C# winform嵌入unity3D


  最近做項目需要winform嵌入unity的功能,由於完全沒接觸過這類嵌入的於是在網上搜,有一種方法是UnityWebPlayer插件,也開始琢磨了一段時間,不過一會發現在5.4版本以后這個東西就被淘汰了,所以果斷放棄。后來有探索新方法,看到另一種使用代碼實現的方法,又開始鼓搗,結果鼓搗完發現也存在問題,但是沒有解決方案,於是有了這篇文章。

  問題:鼠標在winform中操作unity沒有反應。

  對比:兩台機器,A:win7 像素1440*900的老古董,B:win10 1920*1080的新科技。

  得出結論:在A機器上不好使,在B上好使。

  原因:屏幕分辨率惹的禍,unity的程序是1920*1080,放在A上可能會覆蓋掉unity的本身操作,從而導致無效果。

  解決辦法:unity程序默認啟動設置為全屏(去掉框框那種)。

  就這個問題看似很好弄,可是菜鳥的我整整鼓搗兩天,哎...。最開始以為是系統,插件,引用等問題,后來被一一排除,竟然是分辨率...,接下來看主要實現的代碼。

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace demo
{
    public class exetowinform
    {
        EventHandler appIdleEvent = null;
        Control ParentCon = null;
        string strGUID = "";

        public exetowinform(Control C, string Titlestr)
        {
            appIdleEvent = new EventHandler(Application_Idle);
            ParentCon = C;
            strGUID = Titlestr;
        }

        /// <summary>  
        /// 將屬性<code>AppFilename</code>指向的應用程序打開並嵌入此容器  
        /// </summary>  
        public IntPtr Start(string FileNameStr)
        {
            if (m_AppProcess != null)
            {
                Stop();
            }
            try
            {
                ProcessStartInfo info = new ProcessStartInfo(FileNameStr);
                info.UseShellExecute = true;
                info.WindowStyle = ProcessWindowStyle.Minimized;
                m_AppProcess = System.Diagnostics.Process.Start(info);
                m_AppProcess.WaitForInputIdle();
                Application.Idle += appIdleEvent;
            }
            catch
            {
                if (m_AppProcess != null)
                {
                    if (!m_AppProcess.HasExited)
                        m_AppProcess.Kill();
                    m_AppProcess = null;
                }
            }
            return m_AppProcess.Handle;
        }

     
        /// <summary>  
        /// 確保應用程序嵌入此容器  
        /// </summary>  
        /// <param name="sender"></param>  
        /// <param name="e"></param>  
        void Application_Idle(object sender, EventArgs e)
        {
            if (this.m_AppProcess == null || this.m_AppProcess.HasExited)
            {
                this.m_AppProcess = null;
                Application.Idle -= appIdleEvent;
                return;

            }

            Thread.Sleep(300);//這里加阻塞 ,時間可以大些
            Application.DoEvents();
            if (m_AppProcess.MainWindowHandle == IntPtr.Zero)
                return;
            Application.Idle -= appIdleEvent;
            EmbedProcess(m_AppProcess, ParentCon);
        }
        /// <summary>  
        /// 應用程序結束運行時要清除這里的標識  
        /// </summary>  
        /// <param name="sender"></param>  
        /// <param name="e"></param>  
        void m_AppProcess_Exited(object sender, EventArgs e)
        {
            m_AppProcess = null;
        }
        /// <summary>  
        /// 將屬性<code>AppFilename</code>指向的應用程序關閉  
        /// </summary>  
        public void Stop()
        {
            if (m_AppProcess != null)// && m_AppProcess.MainWindowHandle != IntPtr.Zero)  
            {
                try
                {
                    if (!m_AppProcess.HasExited)
                        m_AppProcess.Kill();
                }
                catch (Exception)
                {
                }
                m_AppProcess = null;
            }
        }
        #region 屬性  
        /// <summary>  
        /// application process  
        /// </summary>  
        Process m_AppProcess = null;

        /// <summary>  
        /// 標識內嵌程序是否已經啟動  
        /// </summary>  
        public bool IsStarted { get { return (this.m_AppProcess != null); } }

        #endregion 屬性  

        #region Win32 API  
        [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);

        public static IntPtr SetWindowLong(HandleRef hWnd, int nIndex, int dwNewLong)
        {
            if (IntPtr.Size == 4)
            {
                return SetWindowLongPtr32(hWnd, nIndex, dwNewLong);
            }
            return SetWindowLongPtr64(hWnd, nIndex, dwNewLong);
        }
        [DllImport("user32.dll", EntryPoint = "SetWindowLong", CharSet = CharSet.Auto)]
        public static extern IntPtr SetWindowLongPtr32(HandleRef hWnd, int nIndex, int dwNewLong);
        [DllImport("user32.dll", EntryPoint = "SetWindowLongPtr", CharSet = CharSet.Auto)]
        public static extern IntPtr SetWindowLongPtr64(HandleRef 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);

        [DllImport("user32.dll", EntryPoint = "PostMessageA", SetLastError = true)]
        private static extern bool PostMessage(IntPtr hwnd, uint Msg, uint wParam, uint lParam);

        [DllImport("user32.dll", SetLastError = true)]
        private static extern IntPtr GetParent(IntPtr hwnd);

        [DllImport("user32.dll", EntryPoint = "ShowWindow", SetLastError = true)]
        static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);

        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 WM_CLOSE = 0x10;
        private const int WS_CHILD = 0x40000000;

        private const int SW_HIDE = 0; //{隱藏, 並且任務欄也沒有最小化圖標}  
        private const int SW_SHOWNORMAL = 1; //{用最近的大小和位置顯示, 激活}  
        private const int SW_NORMAL = 1; //{同 SW_SHOWNORMAL}  
        private const int SW_SHOWMINIMIZED = 2; //{最小化, 激活}  
        private const int SW_SHOWMAXIMIZED = 3; //{最大化, 激活}  
        private const int SW_MAXIMIZE = 3; //{同 SW_SHOWMAXIMIZED}  
        private const int SW_SHOWNOACTIVATE = 4; //{用最近的大小和位置顯示, 不激活}  
        private const int SW_SHOW = 5; //{同 SW_SHOWNORMAL}  
        private const int SW_MINIMIZE = 6; //{最小化, 不激活}  
        private const int SW_SHOWMINNOACTIVE = 7; //{同 SW_MINIMIZE}  
        private const int SW_SHOWNA = 8; //{同 SW_SHOWNOACTIVATE}  
        private const int SW_RESTORE = 9; //{同 SW_SHOWNORMAL}  
        private const int SW_SHOWDEFAULT = 10; //{同 SW_SHOWNORMAL}  
        private const int SW_MAX = 10; //{同 SW_SHOWNORMAL}  

        #endregion Win32 API  

        /// <summary>  
        /// 將指定的程序嵌入指定的控件  
        /// </summary>  
        private void EmbedProcess(Process app, Control control)
        {
            // Get the main handle  
            if (app == null || app.MainWindowHandle == IntPtr.Zero || control == null) return;
            try
            {
                // Put it into this form  
                SetParent(app.MainWindowHandle, control.Handle);
            }
            catch (Exception)
            { }
            try
            {
                // Remove border and whatnot                 
                SetWindowLong(new HandleRef(this, app.MainWindowHandle), GWL_STYLE, WS_VISIBLE);
                SendMessage(app.MainWindowHandle, WM_SETTEXT, IntPtr.Zero, strGUID);
            }
            catch (Exception)
            { }
            try
            {
                // Move the window to overlay it on this window  
                MoveWindow(app.MainWindowHandle, 0, 0, control.Width, control.Height, true);
            }
            catch (Exception)
            { }
        }
        [DllImport("User32.dll", EntryPoint = "SendMessage")]
        private static extern int SendMessage(IntPtr hWnd, int Msg, IntPtr wParam, string lParam);

        const int WM_SETTEXT = 0x000C;
    }
}

調用方法:

    //panel1是在窗體中的控件名
    exetowinform fr =  new exetowinform(panel1, "");
    //打開unity生成的exe文件
    fr.Start(@"E:\Text.exe");


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM