05-10-12, 01:14 PM
الكود الخاص بالكونترول TransparentLabel والموجود بالمثال رقم 1 في المشاركة السابقة لكي يستفيد منه من لا يملكون نسخة الفيجوال 2010
فقط عليكم كتابة الكود في اي من مشروعتكم ثم عمل Build للكونترول ومن ثم ستجدون منه نسخة في صندوق الأدوات ويمكنكم التعامل معه مثلما تتعاملون مع أي كونترول أخر
يتم التحكم في شفافية الكونترول من خلال Opacity Property
تقبلوا تحياتي ولا تنسونا في دعائكم..............
أخوكم عمر
فقط عليكم كتابة الكود في اي من مشروعتكم ثم عمل Build للكونترول ومن ثم ستجدون منه نسخة في صندوق الأدوات ويمكنكم التعامل معه مثلما تتعاملون مع أي كونترول أخر
يتم التحكم في شفافية الكونترول من خلال Opacity Property
كود :
Imports System.Windows.Forms
Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.ComponentModel
Public Class TransparentLabel
Inherits Control
#Region " Fields "
Private _opacity As Double = 1.0
Private _imageAlighn As ContentAlignment = ContentAlignment.MiddleCenter
Private _textAlign As ContentAlignment = ContentAlignment.TopLeft
Private _image As Image = Nothing
#End Region
#Region " Constructor "
Public Sub New()
MyBase.SetStyle(ControlStyles.SupportsTransparentBackColor, True)
MyBase.SetStyle(ControlStyles.Opaque, False)
MyBase.SetStyle(ControlStyles.DoubleBuffer, True)
MyBase.SetStyle(ControlStyles.AllPaintingInWmPaint, True)
MyBase.SetStyle(ControlStyles.UserPaint, True)
MyBase.SetStyle(ControlStyles.ResizeRedraw, True)
MyBase.UpdateStyles()
End Sub
#End Region
#Region " Properties "
<Browsable(True)> _
<EditorBrowsable(System.ComponentModel.EditorBrowsableState.Always)> _
<DefaultValue(GetType(Double), "1")> _
<TypeConverter(GetType(OpacityConverter))> _
<Description("Set the opacity percentage of the control.")> _
<Category("Aero")> _
Public Property Opacity() As Double
Get
Return Me._opacity
End Get
Set(ByVal value As Double)
If value = Me._opacity Then
Return
End If
Me._opacity = value
Me.UpdateStyles()
Me.Invalidate()
End Set
End Property
<Browsable(True)> _
<EditorBrowsable(EditorBrowsableState.Always)> _
<DefaultValue(GetType(ContentAlignment), "MiddleCenter")> _
<Description("Set the position of image within the control.")> _
<Category("Aero")> _
Public Property ImageAlign As ContentAlignment
Get
Return Me._imageAlighn
End Get
Set(ByVal value As ContentAlignment)
Me._imageAlighn = value
Invalidate()
End Set
End Property
<Browsable(True)> _
<EditorBrowsable(EditorBrowsableState.Always)> _
<DefaultValue(GetType(ContentAlignment), "TopLeft")> _
<Description("Set the position of text within the control.")> _
<Category("Aero")> _
Public Property TextAlign As ContentAlignment
Get
Return Me._textAlign
End Get
Set(ByVal value As ContentAlignment)
Me._textAlign = value
Invalidate()
End Set
End Property
<Browsable(True)> _
<EditorBrowsable(EditorBrowsableState.Always)> _
<Description("Set the image of the control.")> _
<Category("Aero")> _
Public Property Image As Image
Get
Return Me._image
End Get
Set(ByVal value As Image)
Me._image = value
Invalidate()
End Set
End Property
Public Overloads Property ForeColor As Color
Get
Return MyBase.ForeColor
End Get
Set(ByVal value As Color)
MyBase.ForeColor = value
Invalidate()
End Set
End Property
<Browsable(True), EditorBrowsable(EditorBrowsableState.Always), Localizable(True)> _
Public Overrides Property Text As String
Get
Return MyBase.Text
End Get
Set(ByVal value As String)
MyBase.Text = value
Invalidate()
End Set
End Property
#End Region
#Region " Methods "
Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
InvokePaintBackground(Me, e)
Dim rect As Rectangle = MyBase.ClientRectangle
Using sb As New SolidBrush(Color.FromArgb(Me.BackColor.A * Me.Opacity, Me.BackColor))
e.Graphics.FillRectangle(sb, Me.ClientRectangle)
rect.Width -= 1
rect.Height -= 1
e.Graphics.DrawRectangle(New Pen(sb), rect)
End Using
Dim borderRect As Rectangle = MyBase.ClientRectangle
borderRect.Width -= 1
borderRect.Height -= 1
If Me.Image IsNot Nothing Then
Me.DrawImage(e.Graphics, Image, borderRect, MyBase.RtlTranslateAlignment(Me.ImageAlign))
End If
If Me.Text IsNot Nothing Then
Dim format As New StringFormat()
format.LineAlignment = StringAlignment.Center
format.Trimming = StringTrimming.EllipsisCharacter
Select Case Me._textAlign
Case ContentAlignment.BottomCenter
format.Alignment = StringAlignment.Center
format.LineAlignment = StringAlignment.Far
Exit Select
Case ContentAlignment.BottomLeft
format.Alignment = StringAlignment.Near
format.LineAlignment = StringAlignment.Far
Exit Select
Case ContentAlignment.BottomRight
format.Alignment = StringAlignment.Far
format.LineAlignment = StringAlignment.Far
Exit Select
Case ContentAlignment.MiddleCenter
format.Alignment = StringAlignment.Center
format.LineAlignment = StringAlignment.Center
Exit Select
Case ContentAlignment.MiddleLeft
format.Alignment = StringAlignment.Near
format.LineAlignment = StringAlignment.Center
Exit Select
Case ContentAlignment.MiddleRight
format.Alignment = StringAlignment.Far
format.LineAlignment = StringAlignment.Center
Exit Select
Case ContentAlignment.TopCenter
format.Alignment = StringAlignment.Center
format.LineAlignment = StringAlignment.Near
Exit Select
Case ContentAlignment.TopLeft
format.Alignment = StringAlignment.Near
format.LineAlignment = StringAlignment.Near
Exit Select
Case ContentAlignment.TopRight
format.Alignment = StringAlignment.Far
format.LineAlignment = StringAlignment.Near
Exit Select
End Select
Using textBrush As New SolidBrush(MyBase.ForeColor)
e.Graphics.DrawString(MyBase.Text, MyBase.Font, textBrush, borderRect, format)
End Using
End If
End Sub
Protected Overrides Sub OnPaintBackground(ByVal e As System.Windows.Forms.PaintEventArgs)
Dim grContainer As GraphicsContainer = e.Graphics.BeginContainer()
e.Graphics.TranslateTransform(-Me.Left, -Me.Top)
Using pe As PaintEventArgs = New PaintEventArgs(e.Graphics, e.ClipRectangle)
InvokePaintBackground(Me.Parent, pe)
InvokePaint(Me.Parent, pe)
End Using
e.Graphics.ResetTransform()
e.Graphics.EndContainer(grContainer)
End Sub
Protected Overrides Sub OnMove(ByVal e As System.EventArgs)
MyBase.OnMove(e)
Me.Invalidate()
End Sub
Protected Overrides Sub OnResize(ByVal e As System.EventArgs)
MyBase.OnResize(e)
Me.Invalidate()
End Sub
Private Function GetImageRectangle(ByVal img As Image, ByVal rect As Rectangle, ByVal align As ContentAlignment) As Rectangle
Dim imageSize As Size = img.Size
Dim x As Integer = (rect.X + 2)
Dim y As Integer = (rect.Y + 2)
If ((align And (ContentAlignment.BottomRight Or (ContentAlignment.MiddleRight Or ContentAlignment.TopRight))) <> DirectCast(0, ContentAlignment)) Then
x = (((rect.X + rect.Width) - 4) - imageSize.Width)
ElseIf ((align And (ContentAlignment.BottomCenter Or (ContentAlignment.MiddleCenter Or ContentAlignment.TopCenter))) <> DirectCast(0, ContentAlignment)) Then
x = (rect.X + ((rect.Width - imageSize.Width) / 2))
End If
If ((align And (ContentAlignment.BottomRight Or (ContentAlignment.BottomCenter Or ContentAlignment.BottomLeft))) <> DirectCast(0, ContentAlignment)) Then
y = (((rect.Y + rect.Height) - 4) - imageSize.Height)
ElseIf ((align And (ContentAlignment.TopRight Or (ContentAlignment.TopCenter Or ContentAlignment.TopLeft))) <> DirectCast(0, ContentAlignment)) Then
y = (rect.Y + 2)
Else
y = (rect.Y + ((rect.Height - imageSize.Height) / 2))
End If
Return New Rectangle(x, y, imageSize.Width, imageSize.Height)
End Function
Private Sub DrawImage(ByVal g As Graphics, ByVal img As Image, ByVal imageRect As Rectangle, ByVal align As ContentAlignment)
Dim r As Rectangle = Me.GetImageRectangle(img, imageRect, align)
g.DrawImage(img, r.X, r.Y, img.Width, img.Height)
End Sub
#End Region
End Class ' TransparentLabelتقبلوا تحياتي ولا تنسونا في دعائكم..............
أخوكم عمر
