• Unity


    原文参考

    主要是通过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));
        }
    }
    
    
  • 相关阅读:
    win+ubuntu双系统安装后无法进入win7解决方法
    dell笔记本重装系统
    Linux下文件重命名、创建、删除、修改及保存文件
    个人网站
    如何给网页标题添加icon小图标
    CentOS 7最小化安装后找不到‘ifconfig’命令——修复小提示
    WIN10 通过Console连接交换机
    linux各文件夹的作用
    API设计指南(译)
    Petri网的工具
  • 原文地址:https://www.cnblogs.com/lunoctis/p/12574998.html
Copyright © 2020-2023  润新知