179 lines
5.5 KiB
C#
179 lines
5.5 KiB
C#
using System;
|
||
using System.Collections;
|
||
using System.Collections.Generic;
|
||
using System.Runtime.InteropServices;
|
||
using Unity.VisualScripting;
|
||
//using UnityEditor.PackageManager.UI;
|
||
using UnityEngine;
|
||
using UnityEngine.EventSystems;
|
||
using UnityEngine.UI;
|
||
using UnityEngine.XR;
|
||
|
||
public class ResolutionTest : MonoBehaviour
|
||
{
|
||
public InputField if1, if2;
|
||
public InputField if3, if4;
|
||
public Button bt1, bt2;
|
||
public Text txt;
|
||
|
||
IntPtr hwnd;
|
||
|
||
private void Start()
|
||
{
|
||
hwnd = GetForegroundWindow();
|
||
|
||
#if !UNITY_EDITOR
|
||
bt1.onClick.AddListener(() => SetResolution(int.Parse(if1.text), int.Parse(if2.text)));
|
||
bt2.onClick.AddListener(() => SetResolution(1600, 900));
|
||
|
||
SetWindowLong(hwnd, GWL_STYLE, WS_POPUP);
|
||
bool result = SetWindowPos(GetForegroundWindow(), 0, 0, 0, 1600, 900, SWP_SHOWWINDOW);
|
||
#endif
|
||
//GetRect();
|
||
}
|
||
|
||
RECT GetRect()
|
||
{
|
||
GetWindowRect(hwnd, out RECT rect);
|
||
string msg = rect.Left + " " + rect.Right + " " + rect.Top + " " + rect.Bottom;
|
||
txt.text = msg;
|
||
return rect;
|
||
}
|
||
|
||
int _posX, _posY, _Txtwith, _Txtheight;
|
||
|
||
void SetResolution(int width, int height)
|
||
{
|
||
Screen.SetResolution(width, height, false);
|
||
GetRect();
|
||
_Txtwith = width;
|
||
_Txtheight = height;
|
||
_posX = int.Parse(if3.text);
|
||
_posY = int.Parse(if4.text);
|
||
StartCoroutine("Setposition");
|
||
}
|
||
|
||
IEnumerator Setposition()
|
||
{
|
||
yield return new WaitForSeconds(0.1f); //不知道为什么发布于行后,设置位置的不会生效,我延迟0.1秒就可以
|
||
SetWindowLong(GetForegroundWindow(), GWL_STYLE, WS_POPUP); //无边框
|
||
bool result = SetWindowPos(GetForegroundWindow(), 0, _posX, _posY, _Txtwith, _Txtheight, SWP_SHOWWINDOW); //设置屏幕大小和位置
|
||
}
|
||
|
||
|
||
#region 窗口放大缩小
|
||
public int winPosX; //窗口左上角x
|
||
public int winPosY; //窗口左上角y
|
||
|
||
public int minWidth, maxWidth; //窗口宽度
|
||
public int minHeight, maxHeight; //窗口高度
|
||
|
||
[DllImport("user32.dll")]
|
||
static extern IntPtr SetWindowLong(IntPtr hwnd, int _nIndex, int dwNewLong);
|
||
[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 GetForegroundWindow();
|
||
|
||
const uint SWP_SHOWWINDOW = 0x0040;
|
||
const int GWL_STYLE = -16;
|
||
const int WS_BORDER = 1;
|
||
const int WS_POPUP = 0x800000;
|
||
|
||
[DllImport("user32.dll")]
|
||
static extern bool GetWindowRect(IntPtr hwnd, out RECT lpRect);
|
||
|
||
[StructLayout(LayoutKind.Sequential)]
|
||
public struct RECT
|
||
{
|
||
public int Left; //最左坐标
|
||
public int Top; //最上坐标
|
||
public int Right; //最右坐标
|
||
public int Bottom; //最下坐标
|
||
}
|
||
|
||
/// <summary>
|
||
/// 拖动窗口
|
||
/// </summary>
|
||
/// <param name="window">当前句柄</param>
|
||
public void DragWindow(IntPtr window)
|
||
{
|
||
ReleaseCapture();
|
||
SendMessage(window, 0xA1, 0x02, 0);
|
||
SendMessage(window, 0x0202, 0, 0);
|
||
}
|
||
|
||
public bool candrag;
|
||
|
||
//窗口拖动
|
||
[DllImport("user32.dll")]
|
||
public static extern bool ReleaseCapture();
|
||
[DllImport("user32.dll")]
|
||
public static extern bool SetCapture(IntPtr hwnd);
|
||
[DllImport("user32.dll")]
|
||
public static extern bool SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam);
|
||
|
||
private void Update()
|
||
{
|
||
if(Input.GetMouseButtonDown(0))
|
||
{
|
||
Debug.Log("dndndndndn");
|
||
candrag = !EventSystem.current.IsPointerOverGameObject();
|
||
Debug.Log(Input.mousePosition);
|
||
}
|
||
if (Input.GetMouseButton(0)&&candrag)
|
||
{
|
||
DragWindow(hwnd);
|
||
}
|
||
if (Input.GetMouseButtonUp(0)&& candrag|| Input.GetKeyDown(KeyCode.R))
|
||
{
|
||
Debug.Log("upupupup");
|
||
SetCapture(hwnd);
|
||
SendMessage(hwnd, 0xA2, 0x02, 0);
|
||
SendMessage(hwnd, 0x0202, 0, 0);
|
||
}
|
||
}
|
||
|
||
////获取当前激活窗口
|
||
//[DllImport("user32.dll", EntryPoint = "GetForegroundWindow")]
|
||
//public static extern System.IntPtr GetForegroundWindow();
|
||
|
||
private void WindowSizeEvent(bool ison)
|
||
{
|
||
if (ison)
|
||
{
|
||
//显示器支持的所有分辨率
|
||
int i = Screen.resolutions.Length;
|
||
|
||
int resWidth = Screen.resolutions[i - 1].width;
|
||
int resHeight = Screen.resolutions[i - 1].height;
|
||
|
||
winPosX = resWidth / 2 - minWidth / 2;
|
||
winPosY = resHeight / 2 - minHeight / 2;
|
||
|
||
SetWindowLong(GetForegroundWindow(), GWL_STYLE, WS_POPUP);
|
||
bool result = SetWindowPos(GetForegroundWindow(), 0, winPosX, winPosY, minWidth, minHeight, SWP_SHOWWINDOW);
|
||
|
||
//windowSize.GetComponent<Image>().sprite = icon_big;//修改图标
|
||
}
|
||
else
|
||
{
|
||
//显示器支持的所有分辨率
|
||
int i = Screen.resolutions.Length;
|
||
|
||
int resWidth = Screen.resolutions[i - 1].width;
|
||
int resHeight = Screen.resolutions[i - 1].height;
|
||
|
||
winPosX = resWidth / 2 - minWidth / 2;
|
||
winPosY = resHeight / 2 - minHeight / 2;
|
||
|
||
SetWindowLong(GetForegroundWindow(), GWL_STYLE, WS_POPUP);
|
||
bool result = SetWindowPos(GetForegroundWindow(), 0, 0, 0, maxWidth, maxHeight, SWP_SHOWWINDOW);
|
||
|
||
//windowSize.GetComponent<Image>().sprite = icon_smaller;//修改图标
|
||
}
|
||
}
|
||
#endregion
|
||
|
||
}
|