Skip to content

Commit e23f36f

Browse files
added a demo of drawing while mouse moves, without ClientDC.
1 parent 396d1ad commit e23f36f

File tree

1 file changed

+143
-0
lines changed

1 file changed

+143
-0
lines changed

DrawingWhileMouseMoves.py

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
#!/usr/bin/env python
2+
3+
"""
4+
A test of drawing while the mouse is moving.
5+
6+
The old way of doing this is with a wxClientDC, but that does
7+
not work right, or at least not well on OS-X. So this shows how
8+
to do it with Refresh();Update().
9+
"""
10+
11+
import wx
12+
13+
print "running with version:", wx.__version__
14+
import random
15+
16+
17+
class DrawWindow(wx.Panel):
18+
def __init__(self, parent, id = -1):
19+
## Any data the Draw() function needs must be initialized before
20+
## calling BufferedWindow.__init__, as it will call the Draw
21+
## function.
22+
wx.Panel.__init__(self, parent, id)
23+
24+
self.Bind(wx.EVT_LEFT_DOWN, self.OnLeftDown)
25+
self.Bind(wx.EVT_LEFT_UP, self.OnLeftUp)
26+
self.Bind(wx.EVT_MOTION, self.OnMove)
27+
28+
wx.EVT_PAINT(self, self.OnPaint)
29+
wx.EVT_SIZE(self, self.OnSize)
30+
31+
# OnSize called to make sure the buffer is initialized.
32+
# This might result in OnSize getting called twice on some
33+
# platforms at initialization, but little harm done.
34+
35+
self.mouse_line = None
36+
self.OnSize(None)
37+
38+
def OnPaint(self, event):
39+
# This draws the buffer to the screen, then optionally the mouse_line
40+
dc = wx.PaintDC(self)
41+
dc.DrawBitmap(self._Buffer,0,0)
42+
if self.mouse_line is not None:
43+
dc.SetPen(wx.Pen('WHITE', 2, wx.SHORT_DASH))
44+
dc.DrawLinePoint( *self.mouse_line )
45+
46+
47+
def OnSize(self,event):
48+
# The Buffer init is done here, to make sure the buffer is always
49+
# the same size as the Window
50+
Size = self.GetClientSizeTuple()
51+
52+
# Make sure we don't try to create a 0 size bitmap
53+
Size = (max(Size[0], 1), max(Size[1], 1))
54+
self._Buffer = wx.EmptyBitmap(Size[0],Size[1])
55+
self.Draw()
56+
57+
def Draw(self):
58+
"""
59+
This draws the backgound image on the buffer
60+
61+
"""
62+
# update the buffer
63+
dc = wx.MemoryDC()
64+
dc.SelectObject(self._Buffer)
65+
66+
coords = ((40,40),(200,220),(210,120),(120,300))
67+
dc.BeginDrawing()
68+
dc.SetBackground( wx.Brush("Blue") )
69+
dc.Clear() # make sure you clear the bitmap!
70+
71+
dc.SetPen(wx.RED_PEN)
72+
dc.SetBrush(wx.CYAN_BRUSH)
73+
74+
dc.DrawPolygon(coords)
75+
76+
77+
def OnLeftDown(self, event):
78+
self.CaptureMouse()
79+
self.mouse_line = [ event.GetPosition(), None]
80+
81+
def OnLeftUp(self, event):
82+
self.mouse_line = None
83+
if self.HasCapture():
84+
self.ReleaseMouse()
85+
self.Refresh()
86+
self.Update()
87+
88+
def OnMove(self, event):
89+
if event.Dragging() and event.LeftIsDown() and (self.mouse_line is not None):
90+
self.mouse_line[1] = event.GetPosition()
91+
self.Refresh()
92+
self.Update()
93+
94+
95+
class TestFrame(wx.Frame):
96+
def __init__(self):
97+
wx.Frame.__init__(self, None, -1, "ClientDC Test",
98+
wx.DefaultPosition,
99+
size=(500,500),
100+
style=wx.DEFAULT_FRAME_STYLE | wx.NO_FULL_REPAINT_ON_RESIZE)
101+
102+
self.Window = DrawWindow(self)
103+
104+
class DemoApp(wx.App):
105+
def OnInit(self):
106+
wx.InitAllImageHandlers() # called so a PNG can be saved
107+
frame = TestFrame()
108+
frame.Show(True)
109+
110+
## initialize a drawing
111+
## It doesn't seem like this should be here, but the Frame does
112+
## not get sized untill Show() is called, so it doesn't work if
113+
## it is put in the __init__ method.
114+
115+
#frame.NewDrawing(None)
116+
117+
self.SetTopWindow(frame)
118+
119+
return True
120+
121+
if __name__ == "__main__":
122+
app = DemoApp(0)
123+
app.MainLoop()
124+
125+
126+
127+
128+
129+
130+
131+
132+
133+
134+
135+
136+
137+
138+
139+
140+
141+
142+
143+

0 commit comments

Comments
 (0)