自学内容网 自学内容网

Unity实现关闭应用程序和关闭应用窗口

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Runtime.InteropServices;
using System;

public class WindowsClose : MonoBehaviour
{
    // 声明需要使用的Windows API函数
    [DllImport("user32.dll", SetLastError = true)]
    [return: MarshalAs(UnmanagedType.Bool)]
    static extern bool CloseWindow(IntPtr hWnd);

    //[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
    //static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

    // 调用CloseWindow函数关闭窗口 不触发 ondestory onapplicationquit
    public void CloseAppWindow()
    {
        // 获取当前窗口句柄
        IntPtr windowHandle = FindWindow(null, "WS Display Settings");

        // 关闭窗口
        if (windowHandle != IntPtr.Zero)
        {
            CloseWindow(windowHandle);
        }

    }




    [DllImport("user32.dll", EntryPoint = "FindWindow")]
    private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);


    [DllImport("user32.dll", EntryPoint = "PostMessage", SetLastError = true)]
    public static extern bool PostMessage(IntPtr hwnd, uint Msg, uint wParam, uint lParam);

    public const int WM_SYSCOMMAND = 0x0112;
    public const int SC_CLOSE = 0xF060;
    /// <summary>
    /// 调用 PostMessage 函数关闭窗口  可触发 ondestory onapplicationquit
    /// </summary>
    public void CloseAppWindow1()
    {
        //IntPtr hwnd = FindWindow(null, "WsDisplaySettings");
        //PostMessage(hwnd, WM_SYSCOMMAND, SC_CLOSE, 0);

        IntPtr hwnd1 = FindWindow(null, "WS Display Settings");
        PostMessage(hwnd1, WM_SYSCOMMAND, SC_CLOSE, 0);

    }

}

原文地址:https://blog.csdn.net/qq_39735878/article/details/137961803

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!