持久绘图 + 临时覆盖 wx.PaintDC

Persistent drawing + temporary overlay with wx.PaintDC

我正在尝试使用 wxPython 制作类似 Microsoft Paint 的应用程序。

目前,应用程序 'draws' 在鼠标左下事件期间使用圆形画笔拖到屏幕上。这很好,也是理想的行为。但我还需要一个半径与 'follow' 鼠标相同的圆,而不是它持续地绘制到 wx.PaintDC.

也就是说,一个一定半径的圆跟随鼠标绕着屏幕,但只有当鼠标左键被按住时,圆才应该被'permanently'绘制到缓冲区上。

我采用的方法是 (1) 有一个圆圈跟随鼠标移动,但不管鼠标是否按下,都在 PaintDC 实例上绘制,(2) 跟随鼠标移动但从不持续绘制在 PaintDC 实例上,或者 (3) 不跟随鼠标移动,而是在鼠标左键按下时出现并持续绘制(参见下面的示例)。

谢谢!

import wx

class MyPanel(wx.Panel):
    def __init__(self, parent):
        wx.Panel.__init__(self, parent, -1)

        self.draw = False
        self.radius = 50

        self.Bind(wx.EVT_PAINT, self.OnPaint)
        self.Bind(wx.EVT_MOTION, self.Draw)
        self.Bind(wx.EVT_LEFT_DOWN, self.OnLeftDown)
        self.Bind(wx.EVT_LEFT_UP, self.OnLeftUp)

    def OnPaint(self, event):
        dc = wx.PaintDC(self)

    def Draw(self, event):

        if self.draw == True:
            x = event.GetX()
            y = event.GetY()
            dc = wx.ClientDC(self)

            pen = wx.Pen(wx.Colour(192,192,192,128), 2)
            brush = wx.Brush(wx.Colour(192,192,192,128))

            dc.SetPen(pen)
            dc.SetBrush(brush)
            dc.DrawCircle(x,y,self.radius)

    def OnLeftDown(self, event):
        self.draw = True

    def OnLeftUp(self, event):
        self.draw = False


class MyForm(wx.Frame):

    def __init__(self):
        wx.Frame.__init__(self, None, wx.ID_ANY, "Test",style=wx.DEFAULT_FRAME_STYLE,size=wx.Size(400, 300))
        self.main_panel = MyPanel(self)

if __name__ == "__main__":
    app = wx.App(False)
    frame = MyForm()
    frame.Show()
    app.MainLoop()

wx.Overlay class that does a pretty good job assisting with drawing temporary things on top of more permanent stuff. See the Overlay sample in the demo: https://github.com/wxWidgets/Phoenix/blob/master/demo/Overlay.py