A picturebox object that supports a label (transparent).
this allow to create a piture button and add text it.
This control tested and working :)
use it with wisdom... or not:
have fun:
public class PictureText : PictureBox, ISupportInitialize
{
#region #region Private Data-Members
private string _textDisplayed = string.Empty;
private Font _textFont = new System.Drawing.Font("Arial", 10F, System.Drawing.FontStyle.Bold);
private Color _textForeColor = Color.Black;
#endregion
public string TextDisplayed
{
get { return _textDisplayed; }
set
{
_textDisplayed = value;
this.Invalidate();
}
}
public Font TextFont
{
get { return _textFont; }
set
{
_textFont = value;
this.Invalidate();
}
}
public Color TextForeColor
{
get { return _textForeColor; }
set
{
_textForeColor = value;
this.Invalidate();
}
}
//protected override void OnPaintBackground(PaintEventArgs e)
//{
// base.OnPaintBackground(e);
// //HorizontalAlignment.
// if (string.IsNullOrEmpty(TextDisplayed) == false)
// {
// e.Graphics.DrawString(this.TextDisplayed, this.TextFont, new SolidBrush(this.TextForeColor), this.Location.X, this.Location.Y);
// }
//}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
if (string.IsNullOrEmpty(TextDisplayed) == false)
{
using (StringFormat sf = new StringFormat())
{
sf.Alignment = StringAlignment.Center;
sf.LineAlignment = StringAlignment.Center;
e.Graphics.DrawString(this.TextDisplayed, this.TextFont, new SolidBrush(this.TextForeColor), this.Width / 2, this.Height / 2, sf);
}
}
}
#region ISupportInitialize Members
public void BeginInit()
{
}
public void EndInit()
{
}
#endregion
}
Thank you for this, it helped me. Although, I made a few changes including converting it to VB and introducing a TextAlign property. Here is my code:
ReplyDeleteImports System.ComponentModel
Public Class HybridPictureBox
Inherits PictureBox
Implements ISupportInitialize
#Region "#region Private Data-Members"
Private _textDisplayed As String = String.Empty
Private _textFont As Font = New System.Drawing.Font("Arial", 10.0F, System.Drawing.FontStyle.Bold)
Private _textForeColor As Color = Color.Black
Private _textDisplayArea As RectangleF = New RectangleF(0, 0, Me.Width, Me.Height)
Private _textAlign As ContentAlignment = ContentAlignment.TopLeft
#End Region
Public Property TextDisplayed() As String
Get
Return _textDisplayed
End Get
Set(ByVal value As String)
_textDisplayed = value
Me.Invalidate()
End Set
End Property
Public Property TextFont() As Font
Get
Return _textFont
End Get
Set(ByVal value As Font)
_textFont = value
Me.Invalidate()
End Set
End Property
Public Property TextForeColor() As Color
Get
Return _textForeColor
End Get
Set(ByVal value As Color)
_textForeColor = value
Me.Invalidate()
End Set
End Property
Public Property TextAlign() As ContentAlignment
Get
Return _textAlign
End Get
Set(ByVal value As ContentAlignment)
_textAlign = value
Me.Invalidate()
End Set
End Property
Protected Overloads Overrides Sub OnPaint(ByVal e As PaintEventArgs)
MyBase.OnPaint(e)
If String.IsNullOrEmpty(TextDisplayed) = False Then
Using sf As New StringFormat()
Select Case Me.TextAlign
Case ContentAlignment.BottomCenter
sf.Alignment = StringAlignment.Center
sf.LineAlignment = StringAlignment.Far
Case ContentAlignment.BottomLeft
sf.Alignment = StringAlignment.Near
sf.LineAlignment = StringAlignment.Far
Case ContentAlignment.BottomRight
sf.Alignment = StringAlignment.Far
sf.LineAlignment = StringAlignment.Far
Case ContentAlignment.MiddleCenter
sf.Alignment = StringAlignment.Center
sf.LineAlignment = StringAlignment.Center
Case ContentAlignment.MiddleLeft
sf.Alignment = StringAlignment.Near
sf.LineAlignment = StringAlignment.Center
Case ContentAlignment.MiddleRight
sf.Alignment = StringAlignment.Far
sf.LineAlignment = StringAlignment.Center
Case ContentAlignment.TopCenter
sf.Alignment = StringAlignment.Center
sf.LineAlignment = StringAlignment.Near
Case ContentAlignment.TopLeft
sf.Alignment = StringAlignment.Near
sf.LineAlignment = StringAlignment.Near
Case ContentAlignment.TopRight
sf.Alignment = StringAlignment.Far
sf.LineAlignment = StringAlignment.Near
End Select
e.Graphics.DrawString(Me.TextDisplayed, Me.TextFont, New SolidBrush(Me.TextForeColor), _textDisplayArea, sf)
'e.Graphics.DrawString(Me.TextDisplayed, Me.TextFont, New SolidBrush(Me.TextForeColor), )
End Using
End If
End Sub
#Region "ISupportInitialize Members"
Public Sub BeginInit()
End Sub
Public Sub EndInit()
End Sub
#End Region
Private Sub HybridPictureBox_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Resize
_textDisplayArea = New RectangleF(0, 0, Me.Width, Me.Height)
Me.Invalidate()
End Sub
End Class