unity 调用其他程序最大化最小化

该代码示例是一个Unity组件,使用C#和DllImport调用WindowsAPI来控制窗口的最大化、最小化和还原状态。主要方法包括FindWindow、ShowWindow,用于识别和操作指定名称的窗口。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using UnityEngine;
public class WindowMaxAndMin : MonoBehaviour
{
    public static WindowMaxAndMin instance;

    [DllImport("user32.dll")]
    public static extern bool ShowWindow(IntPtr hwnd, int nCmdShow);

    [DllImport("user32.dll")]
    static extern IntPtr GetForegroundWindow();

    [DllImport("user32.dll", EntryPoint = "FindWindow")]
    static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
    IntPtr ParenthWnd = FindWindow(null, "YJG-Intangible Cultural Heritage");  //build时候的项目名

    const int SW_SHOWMINIMIZED = 2; //{最小化, 激活}
    const int SW_SHOWMAXIMIZED = 3;//最大化
    const int SW_SHOWRESTORE = 1;//还原
    public string response;
    public string oneselfURL;
    private void Awake()
    {
        instance = this;
    }

    private void Start()
    {
        response = ConfigTest.dic["设置"]["softwareURL"];
        oneselfURL = ConfigTest.dic["设置"]["oneselfURL"];
    }
    public void OnGetWindow()
    {

    }
    public void OnMin()
    {
        if (response != null && !string.IsNullOrEmpty(oneselfURL))
        {
            string fileName = System.IO.Path.GetFileName(oneselfURL);
            fileName = fileName.Split('.')[0];
            Debug.Log(fileName);
            OnClickMinimize(fileName);
        }
    }
    public void OnMax()
    {
        if (response != null && !string.IsNullOrEmpty(response))
        {
            string fileName = System.IO.Path.GetFileName(response);
            fileName = fileName.Split('.')[0];
            Debug.Log(fileName);
            OnClickMaximize(fileName);
        }
    }
    public void OnClickMinimize(string _name)
    {
        ParenthWnd = FindWindow(null, _name);
        //最小化 
        //Debug.Log("GetForegroundWindow = " + GetForegroundWindow() + " ParenthWnd =  " + ParenthWnd);
        // ShowWindow(GetForegroundWindow(), SW_SHOWMINIMIZED);
        ShowWindow(ParenthWnd, SW_SHOWMINIMIZED);

        //Invoke("OnClickMaximize", 5.0f) ;
    }

    public void OnClickMaximize(string _name)
    {
        ParenthWnd = FindWindow(null, _name);
        //最大化
        //ShowWindow(GetForegroundWindow(), SW_SHOWMAXIMIZED);
        ShowWindow(ParenthWnd, SW_SHOWMAXIMIZED);
    }

    public void OnClickRestore()
    {
        //还原
        ShowWindow(GetForegroundWindow(), SW_SHOWRESTORE);
    }
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

最菜程序员

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值