-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathtest_overlay2.py
executable file
·74 lines (64 loc) · 2.27 KB
/
test_overlay2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#!/usr/bin/env python
import wx
print("wxPython version:", wx.version())
class OverlayPanel(wx.Panel):
def __init__(self, parent):
super(OverlayPanel, self).__init__(parent)
self.overlay = wx.Overlay()
self.permRect = None
self.selectionStart = None
self.Bind(wx.EVT_PAINT, self.OnPaint)
self.Bind(wx.EVT_LEFT_DOWN, self.OnLeftDown)
self.Bind(wx.EVT_LEFT_UP, self.OnLeftUp)
self.Bind(wx.EVT_MOTION, self.OnMotion)
def OnPaint(self, evt):
dc = wx.PaintDC(self)
if self.permRect:
dc.SetBrush(wx.BLACK_BRUSH)
if 'phoenix' in wx.version():
dc.DrawRectangle(self.permRect)
else:
dc.DrawRectangleRect(self.permRect)
def OnLeftDown(self, evt):
self.CaptureMouse()
self.overlay = wx.Overlay()
self.selectionStart = evt.Position
def OnLeftUp(self, evt):
if not self.HasCapture():
return
self.ReleaseMouse()
if 'phoenix' in wx.version():
self.permRect = wx.Rect(self.selectionStart, evt.Position)
else:
self.permRect = wx.RectPP(self.selectionStart, evt.Position)
self.selectionStart = None
#clear out any existing drawing
self.overlay.Reset()
self.Refresh()
def OnMotion(self, evt):
if not self.HasCapture():
return
dc = wx.ClientDC(self)
odc = wx.DCOverlay(self.overlay, dc)
odc.Clear()
if 'phoenix' in wx.version():
ctx = wx.GraphicsContext.Create(dc)
else:
ctx = wx.GraphicsContext_Create(dc)
ctx.SetPen(wx.GREY_PEN)
ctx.SetBrush(wx.Brush(wx.Colour(192, 192, 192, 128)))
if 'phoenix' in wx.version():
ctx.DrawRectangle(self.selectionStart[0], self.selectionStart[0], evt.Position[0], evt.Position[1])
else:
ctx.DrawRectangle(*wx.RectPP(self.selectionStart, evt.Position))
del odc
if __name__ == '__main__':
app = wx.App(False)
f = wx.Frame(None)
#work around flicker on MSW - setting transparency
#turns on window compositing, which allows for buffering
#of clientDC drawing
f.SetTransparent(254)
p = OverlayPanel(f)
f.Show()
app.MainLoop()