0

I have the following code that gives me a window that is transparent and interacts with the window below. How can I lock it to the top layer? I have tried SetWindowPos() but then I get a top most window with no transparency.

def createWindow(width, height, transparentBG):
    pygame.init()
    os.environ['SDL_VIDEO_CENTERED'] = '2'
    window = pygame.display.set_mode((width, height))  # game window
    pygame.display.set_caption("recticle")  # title

    background = (0, 255, 0)

    if transparentBG:
        hwnd = pygame.display.get_wm_info()["window"]
        win32gui.SetWindowLong(hwnd, win32con.GWL_EXSTYLE,
                           win32gui.GetWindowLong(hwnd, win32con.GWL_EXSTYLE) | 
                                                       win32con.WS_EX_LAYERED)
        win32gui.SetLayeredWindowAttributes(hwnd, , 0, win32con.LWA_COLORKEY)


return window


def main():

    global RUNNING
    background = (0, 255, 0)
    window = createWindow(1920,1080, True)


    testImage = loadImage("customRect.bmp", (0,0))

    while RUNNING:
        window.fill(background) #fill background to transparency colour

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                RUNNING = False


            drawImage(window, testImage, (0,0))
            #update window
            pygame.display.update() #update window

Any help or suggestions appreciated, I have had a look around stack and I have code that sets a top layer window and does all described but it only displays text with win32api drawText function. I dont mind using this but I am not sure how to use a custom draw to display an image. I have tried saving out as a bitmap but again loosing transparency...

This is a non production app for me at home on a windows operating system. It is literally to draw a custom crosshair over a game window as I can see the one a game provides.

If anything is forgotten lemme know so I can edit.

Paul Hashmi
  • 456
  • 5
  • 18
  • 1
    [What if two programs did this?](https://devblogs.microsoft.com/oldnewthing/20050607-00/?p=35413) – IInspectable Jun 15 '20 at 11:15
  • Point very well taken, still need to do it though lol – Paul Hashmi Jun 15 '20 at 11:33
  • 1
    A window can be made a topmost window either by setting the `hWndInsertAfter` parameter to `HWND_TOPMOST` and ensuring that the `SWP_NOZORDER` flag is not set, or by setting a window's position in the Z order so that it is above any existing topmost windows. Refer [SetWindowPos](https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-setwindowpos#remarks). Do you need such an [effect](https://i.stack.imgur.com/549pm.gif)? – Strive Sun Jun 16 '20 at 08:02
  • I got it working with this and a custom draw function that sets pixels as I only needed a 24px x 24px window... Thank you – Paul Hashmi Jun 17 '20 at 03:34
  • It was just a sample written in C++. It looks like you have solved this problem, you can post an answer to end this thread. – Strive Sun Jun 18 '20 at 05:56

0 Answers0