using System.Collections; using System.Collections.Generic; using UnityEngine; using System.Runtime.InteropServices; using System; using static ResolutionTest; using UnityEngine.XR; /*xbb * 系统方法类 * */ public class WindowsTools { //设置当前窗口的显示状态 [DllImport("user32.dll")] public static extern bool ShowWindow(System.IntPtr hwnd, int nCmdShow); //获取当前激活窗口 [DllImport("user32.dll", EntryPoint = "GetForegroundWindow")] public static extern System.IntPtr GetForegroundWindow(); //设置窗口边框 [DllImport("user32.dll")] public static extern IntPtr SetWindowLong(IntPtr hwnd, int _nIndex, int dwNewLong); //设置窗口位置,大小 [DllImport("user32.dll")] public static extern bool SetWindowPos(IntPtr hWnd, int hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags); //窗口拖动 [DllImport("user32.dll")] public static extern bool ReleaseCapture(); [DllImport("user32.dll")] public static extern bool SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam); [DllImport("user32.dll")] [return: MarshalAs(UnmanagedType.Bool)] public static extern bool GetWindowRect(IntPtr hWnd, ref RECT lpRect); [DllImport("user32.dll")] public static extern IntPtr GetWindowLong(IntPtr hWnd, int nIndex); //边框参数 const uint SWP_SHOWWINDOW = 0x0040; const int GWL_STYLE = -16; const int WS_BORDER = 1; const int WS_POPUP = 0x800000; const int SW_SHOWMINIMIZED = 2;//(最小化窗口) private const int WS_CAPTION = 0xC00000; private const int WS_SYSMENU = 0x80000; //private const int WS_MAXIMIZEBOX = &H10000 ; //private const int WS_MINIMIZEBOX = &H20000 ; //private const int WS_SYSMENU = &H80000 ; //private const int WS_CLIPSIBLINGS = &H4000000 ; //private const int WS_CLIPCHILDREN = &H2000000 ; //private const int WS_OVERLAPPED = &H0& ; // private const int WS_THICKFRAME = &H40000 ; /// /// 窗口的Z排序设置。 /// public enum ZOrder { /// /// 让窗口变为当时的最顶层,相当于给窗口设置了一个"置顶"标志, /// 与其他有这个标志的窗口竞争最顶层的位置(鼠标点击可切换哪个窗口成为当时的最顶层), /// 所有带这个标志的窗口处在所有不带这个标志的窗口的上面,离用户更近。 /// TopMost = -1, /// /// 取消窗口的"置顶"标志,于是这个窗口就变成了普通窗口,置顶窗口们就不和它一起玩了,它之后便和其他普通窗口一桌竞争了。 /// 这个设置只对本来就是置顶窗口的窗口有用,对普通窗口没效果。 /// NoTopMost = -2, /// /// 将窗口移动到普通窗口的顶部,依然处在置顶窗口们的下面,依然是普通窗口,不会一直待在顶部,会在以后鼠标点来点去的时候跑到其他窗口下面。 /// Top = 0, /// /// 将窗口移动到普通窗口的底部。其他与Top同理。 /// Bottom = 1, } public RECT GetWinRect(IntPtr _hWnd) { RECT rect = new RECT(); GetWindowRect(_hWnd, ref rect); return rect; } //最小化窗口 public void SetMinWindows(IntPtr _hWnd) { ShowWindow(_hWnd, SW_SHOWMINIMIZED); //具体窗口参数看这 https://msdn.microsoft.com/en-us/library/windows/desktop/ms633548(v=vs.85).aspx } public IntPtr GetWindowLongA(IntPtr _hWnd, int nIndex) { return GetWindowLong(_hWnd, nIndex); } private Rect current_rect; //设置无边框,并设置框体大小,位置 public void SetNoFrameWindow(IntPtr _hWnd, Rect rect) { current_rect = rect; SetWindowLong(_hWnd, GWL_STYLE, WS_POPUP); bool result = SetWindowPos(_hWnd, 0, (int)rect.x, (int)rect.y, (int)rect.width, (int)rect.height, SWP_SHOWWINDOW); } //设置有边框 public void SetFrameWindow(IntPtr _hWnd, Rect rect) { current_rect = rect; SetWindowLong(_hWnd, GWL_STYLE, 0x14ca0000); bool result = SetWindowPos(_hWnd, 0, (int)rect.x, (int)rect.y, (int)rect.width, (int)rect.height, SWP_SHOWWINDOW); } public void SetTopMost(IntPtr _hWnd, int _mode, Rect current_rect) { SetWindowPos(_hWnd, _mode, (int)current_rect.x, (int)current_rect.y, (int)current_rect.width, (int)current_rect.height, SWP_SHOWWINDOW); } /// /// 设置窗口置顶 /// /// public void SetTopMost(IntPtr _hWnd) { SetWindowPos(_hWnd, -1, (int)current_rect.x, (int)current_rect.y, (int)current_rect.width, (int)current_rect.height, SWP_SHOWWINDOW); } /// /// 设置窗口置顶 /// /// public void CancelTopMost(IntPtr _hWnd) { SetWindowPos(_hWnd, -2, (int)current_rect.x, (int)current_rect.y, (int)current_rect.width, (int)current_rect.height, SWP_SHOWWINDOW); } //拖动窗口 public void DragWindow(IntPtr _hWnd) { ReleaseCapture(); SendMessage(_hWnd, 0xA1, 0x02, 0); SendMessage(_hWnd, 0x0202, 0, 0); } public void DragWindowEnd(IntPtr _hWnd) { var _rect = GetWinRect(_hWnd); current_rect.x = _rect.Left; current_rect.y = _rect.Top; } }