余白を設定する(WindowsApiを使用)


RichTextBoxを使用する場合はコチラを参照


TextBoxで余白(Labelで言うとPadding)を設定するにはWindows ApiのSendMessageAで&HB3を送る必要がある。

具体的な方法はこんな感じ。

例)余白を設定可能なカスタムTextBoxをTextBoxを継承して作成。PaddingTextプロパティで余白を設定可能。
※Multiline = Trueにしないと実現は不可能です。
Public Class TextBoxP
   Inherits System.Windows.Forms.TextBox

   Private Declare Function SendMessageA Lib "User32.dll" ( _
          ByVal window As IntPtr, ByVal msg As Integer, _
          ByVal wParam As IntPtr, ByRef lParam As Rect) As Integer
   Const EM_SETRECT As Integer = &HB3

   Public Sub New()
       InitializeComponent()
   End Sub
   '初期化
   Private Sub InitializeComponent()
       Me.SuspendLayout()
       '
       'tboxEx
       '
       Me.Multiline = True
       Me.ResumeLayout(False)
   End Sub

   Private m_PaddingText As Padding = New Padding(0, 0, 0, 0)
   Public Property PaddingText() As Padding
       Get
           Return m_PaddingText
       End Get
       Set(ByVal Value As Padding)
           If m_PaddingText <> Value Then
               Me.m_PaddingText = Value
               Dim r As New Rect()
               r.Left = Value.Left
               r.Top = Value.Top
               r.Right = Me.Width - Value.Right
               r.Bottom = Me.Height - Value.Bottom
               SendMessageA(Me.Handle, EM_SETRECT, 0, r)
           End If
       End Set
   End Property

   Public Structure Rect
       Public Left As Integer
       Public Top As Integer
       Public Right As Integer
       Public Bottom As Integer
   End Structure
End Class






























.
最終更新:2008年07月18日 11:58