Results 1 to 2 of 2

Thread: draw string in center of a rectangle

  1. #1

    Thread Starter
    New Member
    Join Date
    Dec 2022
    Posts
    8

    draw string in center of a rectangle

    My form has a picturebox that draw a shape with mouse down and up.
    Name:  AAA.gif
Views: 460
Size:  207.5 KB
    my problem is the location of text"020"! I want to draw it in center of rectangle.
    please guide me. my codes are:
    Code:
    Imports System.Drawing.Drawing2D
    
    Public Class Form4
    
        Private NewSegment As Segment
        Private Segments As New List(Of Segment)
        Private Sub PictureBox1_MouseDown(sender As Object, e As MouseEventArgs) Handles PictureBox1.MouseDown
    
            NewSegment = New Segment(e.Location, e.Location)
            Segments.Add(NewSegment)
            PictureBox1.Invalidate()
        End Sub
    
        Private Sub PictureBox1_MouseMove(sender As Object, e As MouseEventArgs) Handles PictureBox1.MouseMove
            If NewSegment Is Nothing Then Return
            NewSegment.Point2 = e.Location
            PictureBox1.Invalidate()
        End Sub
    
        Private Sub PictureBox1_MouseUp(sender As Object, e As MouseEventArgs) Handles PictureBox1.MouseUp
            NewSegment = Nothing
            PictureBox1.Invalidate()
        End Sub
    
        Private Sub PictureBox1_Paint(sender As Object, e As PaintEventArgs) Handles PictureBox1.Paint
            e.Graphics.SmoothingMode = Drawing2D.SmoothingMode.AntiAlias
            For Each segment As Segment In Segments
                segment.Draw(e.Graphics, Nothing)
            Next
        End Sub
    End Class
    
    Class Segment
        Private Const RADIUS As Integer = 15
        Private Shared rc_r As New Rectangle(New Point(-RADIUS, -RADIUS), New Size(RADIUS * 1.5, RADIUS * 1.5)) 'for rectangle
        Private pt1, pt2 As Point
        Private angle As Double = 0
        Private length As Double = 0
        Private Shared pen1 As New Pen(Color.Black, 1.5)
        Private Shared drawFontF As New Font("times new roman", 8, style:=FontStyle.Bold)
        Private Shared SF As StringFormat
        Public Sub New(startPoint As Point, endPoint As Point)
            Point1 = startPoint
            Point2 = endPoint
        End Sub
    
        Public Property Point1 As Point
            Get
                Return pt1
            End Get
            Set(value As Point)
                pt1 = value
                UpdateAngleAndLength()
            End Set
        End Property
    
        Public Property Point2 As Point
            Get
                Return pt2
            End Get
            Set(value As Point)
                pt2 = value
                UpdateAngleAndLength()
            End Set
        End Property
    
        Private Sub UpdateAngleAndLength()
            angle = Math.Atan2(Point2.Y - Point1.Y, Point2.X - Point1.X) * 180.0 / Math.PI
            length = Math.Sqrt(Math.Pow(Point2.X - Point1.X, 2) + Math.Pow(Point2.Y - Point1.Y, 2))
        End Sub
        Public Sub Draw(gr As System.Drawing.Graphics, R As Rectangle)
    
            If Point1 = Point2 Then Return
            ' save the current state of the graphics
            Dim curState As GraphicsState = gr.Save()
    
            ' move the origin to the start point of the line
            gr.TranslateTransform(Point1.X, Point1.Y)
            ' rotate the whole surface
            gr.RotateTransform(angle)
    
            ' draw the line on the x-axis
            gr.DrawLine(pen1, 0, 0, CInt(length), 0)
    
            ' move the origin along the x-axis to where the center of the circle should be
            gr.TranslateTransform(length + RADIUS, 0)
            gr.DrawRectangle(pen1, rc_r) ' for rectangle
    
            ' orient back to the "normal" so the 90 is upright
            gr.RotateTransform(-angle)
            ' draw the 90
            Dim rcS As New Rectangle(New Point(-RADIUS, -RADIUS), New Size(RADIUS * 1.5, RADIUS * 1.5))
    
            gr.DrawString("020", drawFontF, Brushes.DimGray, rcS, SF)
    
            gr.Restore(curState)
        End Sub
    End Class
    how change 'RCS' ?
    Last edited by user20; Dec 9th, 2022 at 04:47 AM.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: draw string in center of a rectangle

    Read the documentation for that DrawString method, including the other overloads, and you'll find the answer. ALWAYS read the documentation. You can go directly to it by clicking the member name in code and pressing F1.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width