简体   繁体   English

如何使用pygame制作自定义的最小化,最大化和关闭按钮?

[英]How do I go about making custom minimize, maximize, and close buttons with pygame?

I'm creating a game in , and I want to add custom buttons for minimizing, maximizing, and closing a window, similar to the discord desktop application 我正在创建一个游戏,并且我想添加自定义按钮以最小化,最大化和关闭窗口,类似于不和谐的桌面应用程序

Pygame is not really suited for that task. Pygame并不真正适合该任务。

Here's a running example: 这是一个正在运行的示例:

import pygame
import pygame.freetype

import ctypes
user32 = ctypes.WinDLL('user32')
SW_MAXIMISE = 3
SW_SHOWNORMAL = 1

def main():
    pygame.init()
    flags = pygame.RESIZABLE
    screen = pygame.display.set_mode((500, 500), flags)
    hWnd = user32.GetForegroundWindow()
    orgsize = None 

    clock = pygame.time.Clock()

    min = pygame.Rect((0, 16, 32, 32))
    max = pygame.Rect((0, 16, 32, 32))
    close = pygame.Rect((0, 16, 32, 32))

    min.right = screen.get_rect().right - 90
    max.right = screen.get_rect().right - 50
    close.right = screen.get_rect().right - 10

    font = pygame.freetype.SysFont(None, 32)
    font.origin = True
    while True:
        for e in pygame.event.get():
            if e.type == pygame.QUIT:
                return
            if e.type == pygame.VIDEORESIZE:
                screen = pygame.display.set_mode((e.w, e.h), flags)
                min.right = screen.get_rect().right - 90
                max.right = screen.get_rect().right - 50
                close.right = screen.get_rect().right - 10
            if e.type == pygame.MOUSEBUTTONDOWN:
                pos = pygame.mouse.get_pos()
                if close.collidepoint(pos):
                    return
                if min.collidepoint(pos):
                    pygame.display.iconify()
                if max.collidepoint(pos):
                    if not orgsize:
                        orgsize = screen.get_rect().size
                        user32.ShowWindow(hWnd, SW_MAXIMISE)
                    else:
                        user32.ShowWindow(hWnd, SW_SHOWNORMAL)
                        screen = pygame.display.set_mode(orgsize, flags)
                        orgsize = None

        screen.fill((30, 30, 30))
        pygame.draw.rect(screen, pygame.Color('dodgerblue'), max)
        pygame.draw.rect(screen, pygame.Color('darkorange'), min)
        pygame.draw.rect(screen, pygame.Color('darkred'), close)
        font.render_to(screen, min.move(7, -10).bottomleft, '_')
        font.render_to(screen, max.move(4, -5).bottomleft, 'O')
        font.render_to(screen, close.move(4, -5).bottomleft, 'X')
        pygame.display.update()
        clock.tick(60)

if __name__ == '__main__':
    main()
    pygame.quit()

Minimizing is easy: just call pygame.display.iconify() . 最小化很容易:只需调用pygame.display.iconify()

Closing is easy, too: just quit your main loop. 关闭也很容易:只需退出主循环即可。

Maximizing is not that easy: to have it to work, you need a resizable window, which will not work with a frameless window (at least on Windows). 最大化并不是那么容易:要使其发挥作用,您需要一个可调整大小的窗口,该窗口无法与无框架窗口一起使用(至少在Windows上如此)。 Also, the code to maximize a window is also platform specific. 同样,最大化窗口的代码也是特定于平台的。 My example above will only work on Windows, as it uses the user32.dll. 我上面的示例仅在Windows上有效,因为它使用了user32.dll。

Instead of maximizing, you could switch to fullscreen mode, so you don't have to use platfrom specific code and you don't have to bother with resolution changes. 除了最大化外,还可以切换到全屏模式,这样就不必使用platfrom特定代码,也不必费心处理分辨率更改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM