zoukankan      html  css  js  c++  java
  • 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));
        }
    }
    
    
  • 相关阅读:
    ISEX安全期计算 V4.0 多国语言版女性安全期避孕计算实用工具软件
    FireARP防火墙 V3.1防局域网ARP攻击的软件
    asp.net页面字段绑定概述
    C# 删除、遍历指定目录下的所有指定文件、文件夹
    加入收藏、设为首页常用脚本
    分页加载数据Demo
    学习fragment的生命周期
    widget控件
    Menu菜单的代码和xml的创建
    WebView控件
  • 原文地址:https://www.cnblogs.com/lunoctis/p/12574998.html
Copyright © 2011-2022 走看看