Unity 修改windows窗口的標題


修改windows窗口的標題名稱,就是修改下圖的東西:

 

 第一種:

using UnityEngine;
using System;
using System.Runtime.InteropServices;
public class SetWindowText : MonoBehaviour
{
    #region WIN32API
    delegate bool EnumWindowsCallBack(IntPtr hwnd, IntPtr lParam);
    [DllImport("user32", CharSet = CharSet.Unicode)]
    static extern bool SetWindowTextW(IntPtr hwnd, string title);
    [DllImport("user32")]
    static extern int EnumWindows(EnumWindowsCallBack lpEnumFunc, IntPtr lParam);
    [DllImport("user32")]
    static extern uint GetWindowThreadProcessId(IntPtr hWnd, ref IntPtr lpdwProcessId);
    #endregion
    IntPtr myWindowHandle;
    public void Start()
    {
        IntPtr handle = (IntPtr)System.Diagnostics.Process.GetCurrentProcess().Id;  //獲取進程ID
        EnumWindows(new EnumWindowsCallBack(EnumWindCallback), handle);     //枚舉查找本窗口
        SetWindowTextW(myWindowHandle, "測試代碼"); //設置窗口標題
    }

    bool EnumWindCallback(IntPtr hwnd, IntPtr lParam)
    {
        IntPtr pid = IntPtr.Zero;
        GetWindowThreadProcessId(hwnd, ref pid);
        if (pid == lParam)  //判斷當前窗口是否屬於本進程
        {
            myWindowHandle = hwnd;
            return false;
        }
        return true;
    }
}

第二種:

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

public class NewBehaviourScript : MonoBehaviour {

    //Import the following.
    [DllImport("user32.dll", EntryPoint = "SetWindowTextW", CharSet = CharSet.Unicode)]
    public static extern bool SetWindowTextW(System.IntPtr hwnd, System.String lpString);
    [DllImport("user32.dll", EntryPoint = "FindWindow")]
    public static extern System.IntPtr FindWindow(System.String className, System.String windowName);

    public void Change()
    {
        //Get the window handle.
        var windowPtr = FindWindow(null, "WindowTitleChange");//打包時的ProductName,找到名字是WindowTitleChange的窗口
     //Set the title text using the window handle. 
     SetWindowTextW(windowPtr, "測試代碼");
}
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM