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

私の推測では、改行がない場合は、文字列を1つの単語に分割する必要があります。

次に、単語を1つずつ連結して、長さを測定します。線幅を超える場合は、この文字列を描画して次の単語に進みます。

次の描画は、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 に答える