這個是調用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
}