2

I'm trying to change the game window from fullscreen to windowed mode and from windowed to fullscreen. From windowed to fullscreen mode works fine, but from fullscreen to windowed mode does not work. So why? And how I can fix that?

    [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);
    }
Zer0CooL
  • 21
  • 2
  • If you're using C# Forms: http://stackoverflow.com/questions/505167/how-do-i-make-a-winforms-app-go-full-screen, check out the solutions there is one that is fullscreen to normal as well – Chubosaurus Software Aug 16 '14 at 01:25
  • @Chubosaurus: This question is asking to modify an **external** application's window state. Your link does not provide a solution for this. – IInspectable Aug 16 '14 at 13:15

0 Answers0