Advertisement
Geometrian

PyGame Transparent Window

Mar 10th, 2019
553
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.19 KB | None | 0 0
  1. import os, sys
  2. import traceback
  3.  
  4. import win32api
  5. import win32con
  6. import win32gui
  7.  
  8. with open(os.devnull, "w") as f:
  9.     oldstdout=sys.stdout; sys.stdout=f; import pygame; sys.stdout=oldstdout
  10.  
  11.  
  12. if sys.platform in ["win32","win64"]:
  13.     os.environ["SDL_VIDEO_CENTERED"] = "1"
  14. else:
  15.     raise OSError("Not implemented!")
  16.  
  17. pygame.display.init()
  18.  
  19. icon=pygame.Surface((1,1)); icon.set_alpha(0); pygame.display.set_icon(icon)
  20. pygame.display.set_caption("Transparent PyGame Windows - Ian Mallett")
  21.  
  22.  
  23. def show_startup():
  24.     res = (256,128)
  25.    
  26.     surface = pygame.display.set_mode(res,pygame.NOFRAME)
  27.  
  28.     color_transparent = (255,0,255)
  29.  
  30.     hwnd = pygame.display.get_wm_info()["window"]
  31.     win32gui.SetWindowLong(
  32.         hwnd,
  33.         win32con.GWL_EXSTYLE,
  34.         win32gui.GetWindowLong(hwnd,win32con.GWL_EXSTYLE) | win32con.WS_EX_LAYERED
  35.     )
  36.     win32gui.SetLayeredWindowAttributes(
  37.         hwnd,
  38.         win32api.RGB(*color_transparent), 0,
  39.         win32con.LWA_COLORKEY
  40.     )
  41.  
  42.     while True:
  43.         for event in pygame.event.get():
  44.             if   event.type == pygame.QUIT: return False
  45.             elif event.type == pygame.KEYDOWN:
  46.                 if   event.key == pygame.K_RETURN:
  47.                     return True
  48.                 elif event.key == pygame.K_ESCAPE:
  49.                     return False
  50.  
  51.         surface.fill(color_transparent)
  52.  
  53.         pygame.draw.circle(surface,(64,128,96,128),(128,64),64,0)
  54.         for r in [(0,0,16,16),(240,0,16,16),(0,112,16,16),(240,112,16,16)]:
  55.             pygame.draw.rect(surface,(64,96,255),r,2)
  56.  
  57.         pygame.display.flip()
  58.  
  59. def show_app():
  60.     surface = pygame.display.set_mode((512,256))
  61.  
  62.     while True:
  63.         for event in pygame.event.get():
  64.             if   event.type == pygame.QUIT: return
  65.             elif event.type == pygame.KEYDOWN:
  66.                 if   event.key == pygame.K_ESCAPE: return
  67.  
  68.         surface.fill((64,64,64))
  69.  
  70.         pygame.display.flip()
  71.  
  72. def main():
  73.     did_not_quit = show_startup()
  74.     if did_not_quit:
  75.         show_app()
  76.  
  77.     pygame.quit()
  78.  
  79. if __name__ == "__main__":
  80.     try:
  81.         main()
  82.     except:
  83.         traceback.print_exc()
  84.         pygame.quit()
  85.         input()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement