Visual Basic.NET - Imagen en un TextBox

   
Vista:

Imagen en un TextBox

Publicado por Preguntador VB.NET (22 intervenciones) el 06/07/2008 13:00:29
Buenas, buscando por internet encontre este interesante codigo que, supuestamente, te permite insertar una imagen de fondo en un textbox, pero no estoy seguro de como utilizarlo, ya que no se como utilizar las propiedades para ulizarlas en un textbox ya creado.

Gracias de antemano.

Os pongo el codigo:

Imports System
Imports System.Drawing
Public Class ImageTextBox
Inherits System.Windows.Forms.TextBox
#Region " Windows Form Designer generated code "
Public Sub New()

MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
SetStyle(ControlStyles.AllPaintingInWmPaint, True)
SetStyle(ControlStyles.DoubleBuffer, True)
SetStyle(ControlStyles.ResizeRedraw, True)
End Sub
'UserControl overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)

If disposing Then

If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
components = New System.ComponentModel.Container()
End Sub
#End Region
Private _backImage As Image = Nothing
Private _imageAlign As ContentAlignment = ContentAlignment.TopLeft
#Region " Public Properties"
Public Property BackImage() As System.Drawing.Image
Get
Return _backImage
End Get
Set(ByVal Value As Image)
_backImage = Value
If Me.DesignMode = True Then
Me.Invalidate()
End If
End Set
End Property
Public Property ImageAlign() As ContentAlignment
Get
Return _imageAlign
End Get
Set(ByVal Value As ContentAlignment)
_imageAlign = Value
If Me.DesignMode = True Then
Me.Invalidate()
End If
End Set
End Property
#End Region
#Region " WndProc Overrides Sub"

Protected Overrides Sub WndProc(ByRef m As Message)
MyBase.WndProc(m)
Dim g As Graphics = MyBase.CreateGraphics
If m.Msg = &HF Then
g.FillRectangle(New SolidBrush(MyBase.BackColor), 0, 0, MyBase.Width, MyBase.Height)
If Not _backImage Is Nothing Then
Dim rect As New Rectangle()
'find rect.X
Select Case ImageAlign
Case ContentAlignment.BottomLeft, ContentAlignment.MiddleLeft, ContentAlignment.TopLeft
rect.X = 0
Case ContentAlignment.BottomCenter, ContentAlignment.MiddleCenter, ContentAlignment.TopCenter
rect.X = CInt((MyBase.Width - _backImage.Width) / 2)
Case ContentAlignment.BottomRight, ContentAlignment.MiddleRight, ContentAlignment.TopRight
rect.X = MyBase.Width - _backImage.Width
End Select

'find rect.Y
Select Case ImageAlign
Case ContentAlignment.TopCenter, ContentAlignment.TopLeft, ContentAlignment.TopRight
rect.Y = 0
Case ContentAlignment.MiddleCenter, ContentAlignment.MiddleLeft, ContentAlignment.MiddleRight
rect.Y = CInt((MyBase.Height - _backImage.Height) / 2)
Case ContentAlignment.BottomCenter, ContentAlignment.BottomLeft, ContentAlignment.BottomRight
rect.Y = MyBase.Height - _backImage.Height
End Select
rect.Width = _backImage.Width
rect.Height = _backImage.Height
g.DrawImage(_backImage, rect)
End If
If Not MyBase.Text Is Nothing Then
Dim sFmt As New StringFormat()
Select Case MyBase.TextAlign

Case HorizontalAlignment.Left
sFmt.Alignment = StringAlignment.Near
Case HorizontalAlignment.Center
sFmt.Alignment = StringAlignment.Center
Case HorizontalAlignment.Right
sFmt.Alignment = StringAlignment.Far
End Select
Dim rectF As New RectangleF()
rectF.Width = MyBase.Width
rectF.Height = MyBase.Height
g.DrawString(MyBase.Text, MyBase.Font, New SolidBrush(MyBase.ForeColor), rectF, sFmt)
sFmt.Dispose()
End If
End If
If Not g Is Nothing Then g.Dispose()
End Sub
#End Region
#Region " OnTextChanged Overrides Sub"
Protected Overrides Sub OnTextChanged(ByVal e As System.EventArgs)
MyBase.OnTextChanged(New EventArgs())
Me.Invalidate()
End Sub
#End Region
#Region " OnTextAlignChanged Sub"
Protected Overrides Sub OnTextAlignChanged(ByVal e As System.EventArgs)
MyBase.OnTextAlignChanged(New EventArgs())
Me.Invalidate()
End Sub
#End Region
End Class
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder