211 lines
6.8 KiB
C#
211 lines
6.8 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using System.Runtime.InteropServices;
|
|
using System;
|
|
using static ResolutionTest;
|
|
using UnityEngine.XR;
|
|
using Unity.VisualScripting;
|
|
/*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);
|
|
|
|
[DllImport("user32.dll", SetLastError = true)]
|
|
private static extern int GetSystemMetrics(int nIndex);
|
|
private static int SM_CXSCREEN = 0; //主屏幕分辨率宽度
|
|
private static int SM_CYSCREEN = 1; //主屏幕分辨率高度
|
|
private static int SM_CYCAPTION = 4; //标题栏高度
|
|
private static int SM_CXFULLSCREEN = 16; //最大化窗口宽度(减去任务栏)
|
|
private static int SM_CYFULLSCREEN = 17; //最大化窗口高度(减去任务栏)
|
|
|
|
//边框参数
|
|
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 ;
|
|
|
|
/// <summary>
|
|
/// 窗口的Z排序设置。
|
|
/// </summary>
|
|
public enum ZOrder
|
|
{
|
|
/// <summary>
|
|
/// 让窗口变为当时的最顶层,相当于给窗口设置了一个"置顶"标志,
|
|
/// 与其他有这个标志的窗口竞争最顶层的位置(鼠标点击可切换哪个窗口成为当时的最顶层),
|
|
/// 所有带这个标志的窗口处在所有不带这个标志的窗口的上面,离用户更近。
|
|
/// </summary>
|
|
TopMost = -1,
|
|
|
|
/// <summary>
|
|
/// 取消窗口的"置顶"标志,于是这个窗口就变成了普通窗口,置顶窗口们就不和它一起玩了,它之后便和其他普通窗口一桌竞争了。
|
|
/// 这个设置只对本来就是置顶窗口的窗口有用,对普通窗口没效果。
|
|
/// </summary>
|
|
NoTopMost = -2,
|
|
|
|
/// <summary>
|
|
/// 将窗口移动到普通窗口的顶部,依然处在置顶窗口们的下面,依然是普通窗口,不会一直待在顶部,会在以后鼠标点来点去的时候跑到其他窗口下面。
|
|
/// </summary>
|
|
Top = 0,
|
|
|
|
/// <summary>
|
|
/// 将窗口移动到普通窗口的底部。其他与Top同理。
|
|
/// </summary>
|
|
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);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 设置窗口置顶
|
|
/// </summary>
|
|
/// <param name="_hWnd"></param>
|
|
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);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 设置窗口置顶
|
|
/// </summary>
|
|
/// <param name="_hWnd"></param>
|
|
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);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 设置窗口右下
|
|
/// </summary>
|
|
/// <param name="_hWnd"></param>
|
|
public void right(IntPtr _hWnd)
|
|
{
|
|
// 屏幕分辨率
|
|
int x = GetSystemMetrics(SM_CXSCREEN);
|
|
int y = GetSystemMetrics(SM_CYSCREEN);
|
|
// 屏幕WorkingArea
|
|
int x1 = GetSystemMetrics(SM_CXFULLSCREEN);
|
|
int y1 = GetSystemMetrics(SM_CYFULLSCREEN);
|
|
// 标题栏高度
|
|
int title = GetSystemMetrics(SM_CYCAPTION);
|
|
SetWindowPos(_hWnd, -1, (int)(x-299), (int)(y1+ title - 203), (int)299, (int)203, SWP_SHOWWINDOW);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 设置屏幕原来的窗口
|
|
/// </summary>
|
|
/// <param name="_hWnd"></param>
|
|
public void old(IntPtr _hWnd)
|
|
{
|
|
// 屏幕分辨率
|
|
int x = GetSystemMetrics(SM_CXSCREEN);
|
|
int y = GetSystemMetrics(SM_CYSCREEN);
|
|
// 屏幕WorkingArea
|
|
int x1 = GetSystemMetrics(SM_CXFULLSCREEN);
|
|
int y1 = GetSystemMetrics(SM_CYFULLSCREEN);
|
|
// 标题栏高度
|
|
int title = GetSystemMetrics(SM_CYCAPTION);
|
|
|
|
SetWindowPos(_hWnd, -2, (int)100, (int)100, (int)1600, (int)900, SWP_SHOWWINDOW);
|
|
Debug.Log("设置窗口成功");
|
|
}
|
|
|
|
//拖动窗口
|
|
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;
|
|
}
|
|
}
|
|
|
|
|
|
|