在Unity工程中, 调整某个Windows窗口的大小和位置、边框

FanWndUtils.cs

using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Text;
using UnityEngine;

public static class FanWndUtils
{
    private delegate bool windowsProcDelegate(IntPtr hwnd, string iParam);
    [DllImport("user32")]
    static extern int EnumWindows(windowsProcDelegate hwnd, string iParam);
    [DllImport("user32")]
    private static extern int GetWindowTextW(IntPtr hWnd, [MarshalAs(UnmanagedType.LPWStr)] StringBuilder lpString, int nMaxCount);
    [DllImport("user32")]
    public static extern bool IsWindowVisible(IntPtr hWnd);
    [DllImport("user32")]
    static extern bool GetWindowRect(IntPtr hWnd, ref FanRect lpRect);
    [DllImport("user32")]
    static extern bool SetWindowPos(IntPtr hWnd, int hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);
    [DllImport("user32")]
    static extern IntPtr GetWindowLong(IntPtr hwnd, int nIndex);

    [DllImport("user32")]
    public static extern int SetWindowLong(IntPtr hWnd, int nIndex, IntPtr dwNewLong);

    // 根据窗口名称,获取hwnd
    public static bool TryGetHwnd(string wndName, out IntPtr hWnd)
    {
        bool ret = false;
        hWnd = IntPtr.Zero;
        IntPtr tempHwnd = hWnd;
        EnumWindows(GetTargetNameWnd, wndName);
        hWnd = tempHwnd;
        if (hWnd != IntPtr.Zero)
        {
            ret = true;
        }
        return ret;

        bool GetTargetNameWnd(IntPtr curHwnd, string targetWndName)
        {
            var sb = new StringBuilder(256);
            GetWindowTextW(curHwnd, sb, sb.Capacity);
            var curWndName = sb.ToString();
            if (IsWindowVisible(curHwnd) && curWndName != string.Empty && curWndName.Equals(targetWndName))
            {
                tempHwnd = curHwnd;
            }
            return true;
        }
    }

    // 获取hwnd的位置和长宽
    public static void GetWndPosAndSize(IntPtr hwnd, out Vector2Int pos, out Vector2Int size)
    {
        FanRect lpRect = new FanRect();
        GetWindowRect(hwnd, ref lpRect);
        pos = new Vector2Int(lpRect.Left, lpRect.Top);
        size = new Vector2Int(lpRect.Right - lpRect.Left, lpRect.Bottom - lpRect.Top);
    }

    // 设置hwnd的位置和长宽
    public static void SetWndPosAndSize(IntPtr hwnd, Vector2Int pos, Vector2Int size)
    {
        SetWindowPos(hwnd, -1, pos.x, pos.y, size.x, size.y, 0x0040);
    }

    // 去除hwnd的边框
    public static void RemoveBorder(IntPtr hwnd)
    {
        var dwStyles = GetWindowLong(hwnd, -16);
        long sty = ((long)dwStyles);
        //      标题栏         不显示边框
        sty &= ~(0x00C00000L | 0x00400000L);
        SetWindowLong(hwnd, -16, (IntPtr)sty);
    }

    // 为hwnd添加边框
    public static void AddBorder(IntPtr hwnd)
    {
        long defaultSty = 382664704;
        var dwStyles = GetWindowLong(hwnd, -16);
        long sty = ((long)dwStyles);
        if (sty != defaultSty)
        {
            sty = defaultSty;
        }
        SetWindowLong(hwnd, -16, (IntPtr)sty);
    }

    private struct FanRect
    {
        public int Left;
        public int Top;
        public int Right;
        public int Bottom;
    }
}

你可能感兴趣的:(在Unity工程中, 调整某个Windows窗口的大小和位置、边框)