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 @   lunoctis  阅读(1024)  评论(0编辑  收藏  举报
编辑推荐:
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
点击右上角即可分享
微信分享提示