2012-06-11 7 views
2

가능합니까? TextBox에서 (가운데가 아닌) 텍스트를 맞 춥니 다? 나는 예를 들어TextBox에서 텍스트를 맞 춥니 다

txt.TextAlign = HorizontalAlignment.Left; 

을 알고 있습니다. 그러나 그것을 좌우로 정당화 할 수 있습니까?

+1

당신은 그것을 중심을 의미합니까? :) – Reniuz

+0

완전히 테스트되지 않았으므로 답변으로 게시하지는 않지만 HorizontalAlignment가 비트 단위인지 확실하지 않은 경우 | 연산자를 사용하여 왼쪽 및 오른쪽 정렬을 추가합니다. 비록 내가 왜 당신이 원하는지 또는 어떤 목적으로 끝나는지를 알지는 못하지만 그것이 유용 할 것입니다. – Skintkingle

+1

저는 중앙화가 아니라 칭의를 원합니다. –

답변

2

내가 무엇 OP가가는 것은이 http://en.wikipedia.org/wiki/Justification_(typesetting 생각) 다음이 제목

양쪽 맞춤 (평면에서 볼 권리 왼쪽 및 오른쪽)

나는 쉽게 이것을 할 수있는 방법이 없습니다. Damith가 제안하고 사용자 정의 컨트롤을 만들 때해야 할 것입니다. 문자를 입력 할 때마다 텍스트 간격이 줄어들고 재미있을 것입니다. 읽기 전용 텍스트 상자를 원한다면 할 수는 있지만 사용자가 텍스트를 추가하기를 원한다면 ... 이것을 구현하는 데 시간이 걸릴 큰 이유가 필요하다고 생각합니다.

0

정렬에 정당화하기로 센터에 제공 할 수 있습니다 왼쪽과

textBox1.TextAlign = HorizontalAlignment.Center; 
+1

해당 텍스트를 센터링하지 않습니다. 양쪽 맞춤은 센터링과 동일하지 않습니다. –

+1

@downvoter이 질문에 downvote 공평하지, 그의 대답은 여기에 그의 질문을 수정하고 그가 센터를 원하지 않는다고 선언했다. –

+1

텍스트를 정당화하는 것은 초등 학교에서 가르치는 간단한 개념입니다. 설명이 필요하지 않아야합니다. –

2

이것은 미끄러운 경사면이며 이미 표시된대로 사용자 지정 컨트롤이 실제로 필요합니다. 그러나 나와 같은 사람이라면 정보 상자 나 다른 대화 상자와 같은 텍스트 상자 데이터를 표시하기를 원한다면 PictureBox를 사용하고 실제 텍스트 상자를 숨겨 두면서이 대화 상자를 렌더링하면됩니다. 간격을위한 픽셀 너비를 나타 내기 위해 공백을 특별한 코드로 대체해야합니다. 수십 개의 프린터 드라이버의 베틀란이기 때문에 이것은 나에게 오래된 모자입니다. 이 웹 페이지에서 얼마나 잘 렌더링 될지 확신하지는 않지만 아래에서 코드를 제공 할 것입니다. 어쩌면 그들은 포맷을 바꿀 수도 있고 그들이 묻는다면, 나는 그들에게 직선 코드를 보낼 것이다. 내가 Assembler와 C/C++ 개발자이긴하지만 VB.NET과 사랑에 빠졌고 VB로 작성했습니다 (C++로 2 주일 정도 걸렸습니다.).

JustifyTextBox() 메서드와 JustifyText_Paint() 이벤트로 구성된이 코드를 사용하려면 원하는 TextBox를 sinily 배치하고 원하는 위치에 서식을 지정하십시오. 그런 다음 JustifyTextBox (Me.MyTextBox)와 같은 TextBox를 제공하여 JustifyTextBox() 메서드를 호출하면 사용자는 꺼져 있습니다. 그것은 모든 PictureBox 객체 생성과 페인팅 작업을 처리합니다. 필요한만큼 TextBoxes에 적용 할 수 있습니다. 그것은 한 객체에 묶여 있지 않습니다 (객체 지향적 인 패러다임을 물리 칠 것입니다). 여기

