0

ご挨拶;

System.Drawing.Point インスタンスの配列を正しくインスタンス化し、WinForms アプリケーションで IronPython を使用してポイントの配列を GDI+ GraphicsPath インスタンスに追加するのに少し問題があります。次のコードは、SharpDevelop 3.2 と IronPython 2.6 で正しくコンパイルまたはビルドされます。

import System.Drawing
import System.Drawing.Drawing2D 
import System.Windows.Forms

from System import Array 
from System.Drawing import Pen, Point
from System.Drawing.Drawing2D import GraphicsPath, CustomLineCap
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 Lines"
        self.Paint += self.MainFormPaint
        self.ResumeLayout(False)

    def MainFormPaint(self, sender, e):
        graphicContext = e.Graphics
        bluePen = Pen(Color.Blue, 1)

        points = Array.CreateInstance(Point, 9)
        points[0] = Point(10, 10)
        points[1] = Point(15, 10)
        points[2] = Point(20, 15)
        points[3] = Point(20, 20)
        points[4] = Point(15, 25)
        points[5] = Point(10, 25)
        points[6] = Point(5, 20)
        points[7] = Point(5, 15)
        points[8] = Point(10, 10)

        graphicsPath = GraphicsPath()
        graphicsPath.AddLines(points)
        graphicContext.SmoothingMode = SmoothingMode.AntiAlias

        lineCap = CustomLineCap(nil, graphicsPath)
        lineCap.BaseInset = 0
        lineCap.WidthScale = 1
        lineCap.StrokeJoin = LineJoin.Miter

        bluePen.CustomStartCap = lineCap
        bluePen.CustomEndCap = lineCap

        graphicContext.DrawLine(bluePen, 50, 150, 200, 150)
        graphicContext.DrawLine(bluePen, 150, 50, 150, 200)

        lineCap.Dispose()
        graphicsPath.Dispose()
        bluePen.Dispose()

上記のコードに基づいて、2 つの垂直な青い線が描画され、各線の最後に小さな楕円が表示されることを期待していました。上記の現在の scipting コードを使用すると、GDI+ ランタイム エラーの赤い X が描画されます。何が欠けているか、間違っていますか? また、System.Drawing.Point 値の配列をインスタンス化する、より簡単で簡潔な方法はありますか?

お時間を割いてご協力いただき、ありがとうございました...

4

1 に答える 1

1

公平を期すために、私は「自分の質問に答えた」わけでも、この問題を自分で解決したわけでもありませんが、Matt Ward と Michael Foord の両方から外部の助けを得ることができたと言わなければなりません。Matt と Michael の時間、助け、忍耐に心から感謝します。また、修正を加えて返信してくれたことに心から感謝します。

MainForm.py スクリプトを実行できなかった主な問題は、System.Drawing 名前空間から Color クラスをインポートし、System.Drawing.Drawing2D 名前空間から SmoothingMode および LineJoin 列挙をインポートする際の省略でした。このスクリプトでは、追加の列挙型やクラスを直接インスタンス化していませんが、スクリプト内でアクセスして使用できるようにするには、.NET DLR によってそれぞれのアセンブリから読み込んで参照する必要があります。(注: 繰り返しになりますが、これを指摘してくれた Matt に感謝します。説明に誤りがある場合、それらは私のものであり、Matt のものではありません。)

GDI+ Point インスタンスの元の Array インスタンス化は正しいものでしたが、以下の修正されたスクリプトでは、より簡潔なアプローチが示されています。(注: 繰り返しになりますが、配列のインスタンス化の代替案を指摘してくれた Michael に感謝します。)

修正された動作中の MainForm.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()
于 2010-06-20T23:28:11.397 に答える