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