Wie wäre es dann so?
(abgesehen davon, daß es als Alternative zu Dim g As Graphics = CreateGraphics() bessere Wege gibt)
Public Class Form1
Private Sub Form1_Click(ByVal sender As Object, ByVal e As _
System.EventArgs) Handles Me.Click
Invalidate()
End Sub
Private Sub Form1_MouseMove(ByVal sender As Object, ByVal e As _
System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove
Dim g As Graphics = CreateGraphics()
Dim p As Pen = New Pen(BackColor, 0)
g.Clear(BackColor)
Dim ptMitte As New PointF(CSng(ClientSize.Width / 2), CSng( _
ClientSize.Height / 2))
Static ptmaus As New PointF
Dim br As New SolidBrush(Color.Black)
Dim fn As New Font("Arial", 10)
' Löschen
'g.DrawLine(p, ptMitte, ptmaus)
' Lote
g.DrawLine(p, ptmaus, New PointF(ptmaus.X, ptMitte.Y))
g.DrawLine(p, ptMitte, New PointF(ptmaus.X, ptMitte.Y))
' Sichtbar zeichnen
ptmaus.X = CSng(e.X)
ptmaus.Y = CSng(e.Y)
p.Color = Color.Black
g.DrawLine(p, ptMitte, ptmaus)
' Lote
p.Color = Color.Black
g.DrawLine(p, ptmaus, New PointF(ptmaus.X, ptMitte.Y))
g.DrawLine(p, ptMitte, New PointF(ptmaus.X, ptMitte.Y))
'g.DrawString("a", fn, br, 0, 10)
'g.DrawString("b", fn, br, 0, 40)
'g.DrawString("c", fn, br, 0, 50)
LinieMitText(g, "a", ptMitte, ptmaus)
LinieMitText(g, "b", ptmaus, New PointF(ptmaus.X, ptMitte.Y))
LinieMitText(g, "c", ptMitte, New PointF(ptmaus.X, ptMitte.Y))
End Sub
''' <summary>
''' Text steht direkt auf der Linienmitte, zentriert auf beiden Achsen,
' nicht gedreht um Liniensteigung
''' </summary>
''' <param name="pGR"></param>
''' <param name="pP1"></param>
''' <param name="pP2"></param>
''' <remarks></remarks>
Private Sub LinieMitText(ByVal pGR As Graphics, ByVal pText As String, _
ByVal pP1 As PointF, ByVal pP2 As PointF)
Dim l_TextAnsatzStelle As New Point
Dim l_SF As New StringFormat
If pP1.X < pP2.X Then
l_TextAnsatzStelle.X = pP1.X + 0.5 * (pP2.X - pP1.X)
Else
l_TextAnsatzStelle.X = pP2.X + 0.5 * (pP1.X - pP2.X)
End If
If pP1.Y < pP2.Y Then
l_TextAnsatzStelle.Y = pP1.Y + 0.5 * (pP2.Y - pP1.Y)
Else
l_TextAnsatzStelle.Y = pP2.Y + 0.5 * (pP1.Y - pP2.Y)
End If
l_SF.LineAlignment = StringAlignment.Center
l_SF.Alignment = StringAlignment.Center
pGR.DrawLine(Pens.Black, pP1, pP2)
pGR.DrawString(pText, New Font("Arial", 12), Brushes.Blue, _
l_TextAnsatzStelle, l_SF)
End Sub
End Class |