Imports System.Drawing.Design
<ToolboxBitmap(GetType(Label))> _
Public Class PrintLabel
Inherits Label
Implements IPrintableControl
Friend Const pn As String = "[pn]"
Friend Const pc As String = "[pc]"
Public Sub New()
MyBase.AutoSize = False
MyBase.SetStyle(ControlStyles.UserPaint Or ControlStyles.OptimizedDoubleBuffer, True)
End Sub
#Region "Properties"
#Region "Appearance"
<Category("Printing Appearance: Common"), Description("مساواة العرض أثناء الطباعة بالعرض أثناء التصميم")> _
Public Property FitMaxWidth As Boolean = True
<Category("Printing Appearance: Common"), Description("مساواة الطول أثناء الطباعة بالطول أثناء التصميم")> _
Public Property FitMaxHeight As Boolean = True
<Category("Printing Appearance"), Description("شكل الإطار الخارجي")> _
Public Property Border() As DashStyle = DashStyle.Solid
<Category("Printing Appearance"), Description("سُمك الإطار الخارجي")> _
Public Property BorderSize() As Integer = 0
<Category("Printing Appearance"), Description("لون الإطار الخارجي")> _
Public Property BorderColor() As Color = Color.Black
<Category("Appearance"), Description("زاوية ميلان النص")> _
Public Property Direction() As TextDirection
#End Region
#Region "Printing Data"
Private _SourceControl As Label
<Browsable(False)> _
Public Property SourceControl As System.Windows.Forms.Control Implements IPrintableControl.SourceControl
Get
If _SourceControl Is Nothing Then
_SourceControl = Me
End If
Return _SourceControl
End Get
Set(ByVal value As System.Windows.Forms.Control)
_SourceControl = value
End Set
End Property
<Category("Appearance"), Description("تحديد ما إذا كان الحقل لعرض رقم الصفحة، استخدم خاصية النص للتنسيق ,[pn]=Page Number, [pc]=Page Count")> _
Public Property IsPageNumber As Boolean = False
#End Region
#Region "Printing Components"
Dim _Printer As LabelPrinter
<Browsable(False)> _
Public ReadOnly Property Printer As Printer Implements IPrintableControl.Printer
Get
If _Printer Is Nothing Then
_Printer = New LabelPrinter(Me)
End If
Return _Printer
End Get
End Property
<Browsable(False)> _
Public ReadOnly Property Section As Section Implements IPrintableControl.Section
Get
Return CType(Me.Parent, Section)
End Get
End Property
#End Region
#Region "Printing Behavior"
<Category("Printing Behavior"), Description("تفعيل مقياس الرسم الأفقي")> _
Public Property ScaleHorizontal As Boolean = True Implements IPrintableControl.ScaleHorizontal
<Category("Printing Behavior"), Description("تفعيل مقياس الرسم الرأسي")> _
Public Property ScaleVertical As Boolean = True Implements IPrintableControl.ScaleVertical
<Category("Printing Behavior"), Description("رتبة مرحلة الطباعة")> _
Public Property StageIndex As Integer Implements IPrintableControl.StageIndex
<Category("Printing Behavior"), Description("رتبة الأداة أثناء مرحلة الطباعة")> _
Public Property PrintIndex As Integer Implements IPrintableControl.PrintIndex
<Category("Printing Behavior"), Description("تحديد ما إذا كانت الأداة تستكمل طباعتها في صفحات تالية أو لا")> _
Public Property OnePageOnly As Boolean = False Implements IPrintableControl.OnePageOnly
#End Region
#End Region
#Region "Methods"
Protected Overrides Sub OnParentChanged(ByVal e As System.EventArgs)
If Me.Parent IsNot Nothing Then
If Not TypeOf Parent Is Section Then
Me.Parent.Controls.Remove(Me)
End If
End If
End Sub
Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
'e.Graphics.DrawRectangle(New Pen(Me.BorderColor, Me.BorderSize) With {.Alignment = PenAlignment.Center}, e.ClipRectangle)
DrawText(e.Graphics, e.ClipRectangle, Me.Text)
End Sub
Public Sub DrawText(ByVal g As Graphics, ByVal bounds As Rectangle, ByVal txt As String)
Dim rec = bounds
Dim fl As StringFormatFlags
If Me.RightToLeft = Forms.RightToLeft.Yes Then
fl = StringFormatFlags.DirectionRightToLeft
End If
If Me.Direction = TextDirection.Vertical Then
fl = fl Or StringFormatFlags.DirectionVertical
End If
DrawString(g, Me.Text, Me.Font, Me.ForeColor, rec, Me.TextAlign, fl)
End Sub
Public Sub DrawString(ByVal g As Graphics, ByVal Text As String, ByVal Font As Font, ByVal TextColor As Color, ByVal Rect As Rectangle, ByVal Align As ContentAlignment, ByVal FormatFlags As StringFormatFlags)
Try
Dim sf As New StringFormat
sf.Alignment = GetXAlign(Align)
sf.LineAlignment = GetYAlign(Align)
sf.FormatFlags = FormatFlags
g.DrawString(Text, Font, New SolidBrush(TextColor), Rect, sf)
Catch ex As Exception
End Try
End Sub
#End Region
End Class
Public Enum TextDirection
Horizontal
Vertical
End Enum