/// <summary> /// 测试副屏是否在主屏下方 /// </summary> void TestDown() { Vector2 mousePosition = Input.mousePosition; bool result = ValueAdjust.JudgeRange(Display.displays[0].systemHeight - mousePosition.y, TestMousePos[3].y, ErrorRange); string info = "Target:" + TestMousePos[3] + " Mouse:" + mousePosition + " 副屏在主屏下方:" + result + " \r\n"; Debug_T.Log(info); if (result) { SetPosition(0, 1);//下 } else { SetPosition(0, 0);//都不符合,放在主屏 } }
void Start() { Debug_T.Log(" displays(显示器数): " + Display.displays.Length + " \r\n"); string filePath = Application.streamingAssetsPath + "/Setting.xml"; if (File.Exists(filePath)) { XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load(filePath); XmlNodeList node = xmlDoc.SelectSingleNode("Data").ChildNodes; foreach (XmlElement x1 in node) { if (x1.Name == "UseSecondScreen") { UseSecondScreen = x1.InnerText.ContainIgnoreCase("True") || x1.InnerText == "1"; break; } else if (x1.Name == "SecondScreen") { UseSecondScreen = x1.InnerText.ContainIgnoreCase("True") || x1.InnerText == "1"; break; } } } if (Display.displays.Length > 1 && UseSecondScreen && this.enabled)//多显示器 { WindowWidth = Display.displays[1].systemWidth; WindowHeight = Display.displays[1].systemHeight; WindowPosX = Display.displays[0].systemWidth; WindowPosY = 0; ErrorRange = Mathf.Clamp(ErrorRange, 2, Mathf.Min(Display.displays[0].systemWidth, Display.displays[0].systemHeight, Display.displays[1].systemWidth, Display.displays[1].systemHeight) - 5); TestMousePos[0] = new Vector2Int(Display.displays[0].systemWidth + Display.displays[1].systemWidth / 2, Display.displays[1].systemHeight / 2); TestMousePos[1] = new Vector2Int(-Display.displays[1].systemWidth, Display.displays[1].systemHeight / 2); TestMousePos[2] = new Vector2Int(Display.displays[0].systemWidth / 2, -Display.displays[1].systemHeight / 2); TestMousePos[3] = new Vector2Int(Display.displays[0].systemWidth / 2, Display.displays[0].systemHeight + Display.displays[1].systemHeight / 2); if (Application.platform == RuntimePlatform.WindowsPlayer) { //HWndIntPtr = (IntPtr)WindowsAPI.User32.FindWindow(null, Application.productName);//工程名,非进程名。非英文会因为编码格式问题找不到窗口。可能误判同名窗口。 HWndIntPtr = GetProcessWnd(); //HWndIntPtr = WindowsAPI.User32.GetForegroundWindow();//仅检测前台窗体,不一定为Unity。 SetOverrideMousePos(TestMousePos[0].x, TestMousePos[0].y, true); Invoke(nameof(TestRight), 0.1f);//判定右 } } }
/// <summary> /// 测试副屏是否在主屏上方 /// </summary> void TestUp() { Vector2 mousePosition = Input.mousePosition; bool result = ValueAdjust.JudgeRange(Display.displays[0].systemHeight - mousePosition.y, TestMousePos[2].y, ErrorRange); string info = "Target:" + TestMousePos[2] + " Mouse:" + mousePosition + " 副屏在主屏上方:" + result + " \r\n"; Debug_T.Log(info); SetOverrideMousePos(TestMousePos[3].x, TestMousePos[3].y, true); if (result) { SetPosition(0, -1);//上 } else { Invoke(nameof(TestDown), 0.1f);//判定下 } }
/// <summary> /// 测试副屏是否在主屏左方 /// </summary> void TestLeft() { Vector2 mousePosition = Input.mousePosition; bool result = ValueAdjust.JudgeRange(mousePosition.x, TestMousePos[1].x, ErrorRange); string info = "Target:" + TestMousePos[1] + " Mouse:" + mousePosition + " 副屏在主屏左方:" + result + " \r\n"; Debug_T.Log(info); SetOverrideMousePos(TestMousePos[2].x, TestMousePos[2].y, true); if (result) { SetPosition(-1, 0);//左 } else { Invoke(nameof(TestUp), 0.1f);//判定上 } }
IEnumerator Setposition(int X, int Y) { Screen.SetResolution(WindowWidth, WindowHeight, false); yield return(new WaitForSeconds(0.01f)); OverrideMouse = false; WindowsAPI.User32.SetWindowLong(HWndIntPtr, GWL_STYLE, WS_POPUP);//无边框 X = Mathf.Clamp(X, -1, 1); Y = Mathf.Clamp(Y, -1, 1); switch (X + "," + Y) { case "-1,0": WindowPosX = -Display.displays[1].systemWidth; WindowPosY = 0; break; default: case "0,0": WindowPosX = 0; WindowPosY = 0; break; case "1,0": WindowPosX = Display.displays[0].systemWidth; WindowPosY = 0; break; case "0,1": WindowPosX = 0; WindowPosY = Display.displays[0].systemHeight; break; case "0,-1": WindowPosX = 0; WindowPosY = -Display.displays[1].systemHeight; break; } bool result = WindowsAPI.User32.SetWindowPos(HWndIntPtr, HWND_TOPMOST, WindowPosX, WindowPosY, WindowWidth, WindowHeight, SWP_SHOWWINDOW);//设置屏幕大小和位置 Debug_T.Log("副屏设置" + result); }