C# 在全屏和窗口模式之间切换

C# 在全屏和窗口模式之间切换,c#,.net,winapi,c#-4.0,C#,.net,Winapi,C# 4.0,我正在尝试将游戏窗口从全屏模式更改为窗口模式,并从窗口模式更改为全屏模式。 从窗口模式到全屏模式工作正常,但从全屏模式到窗口模式不工作。那为什么呢?我怎样才能解决这个问题 [DllImport("user32.dll", EntryPoint = "FindWindow", SetLastError = true)] static extern IntPtr FindWindowByCaption(IntPtr zeroOnly, string lpWindowName);

我正在尝试将游戏窗口从全屏模式更改为窗口模式,并从窗口模式更改为全屏模式。 从窗口模式到全屏模式工作正常,但从全屏模式到窗口模式不工作。那为什么呢?我怎样才能解决这个问题

    [DllImport("user32.dll", EntryPoint = "FindWindow", SetLastError = true)]
    static extern IntPtr FindWindowByCaption(IntPtr zeroOnly, string lpWindowName);

    [DllImport("user32.dll", EntryPoint = "SetWindowLongPtr")]
    private static extern IntPtr SetWindowLongPtr64(IntPtr hWnd, int nIndex, long dwNewLong);

    [DllImport("user32.dll", EntryPoint = "GetWindowLong")]
    private static extern IntPtr GetWindowLongPtr32(IntPtr hWnd, int nIndex);

    public static IntPtr SetWindowLongPtr(IntPtr hWnd, int nIndex, long dwNewLong)
    {
        return IntPtr.Size == 8
            ? SetWindowLongPtr64(hWnd, nIndex, dwNewLong)
            : new IntPtr(SetWindowLong32(hWnd, nIndex, dwNewLong));
    }

    [DllImport("user32.dll", SetLastError = true)]
    static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int x, int y, int cx, int cy, SetWindowPosFlags uFlags);

    [DllImport("user32.dll")]
    private static extern bool ShowWindow(IntPtr hWnd, WindowShowStyle nCmdShow);

    // Works perfectly
    public static void SetFullScreenMode(string windowName)
    {
        IntPtr hWnd = FindWindowByCaption(IntPtr.Zero, windowName);

        SetWindowLongPtr(hWnd, GWL_STYLE, WS_VISIBLE | WS_CLIPSIBLINGS);
        SetWindowLongPtr(hWnd, GWL_EXSTYLE, WS_EX_TOPMOST);
        ShowWindow(hWnd, SW_SHOWMAXIMIZED);
    }

    // Does not work. Nothing happening when I call this method
    public static void SetWindowedMode(string windowName)
    {
        IntPtr hWnd = FindWindowByCaption(IntPtr.Zero, windowName);

        SetWindowLongPtr(hWnd, GWL_STYLE, WS_CAPTION | WS_VISIBLE | WS_CLIPSIBLINGS | WS_SYSMENU | WS_THICKFRAME | WS_MINIMIZEBOX | WS_MAXIMIZEBOX);
        SetWindowLongPtr(hWnd, GWL_EXSTYLE, WS_EX_WINDOWEDGE);
        SetWindowPos(hWnd, (IntPtr) 0, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_FRAMECHANGED | SWP_SHOWWINDOW);
    }

如果您使用的是C#Forms:,请查看解决方案,有一个是全屏正常的解决方案well@Chubosaurus:此问题要求修改外部应用程序的窗口状态。您的链接不提供此问题的解决方案。