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

时间:2014-08-15 19:22:11

标签: 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 = "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);
    }

0 个答案:

没有答案