تقييم الموضوع :
  • 0 أصوات - بمعدل 0
  • 1
  • 2
  • 3
  • 4
  • 5
طريقة لكيفية طباعة MSFlexGrid مباشرة
#1
كاتب الموضوع : أحمد جمال

بسم الله الرحمن الرحيم .
السلام علكيم ورحمة الله وبركاته .

هذا الموضوع كنت قد طرحته في المنتدى السابق لطباعة MSFlexGrid على أي حجم ورق مطلوب أو بالعرض أو غير ذلك مما لا تنفع فيها استخدام أدوات التقارير ، هنا سوف نستخدم كائن الطباعة Printer مع مجموعة من خصائصه ووظائفه ، والتي بإمكانك التوسع فيها عن طريق مراجعة درس الاستاذ تركي العسيري : كائن الطباعة للمبتدئين .
وقد حصلت من المنتدى على كود ينظم مثل هذه العملية للطباعة ، وقد عدلت فيه أشياء بسيطة في أحد برامجي وسأسرد الكود مع شرح موجز :

* ضبط مجموعة من خصائص الطباعة مثل الخط والمحاذاة :


كود :
Printer.RightToLeft = True
Printer.FontBold = True: Me.FontName = "Traditional Arabic": Printer.FontName = Me.FontName

* اضفنا مصفوفة من المتغيرات لنحدد بها أبعاد الطباعة لاحقاً :

كود :
Dim Plt(3)

* نصفر أحد اعضاء هذه المصفوفة ، ونبدأ بجمع العرض الكلي للجدول ونضعه فيه :

كود :
Plt(2) = 0
For I = 0 To MSFlexGrid.Cols - 1
Plt(2) = Plt(2) + MSFlexGrid.ColWidth(C) '**** The Grid Width ****
Next I

* نحدد مكان الجهة اليسرى في الطباعة - أي سنبدأ من اليمين ، وبطول معين حتى نصل إلى نقطة نهاية الطباعة في اليسار - .
ونحصل على هذه النقطة بعملية حسابية بسيطة ، ونخزنها في أحد المتغيرات من المصفوفة التي عرفناها .


كود :
If Printer.ScaleWidth >= Plt(2) Then '**** Left Border position ****
Shemal = (Printer.ScaleWidth - Plt(2)) / 2
Else
Shemal = Printer.ScaleLeft + 500
End If

كود :
[FONT=Tahoma]Plt(0) = Shemal '**** Left Border position ****
[/FONT]

* نحدد النص الذي نريد طباعته ، وليكن البسملة :

كود :
mytext = "بسم الله الرحمن الرحيم"

* والآن نحدد منتصف الصفحة بعملية حسابية :

كود :
Printer.CurrentY = 150
txtWidth = Me.TextWidth(mytext)
Printer.CurrentX = (Printer.ScaleWidth - txtWidth) / 2 '**** Alligne Title in the Center ****

* وهكذا نطبع النص المطلوب بالشكل التالي :

كود :
Printer.Print mytext

* والآن نغير المكان الذي نطبع منه مجدداً لبدء عملية طباعة الجدول ، مع تغيير بعض الخصائص لتختلف عن خصائص الكلمة الأولى :

كود :
p = Shemal + 50
Plt(1) = Printer.CurrentY
Printer.FontSize = 12

كود :
[FONT=Tahoma]Printer.DrawStyle = 0: Printer.DrawWidth = 5 '**** To print a line ****
Printer.Line (Plt(0), Printer.CurrentY)-(Plt(2) + Plt(0) + 50, Printer.CurrentY)
[/FONT]

* والآن نبدأ بحلقة تكرار بعدد صفوف الجدول :

كود :
For C = 0 To MSFlexGrid.Cols - 1

والآن ، سنمر على حقل وراء الآخر ونطبعه ، مع مراعاة أن بعض الأعمدة أعرض من بعض ، فمثلاً لو كان العمود الأول مختلفاً عنهم جميعاً فسنضعه في شرط خاص بالشكل التالي :

كود :
If C = 0 Then
p = p + 50
Printer.CurrentX = p
Printer.Print MSFlexGrid.TextMatrix(0, C);
p = p - 50
p = p + MSFlexGrid.ColWidth(C)
C = C + 1
End If
لاحظ الخطوات التالية :
نزود قيمة P .
نضع مؤشر الطباعة على محور X عند P .
نطبع محتويات الحقل .
نعيد P إلى ما كانت عليه .
نزيد P بمقدار عرض العمود .
نزيد C بمقدار 1 .

* ونضع جملة أخرى إذا كانت C = MSFlexGrid.Cols - 41 .

كود :
If C = MSFlexGrid.Cols - 41 Then
p = p + 1200
Printer.CurrentX = p
Printer.Print MSFlexGrid.TextMatrix(0, C);
p = p - 1200
p = p + MSFlexGrid.ColWidth(C)
C = C + 1
End If

* وجملة أخيرة دون شروط :

كود :
Printer.CurrentX = p
Printer.Print Flall.TextMatrix(0, C);
p = p + MSFlexGrid.ColWidth(C)

