最近做项目需要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();//这里加阻塞 ,时间可以大些
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 == )
{
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 = (-);
private const int WS_VISIBLE = 0x10000000;
private const int WM_CLOSE = 0x10;
private const int WS_CHILD = 0x40000000; private const int SW_HIDE = ; //{隐藏, 并且任务栏也没有最小化图标}
private const int SW_SHOWNORMAL = ; //{用最近的大小和位置显示, 激活}
private const int SW_NORMAL = ; //{同 SW_SHOWNORMAL}
private const int SW_SHOWMINIMIZED = ; //{最小化, 激活}
private const int SW_SHOWMAXIMIZED = ; //{最大化, 激活}
private const int SW_MAXIMIZE = ; //{同 SW_SHOWMAXIMIZED}
private const int SW_SHOWNOACTIVATE = ; //{用最近的大小和位置显示, 不激活}
private const int SW_SHOW = ; //{同 SW_SHOWNORMAL}
private const int SW_MINIMIZE = ; //{最小化, 不激活}
private const int SW_SHOWMINNOACTIVE = ; //{同 SW_MINIMIZE}
private const int SW_SHOWNA = ; //{同 SW_SHOWNOACTIVATE}
private const int SW_RESTORE = ; //{同 SW_SHOWNORMAL}
private const int SW_SHOWDEFAULT = ; //{同 SW_SHOWNORMAL}
private const int SW_MAX = ; //{同 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, , , 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");

C# winform嵌入unity3D的更多相关文章

  1. winform展示Unity3D文件(支持动态改变文件路径)

    winform下展示Unity3D文件可以支持对Unity3D实现的模块进行包装,以及在其他的项目中需要展示Unity3D的界面时候,恰到适宜地进行打开展示,这里我展示如何使用winform打开Uni ...

  2. WPF 精修篇 Winform 嵌入WPF控件

    原文:WPF 精修篇 Winform 嵌入WPF控件 首先 创建WPF控件库 这样就有了一个WPF界面 在wpf中增加界面等 在winform中增加WPFDLL 重新生成解决方案 在左侧工具栏 出现W ...

  3. 如何将一个Winform嵌入到一个Control当中

    /// <summary> /// 将一个winform窗体嵌入control中 /// </summary> /// <param name="f" ...

  4. 在winform嵌入外部应用程序

    应朋友要求,需要将一个第三方应用程序嵌入到本程序WinForm窗口,以前在VB6时代做过类似的功能,其原理就是利用Windows API中FindWindow函数找到第三方应用程序句柄,再利用SetP ...

  5. 在winform中使用cefsharp.winform嵌入浏览器(含视频教程)

    免费视频教程和源码: https://www.bilibili.com/video/av84573813/ 1. 开始使用CefSharp在Winform中嵌入网页 2. 解决重复打开Cefsharp ...

  6. WinForm嵌入Web网页的解决方案

    企业级信息化系统绝大部分采用BS架构实现,如门户网站.OA系统.电商网站等,通过浏览器输入Web网址即可访问,对于使用者来说非常便捷,对于开发维护者来说也非常方便,程序维护只需更新服务器即可,使用者无 ...

  7. WPF嵌入Unity3D代码下载

    这不是大家想找的资源,这是给我的一个帖子用的.当然可以使用u3d自带编译器打开.这是给wpf 嵌入u3d的例子贴用http://bbs.csdn.net/topics/391853486?page=1 ...

  8. winform嵌入word解决方案一

    最近一直想用VC#2005做个内嵌WORD/OFFICE的WINFORM程序,目前主要有以下解决途径: 1.直接通过API把WORD/OFFICE的窗口句柄给放到WINFORM中(感觉较为复杂): 2 ...

  9. WPF嵌入Unity3D之后,unity3D程序的键盘和鼠标事件无法触发(3D程序的焦点无法激活)的解决方案

    目前最通用的客户端调用3D的方式,就是WPF程序通过Process启动Unity3D的exe进程,直接上代码: //开启3D进程 internal void Create3DProcess(strin ...

随机推荐

  1. python--第十二天总结(Python操作 RabbitMQ、Redis、Memcache、SQLAlchemy)

    Memcached Memcached 是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载.它通过在内存中缓存数据和对象来减少读取数据库的次数,从而提高动态.数据库驱动网站的速度 ...

  2. Git 基础 - 删除暂存区的文件

    1 直接删除文件 这个文件会有两个操作:已暂存的修改.未暂存的删除 2 直接删除文件 + 记录删除操作 先在工作目录中删除文件,再使用git rm命令记录此次移除文件的操作(删除暂存区中该文件的修改快 ...

  3. Python练习-迭代-2018.11.28

    #遍历list L=['a','b','c','d'] l=[] a=0 for n in L: l.insert(a,n) a=a+1 print(l) #遍历dict里的key,导出为list L ...

  4. BigDecimal用法总结

    BigDecimal用法总结 BigDecimal常用于金额的计算,下面总结下这次项目中BigDecimal的用法. 1.加减乘除 2.设置精度 3.取反 加减乘除分别调用函数 [java] view ...

  5. hdpi对应分辨率

    ldpi  QVGA (240×320) mdpi  HVGA (320×480) hdpi  WVGA (480×800),FWVGA (480×854) xhdpi  720P(1280*720) ...

  6. 微信小程序之 -----事件

    事件分类      1. 冒泡事件:     当一个组件上的事件被触发后,该事件会向父节点传递.      2. 非冒泡事件:   当一个组件上的事件被触发后,该事件不会向父节点传递.   常见的冒泡 ...

  7. ionic3使用第三方图标

    1.打开阿里图标库http://www.iconfont.cn 2.找到自己所需的图标,加入购物车(免费使用的) 3.打开购物车,点击右下角(下载代码) 4.解压文件,打开demo_unicode.h ...

  8. 批量屏蔽符合条件的IP地址,支持添加白名单,IP段,增量,大于指定次数的IP

    批量屏蔽符合条件的IP地址,支持添加白名单,IP段,增量 大概的思路是利用sh,从日志中提取出来对应的IP地址,然后再交由python进行对比,判断,最终将需要添加至iptables列表中的IP写入到 ...

  9. 黑马java课程2222

    课程叫做27天学通java零基础 java 安装: 必须装jdk-7u72-windows-i586.exe 注意必须安装32位的就是i586这个.因为x64的不向下兼容.会有意向不到的bug 配置P ...

  10. 阿里巴巴Java开发规约插件安装使用指南

    编码规范插件安装使用指南 阿里技术公众号于今年的2月9日首次公布<阿里巴巴Java开发规约>,瞬间引起全民代码规范的热潮,后又发布了PDF的终极版,大家踊跃留言,期待配套的静态扫描工具开放 ...