タイトル : Re: System.Drawing.Graphics.DrawLine メソッドについて 投稿日 : 2008/10/15(Wed) 10:42 投稿者 : 魔界の仮面弁士
そもそも、使用している命令が間違っています。 .NET の「DrawLine メソッド」は『直線の描画』を意味します。すなわち .DrawLine(Pens.Black, X1, Y1, X2, Y2) というメソッドは、VB6 でいうところの Line (X1, Y1)-(X2, Y2), vbBlack に相当する命令です。 しかし今回使用するのは、BF 指定(Box-Fill)すなわち『矩形の塗り潰し』ですよね。VB6 の Line (X1, Y1)-(X2, Y2), vbBlack, BF に相当する .NET のコードは、DrawLine ではなく .FillRectangle(Brushes.Black, X1, Y1, X2, Y2) のように、「FillRectangle メソッド」を使う事になります。 ちなみに VB6 の B 指定(Box)すなわち『矩形の枠線描画』を行うのであれば、 「DrawRectangle メソッド」を使う事になります。 Public Class Form1 Private downPos As Point = Point.Empty Private upPos As Point = Point.Empty Private Sub Form1_MouseDown(ByVal sender As Object, ByVal e As MouseEventArgs) Handles Me.MouseDown If e.Button = System.Windows.Forms.MouseButtons.Left Then downPos = e.Location upPos = downPos End If End Sub Private Sub Form1_MouseUp(ByVal sender As Object, ByVal e As MouseEventArgs) Handles Me.MouseUp If e.Button = System.Windows.Forms.MouseButtons.Left Then upPos = e.Location Me.Invalidate() End If End Sub Private Sub Form1_Paint(ByVal sender As Object, ByVal e As PaintEventArgs) Handles Me.Paint If upPos <> downPos Then Dim bounds As New Rectangle( _ New Point(Math.Min(downPos.X, upPos.X), Math.Min(downPos.Y, upPos.Y)), _ New Size(Math.Abs(downPos.X - upPos.X), Math.Abs(downPos.Y - upPos.Y))) e.Graphics.FillRectangle(Brushes.Black, Bounds) End If End Sub End Class |