* ونغلق حلقة التكرار :

كود :
Next C


وهكذا نكمل على نفس المنوال ، وفي المرة السابقة قمنا بطباعة النصوص ، ثم نطبع الخطوط ... وهكذا .
وقد يكون في شرحي بعض الخطأ بسبب أنني لست كاتب هذا الكود ، ولكنني سأرفق لك الكود كاملاً ، وطبق عليه بنفس الطريقة مع الجدول الذي لديك .

وهذا هو الكود بالكامل :


كود :
Private Sub Command1_Click()

كود :
[FONT=Tahoma]'*** This Code requires a Form with the MSFlexGrid and the CommonDialog Controls in it, to add it, Right Click the Tool Box \Components\ Microsoft FlexGrid Control 6.0 and the Microsoft Common Dialog Control 6.0
Dim C, R As Integer, Repl As String
Dim Plt(3), L, p, W, txtWidth, Shemal As Single[/FONT]
[FONT=Tahoma]Printer.RightToLeft = True
Printer.Orientation = 2[/FONT]
[FONT=Tahoma]On Error GoTo PrintErr '**** Initial the printer Setting ****
CommonDialog1.Orientation = cdlLandscape
CommonDialog1.CancelError = True: CommonDialog1.ShowPrinter: CommonDialog1.Flags = cdlPDPrintSetup
Printer.Orientation = CommonDialog1.Orientation: Printer.Copies = CommonDialog1.Copies
Printer.RightToLeft = True
Printer.FontBold = True: Me.FontName = "Traditional Arabic": Printer.FontName = Me.FontName[/FONT]
[FONT=Tahoma]Plt(2) = 0
For I = 0 To MSFlexGrid.Cols - 1
Plt(2) = Plt(2) + MSFlexGrid.ColWidth(C) '**** The Grid Width ****
Next I[/FONT]
[FONT=Tahoma]If Printer.ScaleWidth >= Plt(2) Then '**** Left Border position ****
Shemal = (Printer.ScaleWidth - Plt(2)) / 2
Else
Shemal = Printer.ScaleLeft + 500
End If[/FONT]
[FONT=Tahoma]Plt(0) = Shemal '**** Left Border position ****[/FONT]

[FONT=Tahoma]mytext = "بسم الله الرحمن الرحيم"
Printer.CurrentY = 150
txtWidth = Me.TextWidth(mytext)
Printer.CurrentX = (Printer.ScaleWidth - txtWidth) / 2 '**** Alligne Title in the Center ****
Printer.Print mytext[/FONT]
[FONT=Tahoma]p = Shemal + 50
Plt(1) = Printer.CurrentY
Printer.FontSize = 12[/FONT]
[FONT=Tahoma]Printer.DrawStyle = 0: Printer.DrawWidth = 5 '**** To print a line ****
Printer.Line (Plt(0), Printer.CurrentY)-(Plt(2) + Plt(0) + 50, Printer.CurrentY)[/FONT]
[FONT=Tahoma]Printer.CurrentY = Printer.CurrentY + 50 '**** Print the heading ****[/FONT]
[FONT=Tahoma]For C = 0 To MSFlexGrid.Cols - 1[/FONT]
[FONT=Tahoma]If C = 0 Then
p = p + 50
Printer.CurrentX = p
Printer.Print MSFlexGrid.TextMatrix(0, C);
p = p - 50
p = p + MSFlexGrid.ColWidth(C)
C = C + 1
End If[/FONT]
[FONT=Tahoma]If C = MSFlexGrid.Cols - 41 Then
p = p + 1200
Printer.CurrentX = p
Printer.Print MSFlexGrid.TextMatrix(0, C);
p = p - 1200
p = p + MSFlexGrid.ColWidth(C)
C = C + 1
End If[/FONT]
[FONT=Tahoma]Printer.CurrentX = p
Printer.Print MSFlexGrid.TextMatrix(0, C);
p = p + MSFlexGrid.ColWidth(C)[/FONT]
[FONT=Tahoma]Next C[/FONT]

[FONT=Tahoma]Printer.Print
Printer.DrawStyle = 0: Printer.DrawWidth = 5 '**** To print a line ****
Printer.Line (Plt(0), Printer.CurrentY)-(Plt(2) + Plt(0) + 50, Printer.CurrentY)
Me.FontBold = False: Printer.FontBold = False
Me.FontSize = 10: Printer.FontSize = Me.FontSize[/FONT]
[FONT=Tahoma]For R = 1 To MSFlexGrid.Rows - 1
If R = MSFlexGrid.Rows - 1 Then
Printer.CurrentY = Printer.CurrentY + 200
Else
Printer.CurrentY = Printer.CurrentY + 15

End If

p = Shemal + 50

For C = 0 To MSFlexGrid.Cols - 1 '**** To Allign the numbers to Right ****
If MSFlexGrid.TextMatrix(0, C) = "Price" Or MSFlexGrid.TextMatrix(0, C) Like "Qty*" Or MSFlexGrid.TextMatrix(0, C) = "Value" Then '**** The Column Head in Red For Numbers****
W = MSFlexGrid.ColWidth(C) - TextWidth(Me.MSFlexGrid.TextMatrix(R, C)) - 80
Printer.CurrentX = p + W '**** Add the (Column Width - Text Width) ****
Else
Printer.CurrentX = p
End If

