0

我在 VB.NET 中翻转文本时遇到问题 它已翻转但没有线闸

见链接: http ://www.spider-news.net/Flip_Text_question.JPG

Imports System.Drawing.Drawing2D
Imports System.Drawing


Public Class Form1

  Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint


    ' Draw the text and the surrounding rectangle START.
    Dim text1 As String = RichTextBox1.Text
    Dim font1 As New Font("Arial", 10, FontStyle.Bold, GraphicsUnit.Point)
    Try
        Dim rect1 As New Rectangle(10, 10, 1000, 140)

        ' Create a StringFormat object with the each line of text, and the block 
        ' of text centered on the page. 
        Dim stringFormat As New StringFormat()
        stringFormat.Alignment = StringAlignment.Center
        stringFormat.LineAlignment = StringAlignment.Center


        ' Draw the text and the surrounding rectangle.
        e.Graphics.DrawString(text1, font1, Brushes.Blue, rect1, stringFormat)
        e.Graphics.DrawRectangle(Pens.Black, rect1)


    Finally
        font1.Dispose()
    End Try
    ' Draw the text and the surrounding rectangle END.


    '' FLIP TEXT ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    ' Draw Flipped Text the text surrounding rectangle START.

    Using the_font As New Font("Arial", 20, FontStyle.Bold, GraphicsUnit.Point)

        DrawFlippedText(e.Graphics, the_font, Brushes.Black, 10, 10, RichTextBox1.Text, True, False)

        Dim txt_size As SizeF
        txt_size = e.Graphics.MeasureString(RichTextBox1.Text, the_font)
        e.Graphics.DrawRectangle(Pens.Red, 10, 10, txt_size.Width, txt_size.Height)

    End Using

    ' Draw Flipped Text the text surrounding rectangle END.
    '' FLIP TEXT ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
End Sub

Public Sub DrawFlippedText(ByVal gr As Graphics, ByVal the_font As Font, ByVal the_brush As Brush, ByVal x As Integer, ByVal y As Integer, ByVal txt As String, ByVal flip_x As Boolean, ByVal flip_y As Boolean)

    ' Save the current graphics state.
    Dim state As GraphicsState = gr.Save()

    ' Set up the transformation.
    Dim scale_x As Integer = IIf(flip_x, -1, 1)
    Dim scale_y As Integer = IIf(flip_y, -1, 1)
    gr.ResetTransform()
    gr.ScaleTransform(scale_x, scale_y)


    ' Figure out where to draw.
    Dim txt_size As SizeF = gr.MeasureString(txt, the_font)

    If flip_x Then x = -x - RichTextBox1.Size.Width
    If flip_y Then y = -y - RichTextBox1.Size.Height


    Dim rect1 As New Rectangle(10, 10, 1000, 140)
    Dim stringFormat As New StringFormat()
    stringFormat.Alignment = StringAlignment.Center
    stringFormat.LineAlignment = StringAlignment.Center



    ' Draw.
    gr.DrawString(txt, the_font, the_brush, x, y)

    ' Restore the original graphics state.
    gr.Restore(state)

End Sub


End Class

请帮忙

4

2 回答 2

0

我的猜测是,如果不存在换行符,则必须将字符串拆分为单个单词。

然后将单词一一连接并测量长度。如果它超出了您的线宽,请绘制此字符串并继续下一个单词。

下一次绘制应该在 y 坐标 + 你的行高上。

我在 pdf 中执行此操作,我将文本放置到可能超过 1 行的绝对位置:

 Dim splitted As String() = text.Split()
        Dim tempchunk As Chunk = New Chunk("", pdfFont)
        Dim count As Integer = 0
        For Each s As String In splitted
            Dim chunk2 As Chunk
            chunk2 = New Chunk(tempchunk.Content, pdfFont)
            chunk2.Append(" " & s)
            If chunk2.GetWidthPoint() > 155 Then
                cb.SaveState()
                cb.BeginText()
                cb.MoveText(x, y - (13 * count))
                cb.SetFontAndSize(bfont, 11)
                cb.ShowText(tempchunk.Content.Trim())
                cb.EndText()
                cb.RestoreState()
                tempchunk = New Chunk(s, pdfFont)
                count += 1
            Else
                tempchunk.Append(" " & s)
            End If
        Next
        If tempchunk.Content <> "" Then
            cb.SaveState()
            cb.BeginText()
            cb.MoveText(x, y - (13 * count))
            cb.SetFontAndSize(bfont, 11)
            cb.ShowText(tempchunk.Content.Trim())
            cb.EndText()
            cb.RestoreState()
        End If

它是pdf的代码,但也许有帮助

于 2013-03-04T09:29:37.090 回答
0

尝试这个。

我创建了一个位图,在那里绘制字符串和矩形,翻转它,然后在窗体上绘制位图(带有翻转的文本)。

    Public Class Form1
      Private Sub RichTextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RichTextBox1.TextChanged
        Dim b As New Bitmap(300, 100)
        Dim g As Graphics = Graphics.FromImage(b)
        Dim d As Graphics = Me.CreateGraphics

        Dim r As New Rectangle(0, 0, b.Width - 1, b.Height - 1)
        Dim f As New StringFormat
        f.Alignment = StringAlignment.Center
        f.LineAlignment = StringAlignment.Center

        g.Clear(BackColor)
        g.DrawRectangle(Pens.Red, r)
        g.DrawString(RichTextBox1.Text, RichTextBox1.Font, Brushes.Blue, r, f)
        b.RotateFlip(RotateFlipType.RotateNoneFlipX)
        d.DrawImageUnscaled(b, 10, 10)

        g.Dispose()
        b.Dispose()
        d.Dispose()
    End Sub
End Class
于 2013-03-04T11:42:52.130 回答