公平地说,我必须说,我没有“回答我自己的问题”,也没有独自解决这个问题,而是能够得到马特·沃德和迈克尔·福德两人的外部帮助。我衷心感谢马特和迈克尔的时间,帮助和耐心,我真的很感谢他们回信更正。
主要问题是主窗体.py运行中的脚本是我从系统图命名空间,以及来自系统图.Drawing2D命名空间。尽管我的脚本没有直接实例化任何其他枚举或类,但它们仍然必须由.NET DLR从各自的程序集中加载和引用,以使它们在我的脚本中可访问和使用。(注:再次感谢马特向我指出这一点;如果解释中有任何错误,那是我的,而不是马特的。)
GDI+点实例的原始数组实例化是正确的,但在下面的更正脚本中显示了更简洁的方法。(注意:再次感谢Michael指出了数组实例化的替代方法。)
正确的和有效的主窗体.py脚本如下:
import System.Drawing
import System.Drawing.Drawing2D
import System.Windows.Forms
from System import Array
from System.Drawing import Pen, Point, Color
from System.Drawing.Drawing2D import GraphicsPath, CustomLineCap, SmoothingMode, LineJoin
from System.Windows.Forms import *
class MainForm(Form):
def __init__(self):
self.InitializeComponent()
def InitializeComponent(self):
self.SuspendLayout()
#
# MainForm
#
self.ClientSize = System.Drawing.Size(284, 264)
self.Name = "MainForm"
self.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
self.Text = "GDI+ CustomLineCaps"
self.Paint += self.MainFormPaint
self.ResumeLayout(False)
def MainFormPaint(self, sender, e):
graphics = e.Graphics
bluePen = Pen(Color.Blue, 1)
points = Array[Point] \
((Point(10, 10), Point(15, 10), Point(20, 15), \
Point(20, 20), Point(15, 25), Point(10, 25), \
Point(5, 20), Point(5, 15), Point(10, 10)))
graphicsPath = GraphicsPath()
graphicsPath.AddLines(points)
graphics.SmoothingMode = SmoothingMode.AntiAlias
lineCap = CustomLineCap(None, graphicsPath)
lineCap.BaseInset = 0
lineCap.WidthScale = 1
lineCap.StrokeJoin = LineJoin.Miter
bluePen.CustomStartCap = lineCap
bluePen.CustomEndCap = lineCap
graphics.DrawLine(bluePen, 50, 150, 200, 150)
graphics.DrawLine(bluePen, 150, 50, 150, 200)
lineCap.Dispose()
graphicsPath.Dispose()
bluePen.Dispose()