using System;
using System.Runtime.InteropServices;
using UnityEngine;
public class MyWindow : MonoBehaviour
{
[DllImport("user32.dll")]
static extern IntPtr FindWindow(string strClassName, int nptWindowName);
[DllImport("user32.dll")] static extern IntPtr GetForegroundWindow();
[DllImport("user32.dll")] static extern bool GetWindowRect(IntPtr hWnd, ref RECT lpRect);
[StructLayout(LayoutKind.Sequential)]
private struct RECT
{
public int Left;
public int Top;
public int Right;
public int Bottom;
}
[DllImport("user32.dll")] static extern bool SetWindowPos(IntPtr hWnd, int hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);
[DllImport("user32.dll")] static extern IntPtr SetWindowLong(IntPtr hwnd, int _nIndex, int dwNewLong);
const uint SWP_SHOWWINDOW = 0x0040;
const int GWL_STYLE = -16;
const int WS_BORDER = 1;
Resolution[] resolutions;
private Rect screenPosition;
void Awake()
{
resolutions = Screen.resolutions;
witnOutBorder();
}
public Rect GetWindowInfo()
{
RECT rect = new RECT();
Rect targetRect = new Rect();
GetWindowRect(GetForegroundWindow(), ref rect);
targetRect.width = Mathf.Abs(rect.Right - rect.Left);
targetRect.height = Mathf.Abs(rect.Top - rect.Bottom);
targetRect.x = rect.Left;
targetRect.y = rect.Top;
return targetRect;
}
private int GetTaskBarHeight()
{
int taskbarHeight = 10;
IntPtr hWnd = FindWindow("Shell_TrayWnd", 0);
RECT rect = new RECT();
GetWindowRect(hWnd, ref rect);
taskbarHeight = (int)(rect.Bottom - rect.Top);
return taskbarHeight;
}
private void witnOutBorder()
{
screenPosition.width = resolutions[resolutions.Length - 1].width;
int currMaxScreenHeight = Screen.currentResolution.height - GetTaskBarHeight();
screenPosition.height = currMaxScreenHeight;
Screen.SetResolution((int)screenPosition.width, (int)screenPosition.height, false);
SetWindowLong(GetForegroundWindow(), GWL_STYLE, WS_BORDER);
bool result = SetWindowPos(GetForegroundWindow(), 0, 0, 0, (int)screenPosition.width, (int)screenPosition.height, SWP_SHOWWINDOW);
}
private void Setposition()
{
SetWindowLong(GetForegroundWindow(), GWL_STYLE, WS_BORDER);
bool result = SetWindowPos(GetForegroundWindow(), 0, 0, 0, resolutions[resolutions.Length - 1].width, resolutions[resolutions.Length - 1].height, SWP_SHOWWINDOW);
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 如何调用 DeepSeek 的自然语言处理 API 接口并集成到在线客服系统
· 【译】Visual Studio 中新的强大生产力特性
· 2025年我用 Compose 写了一个 Todo App