1. 程式人生 > >Unity 修改windows視窗的標題

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, "測試程式碼");
}
}