You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何判断Windows应用商店版Minecraft Windows 10是否最大化

How to Check if Minecraft Windows 10 Window is Maximized

Hey there! Let's figure out how to detect if your Minecraft Windows 10 app window is in maximized state. Here's a straightforward approach using Windows API functions:

Step 1: Import Required API and Define Structures

First, we need to use the GetWindowPlacement function from user32.dll—this function retrieves the current state and position of a window. We also need a struct to hold the window's placement data. Add these definitions to your code:

using System.Runtime.InteropServices;
using System.Drawing; // For Point and Rectangle types

// Import the GetWindowPlacement API
[DllImport("user32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool GetWindowPlacement(IntPtr hWnd, ref WINDOWPLACEMENT lpwndpl);

// Struct to store window placement information
[Serializable]
[StructLayout(LayoutKind.Sequential)]
private struct WINDOWPLACEMENT
{
    public int length;
    public int flags;
    public int showCmd;
    public Point ptMinPosition;
    public Point ptMaxPosition;
    public Rectangle rcNormalPosition;
}

// Constant representing the maximized window state
private const int SW_MAXIMIZE = 3;

Step 2: Update Your Timer Tick Logic

In your timer1_Tick method, we'll fix how you get the window handle (important: Process.Handle is the process handle, not the actual window handle!) and add the check for maximized state:

private void timer1_Tick(object sender, EventArgs e)
{
    IntPtr minecraftWindowPtr = IntPtr.Zero;
    try
    {
        // Get the Minecraft Windows process and its main window handle
        var minecraftProcess = System.Diagnostics.Process.GetProcessesByName("Minecraft.Windows").FirstOrDefault();
        if (minecraftProcess != null)
        {
            minecraftWindowPtr = minecraftProcess.MainWindowHandle;
        }
    }
    catch (Exception)
    {
        // Handle the case where Minecraft isn't running
    }

    if (minecraftWindowPtr != IntPtr.Zero)
    {
        WINDOWPLACEMENT windowPlacement = new WINDOWPLACEMENT();
        // Critical: Set this to the size of the struct before calling GetWindowPlacement
        windowPlacement.length = Marshal.SizeOf(windowPlacement);

        if (GetWindowPlacement(minecraftWindowPtr, ref windowPlacement))
        {
            // Check if the window is in maximized state
            bool isMaximized = windowPlacement.showCmd == SW_MAXIMIZE;
            
            // Use this boolean value for your activity logging logic
            if (isMaximized)
            {
                // Example: Log that the window is maximized
                Console.WriteLine("Minecraft window is currently maximized!");
            }
            else
            {
                Console.WriteLine("Minecraft window is not maximized.");
            }
        }
        else
        {
            // Handle error if GetWindowPlacement fails
            int errorCode = Marshal.GetLastWin32Error();
            Console.WriteLine($"Failed to retrieve window placement. Error code: {errorCode}");
        }
    }
}

Key Notes to Remember

  • Window vs Process Handle: Your original code used Process.Handle—that's the handle for the entire process, not the game window. Always use MainWindowHandle to get the actual window handle you need.
  • Struct Initialization: The length field of WINDOWPLACEMENT must be set to the size of the struct before calling GetWindowPlacement, otherwise the function will fail.
  • State Check: The showCmd field in the struct tells us the window's current state. A value of SW_MAXIMIZE (3) confirms the window is maximized.

内容的提问来源于stack exchange,提问作者RomanAleshin

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 02:22:47