Unity 设置外部程序无边框和显示隐藏

这个是调用win10自带屏幕键盘的代码

首先要找的win10自带屏幕键盘的exe文件,保存到unity streamingAssets文件夹下

启动unity 要用管理员模式启动,不然打不开自带的屏幕键盘,对于其他的程序可能不需要用管理员启动unity

上代码:

using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.InteropServices;
using UnityEngine;
 
public class CreateWindowController : MonoBehaviour
{
    private Process keyBoardProcess;
    private IntPtr keyBoardIntPtr;
    void OnGUI()
    {
        if (GUI.Button(new Rect(20, 20, 120, 50), "打开键盘"))
        {
            keyBoardProcess = Process.Start(Application.streamingAssetsPath + "\\osk");
            //等待进程开启后再去获取
            // 一定要获取主窗口句柄,不然控制不了外部程序
            StartCoroutine(WaitTimeGetIntPrt());
        }
 
        if (GUI.Button(new Rect(20, 80, 120, 50), "设置无边框"))
        {
            StartCoroutine(Setposition(keyBoardIntPtr, 0, 0, 400, 150));
        }
 
        if (GUI.Button(new Rect(20, 140, 120, 50), "显示键盘"))
        {
            ShowWindow(keyBoardIntPtr, SW_SHOW);
        }
 
        if (GUI.Button(new Rect(20, 200, 120, 50), "隐藏键盘"))
        {
            ShowWindow(keyBoardIntPtr, SW_HIDE);
        }
 
        if (GUI.Button(new Rect(20, 260, 120, 50), "干掉键盘"))
        {
            keyBoardProcess.Kill();
        }
    }
 
    IEnumerator WaitTimeGetIntPrt()
    {
        yield return new WaitForSeconds(1f);
        keyBoardIntPtr = GetProcessWnd(keyBoardProcess);
    }
 
    #region 获取进程主窗口句柄
 
    public delegate bool WNDENUMPROC(IntPtr hwnd, uint lParam);
    [DllImport("user32.dll", SetLastError = true)]
    public static extern bool EnumWindows(WNDENUMPROC lpEnumFunc, uint lParam);
 
    [DllImport("user32.dll", SetLastError = true)]
    public static extern IntPtr GetParent(IntPtr hWnd);
    [DllImport("user32.dll")]
    public static extern uint GetWindowThreadProcessId(IntPtr hWnd, ref uint lpdwProcessId);
 
    [DllImport("kernel32.dll")]
    public static extern void SetLastError(uint dwErrCode);
 
 
    public static IntPtr GetProcessWnd(Process process)
    {
        IntPtr ptrWnd = IntPtr.Zero;
        uint pid = (uint)process.Id;  // 当前进程 ID
 
        bool bResult = EnumWindows(new WNDENUMPROC(delegate (IntPtr hwnd, uint lParam)
        {
            uint id = 0;
 
            if (GetParent(hwnd) == IntPtr.Zero)
            {
                GetWindowThreadProcessId(hwnd, ref id);
                if (id == lParam)    // 找到进程对应的主窗口句柄
                {
                    ptrWnd = hwnd;   // 把句柄缓存起来
                    SetLastError(0);    // 设置无错误
                    return false;   // 返回 false 以终止枚举窗口
                }
            }
 
            return true;
 
        }), pid);
 
        return (!bResult && Marshal.GetLastWin32Error() == 0) ? ptrWnd : IntPtr.Zero;
    }
 
    #endregion
 
    #region 无边框设置
    [DllImport("user32.dll")]
    private static extern IntPtr SetWindowLong(IntPtr hwnd, int _nIndex, int dwNewLong);
 
    [DllImport("user32.dll")]
    private static extern bool SetWindowPos(IntPtr hWnd, int hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);
 
    [DllImport("user32.dll")]
    private static extern IntPtr GetForegroundWindow();
 
    private const uint SWP_SHOWWINDOW = 0x0040;
    private const int GWL_STYLE = -16;  //边框用的
    private const int WS_BORDER = 1;
    private const int WS_POPUP = 0x800000;
 
    public static IEnumerator Setposition(IntPtr intPtr, int x, int y, int with, int height)
    {
        yield return new WaitForSeconds(0.1f); 
        SetWindowLong(intPtr, GWL_STYLE, WS_POPUP); //无边框
        bool result = SetWindowPos(intPtr, 0, x, y, with, height, SWP_SHOWWINDOW); //设置屏幕大小和位置
        UnityEngine.Debug.Log(result);
    }
    #endregion
 
    #region 窗口最小化定义
 
    [DllImport("user32.dll")]
    public static extern bool ShowWindow(IntPtr hwnd, int nCmdShow);
    private const int SW_HIDE = 0;
 
    private const int SW_NORMAL = 1;
 
    private const int SW_MAXIMIZE = 3;
 
    private const int SW_SHOWNOACTIVATE = 4;
 
    private const int SW_SHOW = 5;
 
    private const int SW_MINIMIZE = 6;
 
    private const int SW_RESTORE = 9;
 
    private const int SW_SHOWDEFAULT = 10;
    #endregion
}
 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章