【问题标题】:Pygame, Resize and Move window with no frame?Pygame,调整大小和移动没有框架的窗口?
【发布时间】:2022-01-10 23:23:48
【问题描述】:

您好,我制作了自己的自定义标题栏,所以我添加了 NOFRAME 标志。有没有办法让我仍然能够移动和调整窗口大小(通过拖动边缘)?

【问题讨论】:

标签: python windows winapi pygame window


【解决方案1】:

我找到了一种移动屏幕的方法(对于 Windows,否则使用 this answer),但不调整大小(不闪烁)。

我使用了 Rabbid76 对 similar question 的回答并进行了编辑以适用于 python 2.0。

这一行是移动窗口 windll.user32.MoveWindow(hwnd, -coordinates[0], -coordinates[1], w, h, False) 的原因,您可以在docs 中了解更多信息

这里是完整的代码示例:

# coding : utf-8
import pygame
from pygame.locals import *
from os import environ
from ctypes import windll

pygame.init()

clock = pygame.time.Clock()
window_size_x, window_size_y = 720, 360

info = pygame.display.Info()
screen = pygame.display.set_mode((window_size_x, window_size_x), pygame.NOFRAME)


def moveWin(coordinates):
    # the handle to the window
    hwnd = pygame.display.get_wm_info()['window']

    # user32.MoveWindow also recieves a new size for the window
    w, h = pygame.display.get_surface().get_size()
    windll.user32.MoveWindow(hwnd, -coordinates[0], -coordinates[1], w, h, False)


def main():
    run = True
    pressed = False
    start_pos = (0, 0)
    window_coords = [0 ,0]
    moveWin(window_coords)
    while run:
        screen.fill((255, 255, 255))
        pygame.display.update()
        clock.tick(60)

        for event in pygame.event.get():
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_ESCAPE:
                    run = False

            if event.type == MOUSEBUTTONDOWN:
                pressed = True
                start_pos = pygame.mouse.get_pos()

            elif event.type == MOUSEMOTION:
                if pressed:
                    new_pos = pygame.mouse.get_pos()
                    window_coords[0] += start_pos[0] - new_pos[0]
                    window_coords[1] += start_pos[1] - new_pos[1]
                    moveWin(window_coords)
            elif event.type == MOUSEBUTTONUP:
                pressed = False


if __name__ == '__main__':
    main()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多