using System;
using System.Runtime.InteropServices;
using UnityEngine;
namespace Chiva.Toolkit.Function.NoBorder
{
///
/// 系统工具类
///
public class WindowsTools
{
///
/// 获取应用对应进程句柄
///
private static IntPtr ProcessHandle;
#region 方法
///
/// 获取顶层窗口的句柄,不搜索子窗口
///
/// 类名
/// 窗口名
///
[DllImport("user32.dll")]
private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
///
/// 设置指定窗口的显示状态
///
///
///
///
[DllImport("user32.dll")]
public static extern bool ShowWindow(IntPtr hwnd, int nCmdShow);
///
/// 获取一个前台窗口的句柄(用户当前工作的窗口)
///
///
[DllImport("user32.dll")]
private static extern IntPtr GetForegroundWindow();
///
/// 用来改变指定窗口的属性
///
/// 窗口句柄及间接给出的窗口所属的类
/// 将设定的大于等于0的偏移值
/// 替换值
///
[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 bool ReleaseCapture();
///
/// 将指定的消息发送到一个或多个窗口
///
/// 指定要接收消息的窗口的句柄
/// 指定被发送的消息
/// 指定附加的消息特定信息
/// 指定附加的消息特定信息
///
[DllImport("user32.dll")]
private static extern bool SendMessage(IntPtr hwnd, int wMsg, int wParam, int IParam);
#endregion
#region 常量
///
/// 显示窗口
///
public const int SWP_SHOWWINDOW = 0x0040;
///
/// 边框样式
///
public const int GWL_STYLE = -16;
///
/// 创建一个单边框的窗口
///
public const int WS_BORDER = 1;
///
/// 该窗口是一个弹出窗口
///
public const int WS_POPUP = 0x800000;
///
/// 激活窗口并将其最小化
///
public const int SW_SHOWMINIMIZED = 2;
///
/// 激活窗口并将其最大化
///
public const int SW_SHOWMAXIMIZED = 3;
///
/// 当光标在一个窗口的非客户区同时按下鼠标左键时提交此消息
///
private const int WM_NCLBUTTONDOWN = 0xA1;
///
/// 当窗口被销毁时发送
///
private const int WM_DESTROY = 0x02;
///
/// 当用户在光标位于窗口的工作区时释放鼠标左键时发布
///
private const int WM_LBUTTONUP = 0x0202;
#endregion
#region 公开
///
/// 获取当前窗口句柄
///
/// 产品名称
public static void GetCurrentWindowHandle(string _productName)
{
IntPtr hWnd = FindWindow("UnityWndClass", _productName);
if (hWnd != null)
{
ProcessHandle = hWnd;
}
else
{
ProcessHandle = GetForegroundWindow();
}
}
private static string msg = "";
///
/// 最小化窗口
///
public static void SetMinWindows()
{
ShowWindow(ProcessHandle, SW_SHOWMINIMIZED);
}
///
/// 设置无边框窗口
///
///
public static void SetNoBorderWindow(Rect _rect)
{
//修改指定句柄窗口的样式、框体大小
SetWindowLong(ProcessHandle, GWL_STYLE, WS_POPUP);
//设置位置
SetWindowPos(
ProcessHandle,
0,
(int)_rect.x,
(int)_rect.y,
(int)_rect.width,
(int)_rect.height,
SWP_SHOWWINDOW);
}
///
/// 拖动窗口
///
public static void DragWindow()
{
ReleaseCapture();
SendMessage(ProcessHandle, WM_NCLBUTTONDOWN, WM_DESTROY, 0);
SendMessage(ProcessHandle, WM_LBUTTONUP, 0, 0);
}
#endregion
}
}