Unity - Win平台修改窗口标题

原文参考

主要是通过user32.dll获取窗口句柄, 和Unity没有什么关系

using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Text;

public class UnityEditorTitle
{
    #region wrap
    private delegate bool EnumThreadWindowsCallback(IntPtr hWnd, IntPtr lParam);

    [DllImport("user32.dll"]
    public static extern bool EnumWindows(EnumThreadWindowsCallback callback, IntPtr extraData);

    [DllImport("user32.dll"]
    public static extern int GetWindowThreadProcessId(HandleRef handle, out int processId);

    [DllImport("user32.dll"]
    public static extern IntPtr GetWindow(HandleRef hWnd, int uCmd);

    [DllImport("user32.dll"]
    public static extern bool IsWindowVisible(HandleRef hWnd);

    [DllImport("user32.dll")]
    private static extern bool GetWindowText(int hWnd, StringBuilder title, int maxBufSize);

    [DllImport("user32.dll"]
    private extern static int GetWindowTextLength(IntPtr hWnd);

    [DllImport("user32.dll"]
    public extern static int SetWindowText(int hwnd, string lpString);
    #endregion

    #region singleton
    private static UnityEditorTitle s_Instance;
    public static UnityEditorTitle Instance
    {
        get
        {
            if (s_Instance == null)
            {
                s_Instance = new UnityEditorTitle();
            }
            return s_Instance;
        }
    }
    #endregion

    private int m_HWnd;
    private bool m_HaveMainWindow;
    private IntPtr m_MainWindowHandle;
    private int m_ProcessId;
    private string m_OriginalTitle;

    private UnityEditorTitle()
    {
        m_ProcessId = Process.GetCurrentProcess().Id;
        m_HWnd = GetMainWindowHandle();
        StringBuilder sb = new StringBuilfer(255);
        GetWindowText(m_HWnd, sb, 255);
        m_OriginalTitle = sb.ToString();
    }

    public void SetText(string content)
    {
        SetWindowText(m_HWnd, content);
    }

    public void Recover()
    {
        SetWindowText(m_HWnd, m_OriginalTitle);
    }

    private int GetMainWindowHandle()
    {
        if (!m_HaveMainWindow)
        {
            m_MainWindowHandle = IntPtr.Zero;
            var callback = new EnumThreadWindowsCallback(
                (handle, ptr) => {
                    int num;
                    GetWindowThreadProcessId(new HandleRef(this, handle), out num);
                    if ((num == m_ProcessId) && IsMainWindow(handle))
                    {
                        m_MainWindowHandle = handle;
                        return false;
                    }

                    return true;
                }
            );
            EnumWindows(callback, IntPtr.Zero);
            GC.KeepAlive(callback);

            m_HaveMainWindow = true;
        }

        return m_MainWindowHandle.ToInt32();
    }

    private bool IsMainWindow(IntPtr handle)
    {
        if (GetWindow(new HandleRef(this, handle), 4) != IntPtr.Zero)
        {
            return false;
        }

        return IsWindowVisible(new HandleRef(this, handle));
    }
}

posted @ 2020-03-26 15:58  lunoctis  阅读(1012)  评论(0编辑  收藏  举报