项目中遇到一个情况,客户想在副屏上显示程序,在主屏幕上显示桌面内容,操作主频不影响程序。
using UnityEngine;
using System.Collections;
using System;
using System.Runtime.InteropServices;
using System.IO;
public class WindowShow : MonoBehaviour
{
[HideInInspector]
//导入设置窗口函数
[DllImport("user32.dll")]
static extern bool SetWindowPos(IntPtr hWnd, int hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);
//函数功能:设置一个窗口显示的位置,大小,以及在空间中Z轴的位置情况。
// hWnd 要设置的窗口,本文就是指要显示在扩展屏上的窗口
//hWndInsetAfter 是窗口在空间Z周上的位置,显示在其他窗口的前或者后(当值为-1,窗口显示在所有窗口最前面)
//x 窗口在整个大屏上距离左边的X轴位置起始点
//y 窗口在整个大屏上距离左边的Y轴位置起始点
//cx 扩展屏宽度,也就是窗口的width
//cy 扩展屏高度,也就是窗口的height
//GetActiveWindow()
//函数功能:得到当前程序的线程的活动窗口
//导入当前活动窗口
[DllImport("user32.dll")]
static extern IntPtr GetActiveWindow();
//显示窗口
const uint SWP_SHOWWINDOW = 0x0040;
//扩展屏在整个大屏的X起始位置 (也就是你主屏的宽度)
int x = 1024;
//扩展屏在整个大屏的Y起始位置
int y = 0;
//扩展屏宽度 (程序的宽)
int width = 1024;
//扩展屏高度(程序的高)
int height = 576;
public void ReadX(string path)
{
if (File.Exists(path))
{
StreamReader sr = new StreamReader(path);
string str = sr.ReadToEnd();
x = int.Parse(str);
Debug.Log("X:" + str);
sr.Close();
}
}
private void Awake()
{
//ReadX(Application.streamingAssetsPath+ "/主显示器宽.txt");
}
void Start()
{
SetWindowPos(GetActiveWindow(), -1, x, y, width, height, SWP_SHOWWINDOW);
}
}```