最近做项目碰到这个问题 是通过WIN获取句柄来实现的,是要把程序设置成窗口模式 然后把边框去除 找到程序名称句柄把他显示在最前端
[DllImport("User32.dll")]
private static extern IntPtr SetWindowLong(IntPtr hwnd, int _nIndex, int dwNewLong);
[DllImport("user32.dll")]
private static extern bool SetWindowPos(IntPtr hWnd, int hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);
[DllImport("user32.dll")]
private static extern IntPtr GetForegroundWindow();
[DllImport("User32.dll", EntryPoint = "FindWindow", CharSet = CharSet.Auto)]
private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint = "SetForegroundWindow")]
public static extern bool SetForegroundWindow(IntPtr hWnd);
private const uint SWP_SHOWWINDOW = 0x0040;
private const int GWL_STYLE = -16;
private const int WS_BORDER = 1;
private IntPtr handler;
int m_Wight;
int m_Height;
private void Start()
{
#if !UNITY_EDITOR
string name = Application.productName;
handler = FindWindow(null, name);
if (handler == IntPtr.Zero){
handler = GetForegroundWindow();
}
SetForegroundWindow(handler);
SetWindowLong(handler, GWL_STYLE, WS_BORDER);
bool result = SetWindowPos(handler, -1, 0, 0, m_Wight, m_Height, SWP_SHOWWINDOW);
#endif
}
有几点注意事项 1.因为是多屏幕 所以代码里不能有 Screen.SetResolution(this.m_Wight, this.m_Height, true);这样程会在主屏幕(一个屏)中全屏显示 2.使用 Screen.SetResolution(this.m_Wight, this.m_Height, false)后 边框不能去除(我也不知道原因) 所以全局查找一遍删除全部的Screen.SetResolution();
|