マウスでクリックした点を結ぶ線を描く

画面上でのマウスをクリックする。それらの点を結ぶ線を描くIronPythonプログラムは以下の通り。

import clr
clr.AddReference("System.Windows.Forms")
clr.AddReference("System.Drawing")

from System.Windows.Forms import Application, Form
from System.Drawing import Size, Pen, Color
from System.Drawing.Drawing2D import DashStyle

class IForm(Form):
    def __init__(self):
        self.Text = 'Lines'
        self.Size = Size(280, 270)
        self.coords = list()
        self.Click += self.OnClick
        self.Paint += self.OnPaint
        self.CenterToScreen()

    def OnClick(self, event):
        xy = [event.X, event.Y]
        self.coords.append(xy)
        self.Invalidate()
       
    def OnPaint(self, event):
        g = event.Graphics

        pen = Pen(Color.Blue, 1)
#        pen.DashStyle = DashStyle.Dot   #線の種類がいくつかある
#        pen.DashStyle = DashStyle.DashDot
#        pen.DashStyle = DashStyle.Dash
#        pen.DashStyle = DashStyle.DashDotDot

        for i in range(len(self.coords)-1):
            xs = self.coords[i][0]
            ys = self.coords[i][1]
            xe = self.coords[i+1][0]
            ye = self.coords[i+1][1]
            g.DrawLine(pen, xs, ys, xe, ye) #線を描く
       
        pen.Dispose()
        g.Dispose()

Application.Run(IForm())

実行例