는 두 가지 방법이 필요합니다

'******************************************************************************* 
' Method Name : JustifyTextBox 
' Copyright (c) David Ross GOben March 10, 2013. All rights reserved. 
' 
' Purpose  : Reformat spacing in a textbox to allow for variable spacing, exactly 
'    : as a printer driver will process spacing for justification. 
'    : Having written many dozens of printer drivers over the years, this 
'    : is old hat. By the way, though you can do this with a RichTextBox, 
'    : you really should consider interpreting the RTF code to properly 
'    : render everything else, otherwise, if you were to simply replace 
'    : the TextBox casting to a RichTextBox, it would only process the 
'    : data from its Text property, so pretty text and coloring and font 
'    : typeface, size, and enhancements will not display in the PictureBox. 
'******************************************************************************* 
' Set up your code to use the JustifyTextBox() method and the JustifyText_Paint() event like this: 
' 
' JustifyTextBox(TextBoxObjectToJustify) 'create a PictureBox control to justify the text, and hide this TextBox 
'            'This will also link the created PictureBox's Paint() event to JustifyText_Paint 
'------------------------------------------------------------------------------- 
' NOTE: You can duplicate the above code for as many text boxes as you require. 
'  Also, there is no need to render them ReadOnly, because the user will not 
'  be able to access them through the user itnerface. 
'******************************************************************************* 
Private Sub JustifyTextBox(ByRef TxtBox As TextBox) 
    Dim picImage As PictureBox = New PictureBox        'create a PictureBox object (The textBox data will be painted to this) 
    With picImage 
     .Location = TxtBox.Location           'locate the PictureBox to the provided TextBox control 
     .Size = TxtBox.Size             'size it to the textbox boundaries 
     .Tag = TxtBox              'save a reference to the textbox for use during printing 
     .Parent = TxtBox.Parent            'set parent so it will display when the parent is displayed 
     AddHandler picImage.Paint, AddressOf JustifyText_Paint    'attach an event handler to the picture box's paint event 
     TxtBox.Visible = False            'render the associated textbox invisible 
    End With 
    '======================================================================= 
    With TxtBox 
     Dim TextSize As Size = TextRenderer.MeasureText("W y", .Font)  'get pixel width of "W y" 
     Dim Yinc As Int32 = TextSize.Height         'get vertical spacing to increment lines 
     Dim SpcSize As Int32 = TextSize.Width        'save measurement (we will adust the Space Size variable property in a second) 
     TextSize = TextRenderer.MeasureText("Wy", .Font)     'get pixel width of "Wy", without the space 
     SpcSize = SpcSize - TextSize.Width - 1        'compute the width of a space (drop 1 more for proper rendering) 
     Dim SpcInit As Int32 = 128 + SpcSize        'initial size of space and ecoding of space codes (spacing size + 128) 

     Dim CurlineIdx, NxtLineIdx, cAsc As Int32       'used variables 
     Dim LastLine As Int32 = .GetLineFromCharIndex(Len(.Text))   'get the last line index of the TextBox, offset from zero 

     Dim Result As String = Nothing          'init encoded result 
     Dim Txt As String = Nothing           'string to hold each line of text in the TextBox 
     Dim Tmptext As String            'copy of Txt that is altered 

     For Idx As Int32 = 0 To LastLine         'process each line (offset from 0) 
      CurlineIdx = .GetFirstCharIndexFromLine(Idx)     'get start of the indicated line number line 
      If Idx = LastLine Then           'at the last line of text data in the TextBox (if so, we do not need to justify it? 
       Txt = RTrim(.Text.Substring(CurlineIdx))     'yes, so simply grab the text of the line from the start character index to the end of the text data 
       Tmptext = Txt            'make a copy to TmpText 
      Else               'otherwise... grab the current line of text from the TextBox 
       NxtLineIdx = .GetFirstCharIndexFromLine(Idx + 1)   'not the last line, so get the index to the start of the next line 
       Txt = RTrim(.Text.Substring(CurlineIdx, NxtLineIdx - CurlineIdx)) 'grab the text of the current line. Also remove any trailing spaces 
       If Len(Txt) <> 0 AndAlso VB.Right(Txt, 2) = vbCrLf Then  'does the current line end with CR/LF? 
        Tmptext = RTrim(VB.Left(Txt, Len(Txt) - 2))    'yes, so strip CR/LF and strip any spaced leading them. There is no need to justify this line 
       ElseIf Len(Txt) = 0 Then         'otherwise, if the text notains NO data... 
        Tmptext = Nothing          'then simply make the line blank and do nothing else 
       Else              'otherwise... we have to encode the line to justify it 
        Tmptext = Txt           'get a copy of this string, which we will use to encode 
        TextSize = TextRenderer.MeasureText(Tmptext, .Font)  'get its current size 
        Dim SpcNeeded As Int32 = .Width - TextSize.Width  'compute number of pixels to add 
        If SpcNeeded > 0 Then         'do we need to add spacing? (allow for negative result) 
         Tmptext = Join(Split(Tmptext, " "), Chr(SpcInit)) 'yes, so first replace spaces with special code for point-size spacing 
         Dim Idy As Int32 = 1        'init column start index (used to skip over leading spaces) 
         Do While Asc(Mid(Tmptext, Idy, 1)) > 127 
          Idy += 1          'skip past leading spaces (indent) 
         Loop 
         '--------------------------------------------------- 
         Dim AddedSpacing As Boolean = False     'set up a flag to deterct if we added any spacing to the line 
         Do While SpcNeeded <> 0        'now loop through and add a pixel to each space in the text until there are none left to add 
          For Idz As Int32 = Idy To Len(Tmptext)   'scan through line data 
           cAsc = Asc(Mid(Tmptext, Idz, 1))   'get code 
           If cAsc > 127 Then       'special space character? 
            cAsc += 1        'add 1 pixel to its size 
            Mid(Tmptext, Idz, 1) = Chr(cAsc)  'stuff result back 
            AddedSpacing = True 
            SpcNeeded -= 1       'drop 1 from count of spacing needed 
            If SpcNeeded = 0 Then     'have we added all the spaces we need? 
             Exit For       'yes, so do not add any more 
            End If 
           End If 
          Next 
          If Not AddedSpacing Then      'if we could not add any space... 
           Exit Do          'then exit the loop 
          End If 
          AddedSpacing = False 
          '----------------------------------------------- 
         Loop            'loop through the line again to add additional spacing 
        End If 
       End If 
      End If 
      '--------------------------------------------------------------- 
      Result &= vbCrLf & Tmptext          'add TmpText data, manipulated or not, to the Result accumulator string 
     Next                'process the next line of code 
     .Text = Mid(Result, 3)            'stuff the result back to the TextBox for storage, less the leading CR+LF 
    End With 
