0

How can I keep a full screen PyGame window on top?

I have written a simple program below that creates a screen overlay and draws a circle on it. The overlay can be clicked through and content behind the overlay can be interacted with.

import pygame

import win32api
import win32con
import win32gui

pygame.init()

screen = pygame.display.set_mode((0, 0), pygame.FULLSCREEN)

transparent = (74, 65, 42)
window_info = pygame.display.get_wm_info()["window"]
win32gui.SetWindowLong(window_info, win32con.GWL_EXSTYLE, win32gui.GetWindowLong(window_info, win32con.GWL_EXSTYLE) | win32con.WS_EX_LAYERED)
win32gui.SetLayeredWindowAttributes(window_info, win32api.RGB(*transparent), 0, win32con.LWA_COLORKEY)

running = True
while running:

      for event in pygame.event.get():
            if event.type == pygame.QUIT:
                  running = False
            if event.type == pygame.KEYDOWN:
                  if event.key == pygame.K_RCTRL:
                        running = False

      screen.fill(transparent)

      pygame.draw.circle(screen, (255, 255, 255), (150, 150), 75)

      pygame.display.flip()

pygame.quit()

My problem is that when I interact with the windows behind the overlay, it moves the overlay to the back. I want to be able to pin the overlay on top but still be able to interact with the background items.

I have tried using functions of the win32gui and tried code from https://stackoverflow.com/a/25383929/14335807 and https://stackoverflow.com/a/37001979/14335807.

  • This [answer](https://stackoverflow.com/a/25383929) works if you use `pygame.NOFRAME` instead of `pygame.FULLSCREEN`. – import random Nov 13 '20 at 00:28
  • @importrandom Unfortunately, it has the same issues as all my other attempts. When I interact with something behind the overlay, it sends the overlay to the back. Currently, I am using Taskbar Hide to keep it on top but I don't want to use Taskbar Hide every time. For some reason, Taskbar Hide and WindowTop work. – Brian Nguyen Nov 13 '20 at 01:56
  • I think this is a side effect of your transparency approach. Have you considered taking a screenshot of the background and blitting on top of that? – import random Nov 16 '20 at 01:37
  • @importrandom I want to be able to interact with the background though. Think of this as an overlay that displays text and pictures on top of the background, not a camera of the background itself. If I blit a screenshot on top, it takes away the clickthrough ability. – Brian Nguyen Nov 25 '20 at 23:08

0 Answers0