首页 > 技术文章 > Win平台下窗口操作(Unity)

tiancaiwrk 2019-07-23 10:56 原文

  Unity发布在Win平台时, 可以通过user32.dll的方式与Win API通信, 最小化窗口代码:

public static class DllImports
{
    private const int WM_SYSCOMMAND = 0x0112;

    const int SC_MINIMIZE = 0xF020;
    const int SC_MAXMIZE = 0xF030;
    const int SC_CLOSE = 0xF060;

#if UNITY_STANDALONE_WIN
    [System.Runtime.InteropServices.DllImport("user32.dll")]
    private static extern System.IntPtr GetActiveWindow();
    [DllImport("user32.dll", EntryPoint = "SendMessage", SetLastError = true)]
    private static extern IntPtr SendMessage(IntPtr hWnd, Int32 Msg, IntPtr wParam, IntPtr lParam);
#endif

    public static void MinimizeWindow()
    {
#if UNITY_STANDALONE_WIN
        try
        {
            IntPtr lHwnd = GetActiveWindow();
            SendMessage(lHwnd, WM_SYSCOMMAND, (IntPtr)SC_MINIMIZE, IntPtr.Zero);
        }
        catch(System.Exception ex)
        {
            Common.CommonDebug.instance.LogErrorToGUI(@ex.ToString());
        }
#endif
    }
}

  这些命令代码跟win版本有关, 不是很可靠

推荐阅读