在透明的风中划过

2024-04-24 16:44:09 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在编写一个需要选择屏幕区域的应用程序。我需要将光标改为一个十字,然后在用户选择的地方画一个矩形-这是成功的(虽然一个错误不断出现,我正在使用一个'不推荐'的功能-没有想出如何修复这个问题)。在

主要问题是当我将窗口更改为完全透明时(通过设置self.SetTransparent(0)),选择矩形也将变为透明,因此我无法使窗口完全透明并且仍能看到所选内容。在

代码如下:

import wx

class SelectableFrame(wx.Frame):

    c1 = None
    c2 = None

    def __init__(self, parent=None, id=-1, title=""):
        wx.Frame.__init__(self, parent, id, title, size=wx.DisplaySize())

        self.panel = wx.Panel(self, size=self.GetSize())
        self.panel.Bind(wx.EVT_MOTION, self.OnMouseMove)
        self.panel.Bind(wx.EVT_LEFT_DOWN, self.OnMouseDown)
        self.panel.Bind(wx.EVT_LEFT_UP, self.OnMouseUp)
        self.panel.Bind(wx.EVT_PAINT, self.OnPaint)
        self.SetCursor(wx.StockCursor(wx.CURSOR_CROSS))
        self.SetTransparent(10)

    def OnMouseMove(self, event):
        if event.Dragging() and event.LeftIsDown():
            self.c2 = event.GetPosition()
            self.Refresh()

    def OnMouseDown(self, event):
        self.c1 = event.GetPosition()

    def OnMouseUp(self, event):
        self.SetCursor(wx.StockCursor(wx.CURSOR_ARROW))

    def OnPaint(self, event):
        if self.c1 is None or self.c2 is None: return

        dc = wx.PaintDC(self.panel)
        dc.SetPen(wx.Pen('red', 2)) 
        dc.SetBrush(wx.TRANSPARENT_BRUSH) 
        dc.DrawRectangle(self.c1.x, self.c1.y, self.c2.x - self.c1.x, self.c2.y - self.c1.y)

    def PrintPosition(self, pos):
        return str(pos.x) + " " + str(pos.y)

class MyApp(wx.App):

    def OnInit(self):
        frame = SelectableFrame()
        frame.Show(True)
        self.SetTopWindow(frame)

        return True

app = MyApp(0)
app.MainLoop()

如何使窗口透明但使选择保持可见?在


Tags: posselfnoneeventreturnbinddefdc