07-04-13, 11:17 PM
الحمد لله تم
فقط اتجاه عمودي او أفقي، لم أدعم بالزاوية لأنها تحتاج تفرغ
انسخ هذا الكود في PrintLabel
وهذا الكود في LabelPrinter
فقط اتجاه عمودي او أفقي، لم أدعم بالزاوية لأنها تحتاج تفرغ
انسخ هذا الكود في PrintLabel
PHP كود :
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
وهذا الكود في LabelPrinter
PHP كود :
Public Class LabelPrinter
Inherits Printer
Dim Text As String
Dim ptxt As String
Public Sub New(ByVal printControl As IPrintableControl)
MyBase.New(printControl)
End Sub
Private ReadOnly Property txt As PrintLabel
Get
Return MyBase.Control
End Get
End Property
ReadOnly Property man As PrintManager
Get
Return txt.Section.Page.PManager
End Get
End Property
Protected Friend Overrides Function Print(ByVal g As System.Drawing.Graphics, ByVal Bounds As System.Drawing.Rectangle) As Boolean
Dim sc = txt.SourceControl
'g.DrawRectangle(Pens.Red, Bounds)
Dim Completed As Boolean
'If First Printing
If Me.Text Is Nothing AndAlso sc.Text IsNot Nothing Then
Me.Text = sc.Text
End If
If Me.txt.IsPageNumber Then
Text = Me.txt.Text.Replace(PrintLabel.pn, man.CurPage).Replace(PrintLabel.pc, man.PageCount)
End If
If sc.Text IsNot Nothing Then
'Decleration
Dim Rec As Rectangle = Bounds
Dim StringFormat = New StringFormat
Dim hAlign As ContentAlignment = ContentAlignment.MiddleLeft
'For Measure the text
Dim CurText As String
Dim numChars As Integer
Dim numLines As Integer
'Alignment........
With StringFormat
.LineAlignment = GetYAlign(txt.TextAlign)
.Alignment = GetXAlign(txt.TextAlign)
.Trimming = StringTrimming.Word
Dim rtl = txt.RightToLeft
If rtl = Windows.Forms.RightToLeft.Yes Then
.FormatFlags = StringFormatFlags.DirectionRightToLeft
End If
End With
'Measure Text
'Dim s = Bounds.Size
's.Height -= txt.Font.GetHeight(g)
Rec.Size = g.MeasureString(Text, txt.Font, Rec.Size, StringFormat, numChars, numLines).ToSize()
's.Height += txt.Font.GetHeight(g)
'Get Max of te=t
CurText = Text.Substring(0, numChars)
'Adjust location by align
Rec = GetRect(Rec.Size, Bounds, txt.TextAlign, 0, 0)
'Auto Size not
'Vertical
If txt.FitMaxHeight Then
Rec.Height = Bounds.Height
Rec.Y = Bounds.Top
End If
'Horizontal
If txt.FitMaxWidth Then
Rec.Width = Bounds.Width
Rec.X = Bounds.X
End If
'Drawing:
'1- Fill Final Bounds
g.FillRectangle(New SolidBrush(txt.BackColor), Rec)
'2- Draw Border
If txt.BorderSize > 0 Then
Dim pn = New Pen(txt.BorderColor, txt.BorderSize)
pn.DashStyle = txt.Border
g.DrawRectangle(pn, Rec)
End If
'3- Draw Text
'g.DrawString(CurText, txt.Font, New SolidBrush(txt.ForeColor), Rec, StringFormat)
txt.DrawText(g, Rec, CurText)
'g.DrawRectangle(Pens.Red, Rec)
'Finishing is completed ?
If numChars < Me.Text.Length AndAlso Not Me.txt.OnePageOnly Then
ptxt = Me.Text
Me.Text = Text.Substring(numChars)
If ptxt Is Me.Text Then
Return True
End If
Completed = False
Else
Me.Text = txt.Text
Completed = True
End If
If txt.IsPageNumber Then
Return True
End If
Return Completed
End If
End Function
End Class