If R = MSFlexGrid.Rows - 1 Then
p = p + 1000
Printer.CurrentX = p
Printer.FontSize = 12
Printer.Print MSFlexGrid.TextMatrix(R, C);
p = p - 1000
p = p + MSFlexGrid.ColWidth(C)
Else
Printer.Print MSFlexGrid.TextMatrix(R, C);
p = p + MSFlexGrid.ColWidth(C)
End If
Next C
Printer.Print
Printer.DrawStyle = 0: Printer.DrawWidth = 5
If R = MSFlexGrid.Rows - 1 Then
Printer.CurrentY = Printer.CurrentY + 200
End If
Printer.Line (Plt(0), Printer.CurrentY)-(Plt(2) + Plt(0) + 50, Printer.CurrentY)

GoTo NewP

Resu:[/FONT]
[FONT=Tahoma]If Printer.CurrentY > Printer.ScaleHeight - 1000 Then '**** If End Of Page ****

Printer.NewPage
'
Printer.Print[/FONT]

[FONT=Tahoma]Printer.CurrentY = 400
Plt(1) = Printer.CurrentY + 100
Plt(0) = Shemal
[/FONT]
[FONT=Tahoma]Printer.DrawStyle = 0
Printer.DrawWidth = 5 '**** To print a line ****
Printer.Line (Plt(0), Plt(1))-(Plt(0) + Plt(2) + 50, Plt(1))

Printer.CurrentY = Printer.CurrentY + 50 '**** Print the heading ****
Printer.FontBold = True: Me.FontSize = 12: Printer.FontSize = Me.FontSize

p = Shemal + 50
For C = 0 To MSFlexGrid.Cols - 1[/FONT]
[FONT=Tahoma]If C = 0 Then '"?"
p = p + 50
Printer.CurrentX = p
Printer.Print MSFlexGrid.TextMatrix(0, C);
p = p - 50
p = p + MSFlexGrid.ColWidth(C)
C = C + 1
End If[/FONT]
[FONT=Tahoma]If C = MSFlexGrid.Cols - 41 Then '"?????"
p = p + 1200
Printer.CurrentX = p
Printer.Print MSFlexGrid.TextMatrix(0, C);
p = p - 1200
p = p + MSFlexGrid.ColWidth(C)
C = C + 1
End If[/FONT]

[FONT=Tahoma]Printer.CurrentX = p
Printer.Print MSFlexGrid.TextMatrix(0, C);
p = p + MSFlexGrid.ColWidth(C)[/FONT]
[FONT=Tahoma]Next C
Printer.Print
Printer.DrawStyle = 0: Printer.DrawWidth = 5 '**** To print a line ****
Printer.Line (Plt(0), Printer.CurrentY)-(Plt(0) + Plt(2) + 50, Printer.CurrentY)
Printer.FontBold = False: Printer.FontSize = 10
End If
Next R[/FONT]
[FONT=Tahoma]Printer.EndDoc[/FONT]
[FONT=Tahoma]Exit Sub[/FONT]
[FONT=Tahoma]NewP:
L = Shemal + Plt(2) - 400
Plt(3) = Printer.CurrentY
Printer.DrawStyle = 0
Printer.DrawWidth = 5[/FONT]
[FONT=Tahoma]Printer.Line (Plt(2) + Plt(0) + 50, Plt(1))-(Plt(2) + Plt(0) + 50, Plt(3)) '**** Right Line ****
Printer.Line (Plt(0), Plt(1))-(Plt(0), Plt(3)) '**** Left Line ****[/FONT]
[FONT=Tahoma]Printer.DrawStyle = 0: Printer.DrawWidth = 5[/FONT]
[FONT=Tahoma]For C = 1 To MSFlexGrid.Cols - 1
If C = 9 Or C = 17 Or C = 25 Or C = 34 Then
Printer.DrawStyle = 0: Printer.DrawWidth = 15
Else
Printer.DrawStyle = 0: Printer.DrawWidth = 5
End If
Printer.Line (L, Plt(1))-(L, Plt(3)) '**** Vertical Lines ****
L = L - MSFlexGrid.ColWidth(C)
Next C[/FONT]
[FONT=Tahoma]GoTo Resu[/FONT]

[FONT=Tahoma]PrintErr:
If Err.Number = 32755 Then '**** The Printing is canceled ****
MsgBox "Printing is Terminated", vbInformation, "Stop Printing..."
Else
MsgBox Err.Description, vbInformation, "Error in Printing..."
Exit Sub

End If[/FONT]
[FONT=Tahoma]End Sub
[/FONT]


والله الموفق ....
والسلام عليكم ورحمة الله وبركاته .
}}}
تم الشكر بواسطة: Amir_Alzubidy



التنقل السريع :


يقوم بقرائة الموضوع: بالاضافة الى ( 1 ) ضيف كريم