using System;
using System.Collections;
using System.Runtime.InteropServices;
using System.Xml;
using UnityEngine;
using UnityEngine.Networking;
public class ShowSingleGreen : MonoBehaviour
{
static ShowSingleGreen instance;
private ShowSingleGreen()
{
}
static readonly object obj = new object();
public static ShowSingleGreen Instance
{
get
{
if (instance == null)
{
lock (obj)
{
if (instance == null)
{
instance = new ShowSingleGreen();
}
}
}
return instance;
}
}
[DllImport("user32.dll", EntryPoint = "FindWindow")]
private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll")]
static extern IntPtr SetWindowLong(IntPtr hwnd, int _nIndex, int dwNewLong);
[DllImport("user32.dll")]
static extern bool SetWindowPos(IntPtr hWnd, int hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);
[DllImport("user32.dll")]
static extern IntPtr GetForegroundWindow();
const uint SWP_SHOWWINDOW = 0x0040;
const int GWL_STYLE = -16; //边框用的
const int WS_POPUP = 0x800000;
bool active;
//---------------
Vector2 position,resolution;
string[] str_arr;
//******
string path_txt;
void Start()
{
if (instance == null)
{
instance = this;
DontDestroyOnLoad(gameObject);
}
else
{
Destroy(gameObject);
}
//xml 读取
Xml_get_set();
//txt_load();//txt读取
}
#region xml
/// <summary>
/// 获取login地址
/// </summary>
void Xml_get_set()
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(Application.streamingAssetsPath + "/url_manager.xml");
XmlNodeList node = xmlDoc.SelectSingleNode("item").ChildNodes;
//遍历节点
foreach (XmlElement x1 in node)
{
//以名称进行判断
switch (x1.Name)
{
case "Position":
str_arr = x1.InnerText.Split(',');
position.x =float.Parse(str_arr[0]);
position.y = float.Parse(str_arr[1]);
print("position:" + position);
break;
case "Resolution":
str_arr = x1.InnerText.Split(',');
resolution.x = float.Parse(str_arr[0]);
resolution.y = float.Parse(str_arr[1]);
print("resolution:" + resolution);
break;
case "Title":
active = bool.Parse(x1.InnerText);
print("active:" + active);
break;
}
}
if (!active)
{
set_Frame();
}
Set_Position(position, resolution);
}
#endregion
#region txt
/// <summary>
/// 读取txt
/// </summary>
void txt_load()
{
path_txt = Application.streamingAssetsPath + "/Mytext.txt";
StartCoroutine(Load(path_txt));
}
public IEnumerator Load(string path)
{
UnityWebRequest m_request = UnityWebRequest.Get(path);
yield return m_request.SendWebRequest();
string result = m_request.downloadHandler.text;
print(result);
string[] arr = result.Split('\n');
print(arr.Length);
print(arr[0]);
}
#endregion
/// <summary>
/// 更改屏幕位置
/// </summary>
/// <param name="pos">位置从左上角开始计算</param>
/// <param name="width_hight">当前设置的分辨率</param>
public void Set_Position(Vector2 pos,Vector2 width_hight)
{
StartCoroutine(Setpositionable(pos, width_hight));
}
/// <summary>
/// 隐藏边框
/// </summary>
/// <param name="yes_no"></param>
private void set_Frame()
{
StartCoroutine(Set_Frame_yes_no());
}
IEnumerator Set_Frame_yes_no()
{
yield return new WaitForSeconds(0.1f);
IntPtr ptr = FindWindow(null, "Ce_shi_test"); //Ce_shi_test为程序名,需要替换
SetWindowLong(GetForegroundWindow(), GWL_STYLE, WS_POPUP); //无边框
}
IEnumerator Setpositionable(Vector2 pos, Vector2 width_hight)
{
yield return new WaitForSeconds(0.1f); //不知道为什么发布于行后,设置位置的不会生效,我延迟0.1秒就可以
//GetForegroundWindow()==FindWindow(null, "Ce_shi_test")
SetWindowPos(GetForegroundWindow(), 0, (int)pos.x, (int)pos.y, (int)width_hight.x, (int)width_hight.y, SWP_SHOWWINDOW);
}
}
参考链接:https://blog.csdn.net/qq_39097425/article/details/81664448