Keeping Python Application Always on Top (Even During Gaming)

Anonymous
2025-01-13T07:39:46+00:00

Hi all,

I am working on a Python script that continuously displays data from my program and other applications, ensuring that the data remains visible at all times, regardless of which window or application is active.

I have attempted to implement the "always on top" functionality using the following approaches:

self.root.attributes("-topmost", True)  # Always on top

def set_window_topmost(self):
    hwnd = windll.user32.GetForegroundWindow()
    extended_style = win32gui.GetWindowLong(hwnd, win32con.GWL_EXSTYLE)
    win32gui.SetWindowLong(
        hwnd, win32con.GWL_EXSTYLE, extended_style | win32con.WS_EX_TOPMOST
    )
    windll.user32.SetWindowPos(
        hwnd,
        win32con.HWND_TOPMOST,
        0, 0, 0, 0,
        win32con.SWP_NOMOVE | win32con.SWP_NOSIZE | win32con.SWP_NOACTIVATE
    )

Despite these efforts, the window fails to stay on top of fullscreen applications, such as games. However, I’ve noticed that applications like Skype and WhatsApp are able to achieve this functionality with their picture-in-picture feature, which remains on top even during fullscreen gaming or when other fullscreen software is in use.

I would greatly appreciate any insights or guidance on how to achieve this behavior. Is there a specific method or workaround that enables applications to stay on top of fullscreen windows?

Thank you for your time and assistance!

Windows for home | Windows 11 | Display and graphics

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

3 answers

Sort by: Most helpful
  1. Emmanuel Santana 40,110 Reputation points Independent Advisor
    2025-01-13T08:47:23+00:00

    No problem, happy to provide some guidance, Kumar. Have a nice day :)

    Was this answer helpful?

    0 comments No comments
  2. Anonymous
    2025-01-13T08:46:04+00:00

    Thanks a lot for your help and suggestion. I will try this code and also followup in the developer community.

    Thanks again

    Was this answer helpful?

    0 comments No comments
  3. Emmanuel Santana 40,110 Reputation points Independent Advisor
    2025-01-13T08:22:56+00:00

    Hello, thank you for reaching out to the Microsoft Community. I'm here to help with your questions or issues. Just note – this is a place where passionate Microsoft users help each other, we don't work directly for Microsoft.

    I see what you’re trying to do, but this forum might not be the best place for this kind of question. I’d suggest heading over to the Microsoft Developer Community instead—it’s a much better platform for technical inquiries like this. You can check it out here: https://developercommunity.microsoft.com/home.

    That said, let me give you a quick overview based on what you’ve shared, so, what you’re trying to pull off can be a bit tricky since some apps take over completely when they’re in fullscreen mode. The apps you mentioned usually handle this by using overlay techniques or "layered windows" to stay visible. Sometimes, they also use DirectX or OpenGL, so your overlay would need to work smoothly with those methods.

    Here’s an example code snippet that might help:

    import ctypes
    from win32api import GetModuleHandle
    from win32con import (
        WS_EX_LAYERED, WS_EX_TOPMOST, WS_POPUP, LWA_ALPHA, GWL_EXSTYLE, SWP_NOACTIVATE,
        SW_SHOW
    )
    from win32gui import (
        CreateWindowEx, UpdateWindow, SetLayeredWindowAttributes, ShowWindow,
        RegisterClass, WNDCLASS, DefWindowProc, LoadCursor, GetStockObject, DrawText
    )
    import time
    import win32con
    
    def create_testapp_overlay():
        """Creates an always-on-top layered window with transparency for TestApp."""
        h_instance = GetModuleHandle(None)
        class_name = "TestAppOverlay"
    
        # Define the window class attributes
        wnd_class = WNDCLASS()
        wnd_class.style = win32con.CS_HREDRAW | win32con.CS_VREDRAW
        wnd_class.lpfnWndProc = DefWindowProc
        wnd_class.hInstance = h_instance
        wnd_class.hCursor = LoadCursor(0, win32con.IDC_ARROW)
        wnd_class.hbrBackground = GetStockObject(win32con.WHITE_BRUSH)
        wnd_class.lpszClassName = class_name
    
        # Register the window class
        RegisterClass(wnd_class)
    
        # Create a transparent and always-on-top window
        hwnd = CreateWindowEx(
            WS_EX_TOPMOST | WS_EX_LAYERED,  # Extended styles for topmost and layered window
            class_name,                     # Class name
            "TestApp Overlay",              # Window title
            WS_POPUP,                       # Basic popup window
            100, 100, 400, 200,             # Position and size (x, y, width, height)
            None, None, h_instance, None
        )
    
        # Set transparency level (0 = fully transparent, 255 = opaque)
        SetLayeredWindowAttributes(hwnd, 0, 200, LWA_ALPHA)
    
        # Show the window
        ShowWindow(hwnd, SW_SHOW)
        UpdateWindow(hwnd)
    
        return hwnd
    
    def draw_text_on_window(hwnd):
        """Draw simple text on the window."""
        hdc = win32gui.GetDC(hwnd)
        text = "TestApp Overlay - Always On Top"
        rect = (10, 10, 390, 190)  # Coordinates for text area
        win32gui.DrawText(hdc, text, -1, rect, win32con.DT_CENTER | win32con.DT_VCENTER)
        win32gui.ReleaseDC(hwnd, hdc)
    
    # Create the TestApp overlay
    hwnd = create_testapp_overlay()
    
    # Main loop to keep the overlay running and display text
    while True:
        draw_text_on_window(hwnd)  # Render text on the overlay
        time.sleep(1)  # Keep the app running, updating every second
    

    The create_testapp_overlay() function sets up a transparent window that always stays on top.

    The draw_text_on_window() function handles displaying text directly on the overlay.

    The main loop keeps everything running and refreshes the display at regular intervals. You can tweak the transparency or even use Direct2D or OpenGL if you want smoother visuals.

    I hope this gives you a good starting point! I know it can be tricky, so don’t hesitate to check out the developer community link I shared earlier—it’s a great resource for finding more detailed help.

    Was this answer helpful?

    0 comments No comments