[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: [pygame] how do I maximize the window?



On Sun, Oct 14, 2007 at 04:55:14AM +0000, Richard wrote:
> How do I maximize the pygame window or start it maximized? not fullscreen, just
> maximized.  by code, not by clicking on the maximize button.  i can't find a way
> to do this. if there's really not a way to do it, that's kind of disappointing.
Hi,

you can't using a built-in Pygame function. There is iconify() but no
corresponding restore() or maximize(). It is a limitation of the
underlying SDL library, which is being fixed in SDL-1.3 (or SDL-2).

For now, I use the ctypes module to call the platform-specific
functions:

import sys
import pygame

if sys.platform == "win32":
    from ctypes import windll, Structure, c_long, c_ulong, sizeof, byref

    SW_HIDE             =   0
    SW_SHOWNORMAL       =   1
    SW_NORMAL           =   1
    SW_SHOWMINIMIZED    =   2
    SW_SHOWMAXIMIZED    =   3
    SW_MAXIMIZE         =   3
    SW_SHOWNOACTIVATE   =   4
    SW_SHOW             =   5
    SW_MINIMIZE         =   6
    SW_SHOWMINNOACTIVE  =   7
    SW_SHOWNA           =   8
    SW_RESTORE          =   9
    SW_SHOWDEFAULT      =   10
    SW_FORCEMINIMIZE    =   11
    SW_MAX              =   11

    SWP_NOSIZE          =   0x0001
    SWP_NOMOVE          =   0x0002
    SWP_NOZORDER        =   0x0004
    SWP_NOREDRAW        =   0x0008
    SWP_NOACTIVATE      =   0x0010
    SWP_FRAMECHANGED    =   0x0020
    SWP_SHOWWINDOW      =   0x0040
    SWP_HIDEWINDOW      =   0x0080
    SWP_NOCOPYBITS      =   0x0100
    SWP_NOOWNERZORDER   =   0x0200
    SWP_NOSENDCHANGING  =   0x0400
    SWP_DRAWFRAME       =   SWP_FRAMECHANGED
    SWP_NOREPOSITION    =   SWP_NOOWNERZORDER

    HWND_TOP            =   0
    HWND_BOTTOM         =   1
    HWND_TOPMOST        =  -1
    HWND_NOTOPMOST      =  -2

    user32              = windll.user32
    IsIconic            = user32.IsIconic
    IsZoomed            = user32.IsZoomed
    ShowWindow          = user32.ShowWindow
    GetWindowRect       = user32.GetWindowRect
    SetWindowPos        = user32.SetWindowPos
    GetForegroundWindow = user32.GetForegroundWindow
    SetForegroundWindow = user32.SetForegroundWindow

    class RECT(Structure):
        _fields_ = [
            ('left',    c_long),
            ('top',     c_long),
            ('right',   c_long),
            ('bottom',  c_long),
        ]
        def width(self):  return self.right  - self.left
        def height(self): return self.bottom - self.top


    def getSDLWindow():
        return pygame.display.get_wm_info()['window']

    def SDL_IsIconic():
        return IsIconic(getSDLWindow())

    def SDL_IsMaximized():
        return IsZoomed(getSDLWindow())

    def SDL_Minimize():
        return ShowWindow(getSDLWindow(), SW_MINIMIZE)

    def SDL_Maximize():
        return ShowWindow(getSDLWindow(), SW_MAXIMIZE)

    def SDL_Restore():
        return ShowWindow(getSDLWindow(), SW_RESTORE)

    def SDL_Show(state):
        state = (SW_HIDE, SW_SHOW)[bool(state)]
        return ShowWindow(getSDLWindow(), state)

    def SDL_Activate():
        hWnd = getSDLWindow()
        if GetForegroundWindow() != hWnd:
           SetForegroundWindow(hWnd)

    def SDL_GetWindowPos():
        rc = RECT()
        GetWindowRect(getSDLWindow(), byref(rc))
        return rc.left, rc.top

    def SDL_SetWindowPos(x, y):
        return SetWindowPos(getSDLWindow(), 0, x, y, 0, 0, SWP_NOZORDER|SWP_NOSIZE)

    def SDL_AlwaysOnTop(state):
        zorder = (HWND_NOTOPMOST, HWND_TOPMOST)[state]
        return SetWindowPos(getSDLWindow(), zorder, 0, 0, 0, 0, SWP_NOMOVE|SWP_NOSIZE)

else:
    def SDL_IsIconic():
        return 0

    def SDL_IsMaximized():
        return 0

    def SDL_Minimize():
        return 0

    def SDL_Maximize():
        return 0

    def SDL_Restore():
        return 0

    def SDL_Show(state):
        return 0

    def SDL_Activate():
    	pass

    def SDL_GetWindowPos():
        return (-1, -1)

    def SDL_SetWindowPos(x, y):
        return 0

    def SDL_AlwaysOnTop(state):
        return 0


I don't have any code for X, but I don't believe it's much more
complicated.

Equivalent functions could be added to Pygame with support for other
Windowing systems,

regards,
John.