End Sub 

'******************************************************************************* 
' Method Name : JustifyText_Paint 
' Purpose  : Draw text from a textbox onto a PictureBox control (created in the 
'    : setup code), using special spacing codes inserted by JustifyTextBox(). 
'    : 
' NOTE  : A reference to the associated Textbox is stored in the PictureBox's Tag property 
'******************************************************************************* 
' NOTE: You may notice that we seem to add 1 pixel to the width of the space character, 
'  below, but we subtracted 1 pixel in the the JustifyTextBox() method. This is 
'  originally subtracting 1. It also allows the right margin to reamin at its 
'  straightest. I realize that this actually has to do something with the margins 
'  applied in the TextRender.MeasureText() process, and even though I have tried to 
'  set theTextFormatFlags to NoPadding and in various other ways, and even none 
'  at all (as it is now), it dors not make one bit of difference, at all. However, 
'  on average, with or without using the TextFormatFlags, if we subtract a pixel from 
'  the space size and subtract 6 (typical margin of 3 pixels on either side) from 
'  measuring each word, the alignment is about perfect. However, a side effect is 
'  that some long words seem to almost butt up against a fllowing word. A hack around 
'  that is to simply manually insert an extra space between these words in the textbox. 
'******************************************************************************* 
Private Sub JustifyText_Paint(sender As Object, e As PaintEventArgs) 
    With DirectCast(DirectCast(sender, PictureBox).Tag, TextBox) 
     Dim TextSize As Size = TextRenderer.MeasureText("X y", .Font)    'get size of sample text 
     Dim Yinc As Single = CSng(TextSize.Height)         'keep height for adding lines in the picturebox 
     Dim SpcSize As Single = CSng(TextSize.Width)        'save the length result 
     TextSize = TextRenderer.MeasureText("Xy", .Font)       'grab the same text, but without the space (drop 1 more for proper rendering) 
     SpcSize = SpcSize - CSng(TextSize.Width + 1)        'compute space width (we are actually subtracting an addtional pixel) 
     Dim txtMargin As Single = CSng(.Margin.Left + .Margin.Right)    'compute the margin allowance (MeasureText adds this to the width) 
     Dim PosnX As Single = CSng(e.ClipRectangle.Left)       'start postion to draw text in the picturebox 
     Dim PosnY As Single = CSng(e.ClipRectangle.Top) 

     Dim Tmptxt As String = Nothing            'init temporary text holder 
     Dim Brsh As New SolidBrush(.ForeColor)          'get the brush for the text color to use from the textbox 
     For Idx As Int32 = 1 To Len(.Text)           'scan each character in the master text 
      Dim iChar As Int32 = Asc(Mid(.Text, Idx, 1))       'grab the cascii code for the currently indexed character 
      Select Case iChar 
       Case Is > 127              'special spacing character? 
        If Len(Tmptxt) <> 0 Then          'yes, so dump the temp text if it has accumulated data 
         TextSize = TextRenderer.MeasureText(Tmptxt, .Font)   'first grab the size of the text 
         e.Graphics.DrawString(Tmptxt, .Font, Brsh, PosnX, PosnY) 'then draw it 
         PosnX += CSng(TextSize.Width - txtMargin)     'then bump the x offset, less the margin allowance 
         Tmptxt = Nothing           'clear the temp text buffer 
        End If 
        PosnX += CSng((iChar And 127))         'finally, bump the x offset by the spaceing code (pixel count + 128) 
       Case 13                'vbCr? 
        If Len(Tmptxt) <> 0 Then          'yes, so dump the temp text if it has accumulated data 
         e.Graphics.DrawString(Tmptxt, .Font, Brsh, PosnX, PosnY) 'then draw it 
         Tmptxt = Nothing           'clear the temp text buffer 
        End If 
        PosnX = CSng(e.ClipRectangle.Left)        'reset the X offset to the left side 
        PosnY += Yinc             'bump the line index to the next line position 
       Case 32                'Space? 
        If Len(Tmptxt) <> 0 Then          'yes, so dump the temp text if it has accumulated data 
         TextSize = TextRenderer.MeasureText(Tmptxt, .Font)   'first grab the size of the text 
         e.Graphics.DrawString(Tmptxt, .Font, Brsh, PosnX, PosnY) 'then draw it 
         PosnX += CSng(TextSize.Width - txtMargin)     'then bump the x offset, less the margin allowance 
         Tmptxt = Nothing           'clear the temp text buffer 
        End If 
        PosnX += SpcSize            'bump index by a space size 
       Case 10                'vbLf? If so, ignore it 
       Case Else               'normal character 
        Tmptxt &= Chr(iChar)           'add the character to the temp text buffer 
      End Select 
     Next 

     If Len(Tmptxt) <> 0 Then             'dump the temp text if it has accumulated data 
      e.Graphics.DrawString(Tmptxt, .Font, Brsh, PosnX, PosnY)    'then draw it 
     End If 
     Brsh.Dispose()                'finally, dispose of the used brush resource 
    End With 
End Sub 
관련 문제