Pygame:允许点击通过窗口 [英] Pygame: allow clicks to go through the window

查看:53
本文介绍了Pygame:允许点击通过窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在 pygame 中制作一个伪透明窗口,目的是显示各种信息,如HUD"

I'm making a pseudo transparent window in pygame with the intent of displaying varied info like a "HUD"

该脚本使用 PIL 抓取桌面图像并将其用作窗口的背景.

The script uses PIL to grab an image of the desktop and use it as the background of the window.

一个简单的版本:

import pygame as py
from ctypes import windll
import ImageGrab, Image

SetWindowPos = windll.user32.SetWindowPos

py.init()

def get_image():
    im = ImageGrab.grab((0,0,window_x,window_y))
    mode = im.mode
    size = im.size
    data = im.tobytes()
    im = py.image.fromstring(data,size,mode)
    return im

window_x = 1920
window_y = 100

background = py.Surface((window_x,window_y))
background.blit(get_image(),(0,0))

window_pos = (0,0)


screen = py.display.set_mode((window_x,window_y),py.HWSURFACE|py.NOFRAME)

SetWindowPos(py.display.get_wm_info()['window'],-1,0,0,0,0,0x0001)

clock = py.time.Clock()

done = False

while not done:
    for event in py.event.get():
        if event.type == py.QUIT:
            done = True
    screen.blit(background,(0,0))
    py.display.flip()
    clock.tick(30)

py.quit()

这会在屏幕顶部创建一个 Pygame 窗口.

This creates a Pygame window at the top of the screen.

我的问题是 Pygame 窗口阻止了鼠标与其下方的任何交互.

My problem is that the Pygame window blocks any mouse interaction with anything beneath it.

有没有办法允许鼠标事件被忽略并通过"窗口,例如点击 Pygame 窗口下方的桌面图标.

Is there a way to allow mouse events to be ignored and go 'through' the window, like for example clicking on a desktop icon, underneath a Pygame window.

推荐答案

你需要做一些额外的黑客攻击,这是 PyGame 给你的.应该可以在 Python 中将 PyGame 画布渲染到另一个窗口框架中,并尝试使用该库的高级功能来实现这一点.

You will need to do a bit of an extra hacking which is outside what PyGame gives you. It should be possible to render the PyGame canvas into another windowing framework in Python and try to use advanced features of that library to achieve this.

一个例子是 wxWidgets.如该线程中所述,听起来与您要实现的目标非常相似,用户已经设置了一个可以点击的透明窗口.

One example is wxWidgets. As described in this thread, which sounds quite similar to what you are trying to achieve, the user has setup a window which can be clicked through and is transparent.

另见这篇 Stackoverflow 帖子提到了如何在 C# 中处理命中测试.在此处发布该帖子的代码:

Also see this another Stackoverflow Post which mentions how to handle the Hit Testing in C#. Posting code from that post here:

protected override void WndProc(ref Message m)
{
    if (m.Msg == (int)WM_NCHITTEST)
        m.Result = (IntPtr)HTTRANSPARENT;
    else
        base.WndProc(ref m);
}

可以使用 win32 API 在 Python 中进行类似的测试.这是一段执行此操作的 Python 代码. 找到程序员为事件设置回调的部分(类似于win32con.WM_NCHITTEST: self.onC,其中self.onChi 是回调).

It is possible to do similar testing in Python using win32 APIs. This is a piece of Python code that does exactly this. Locate the part where the programmer sets up the callback for the event (something like win32con.WM_NCHITTEST: self.onC where self.onChi is the callback).

我希望这能给你一个起点.我怀疑是否有任何现成的东西你会发现开箱即用,但这些应该会给你一些关于寻找什么的指示.

I hope this gives you a starting point. I doubt there is anything readymade that you will find out of the box but these should give you some pointers on what to look for.

这篇关于Pygame:允许点击通过窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