การพิมพ์ใน Visual Basic

อยากจะพิมพ์โน่น อยากจะพิมพ์นี่ ก็ทำได้อย่างง่ายดายด้วย VB
สำหรับการพิมพ์ใน VB นั้น Visual Basic มี Printer และ Printers Object อยู่ในตัว Runtime ของ Visual Basic อยู่แล้ว สำหรับ Printer Object จะควบคุม Output ของ Printer และ รับข้อมูลเกี่ยวกับเครื่อง Printer ได้เช่น Printer Name

สิ่งแรกที่ควรจะลองศึกษาดูก่อน คือสั่งในการสั่งพิมพ์ ตัวอย่าง : ถ้าเราต้องการพิมพ์ "Hello" เราจะต้องใช้คำสั่ง

Printer.Print "Hello"

เมื่อคุณสั่งพิมพ์แล้ว Output ของคุณจะเลื่อนไปยังบรรทัดต่อไป ดังนั้น

Printer.Print "Hello"
Printer.Print "Goodbye!"

จะแสดงออมา

Hello
Goodbye!

บนกระดาษ
ถ้าคุณต้องการกระดาษแผ่นใหม่คุณเพียงแค่ใช้คำสั่ง

Printer.NewPage

หลักจากที่สั่งพิมพ์ข้อมูลไปยังเครื่องพิมพ์คุณควรจะใช้คำสั่ง EndDoc :

Printer.EndDoc

และใน Object Printer นี้คุณยังสามารถเปลี่ยนค่าต่างๆของตัวอักษรได้เช่น
FontName,FontSize,FontBold,FontItalic,FontStrikeThru และ FontUnderline

ตัวอย่าง
เช่นเราจะเปลี่ยน Font เป็น Tahoma ก็จะใช้คำสั่งดังตัวอย่าง

Printer.FontName = "Tahoma"

สำหรับโค้ดต่อไปนี้ เป็นตัวอย่างเกี่ยวกับ การเปลี่ยนลักษณะของ Font ใน Printer

With Printer
Printer.Print "Normal Line"
.FontBold = True
Printer.Print "Bold Line"
.FontItalic = True
Printer.Print "Bold and Italic Line"
.FontBold = False
.FontItalic = False
Printer.Print "Second Normal Line"
.EndDoc
End With

สำหรับการตั้งขอบกระดาษก็ไม่ยากเลยเราจะมาลองตั้งเพื่อให้พิมพ์เลขหน้า อยู่ตรงกลางหน้ากระดาษ
ดังตัวอย่างต่อไปนี้

sMsg = "Page " & Printer.Page ' Print Page number
HWidth = Printer.TextWidth(sMsg) / 2 ' Get one-half width.
HHeight = Printer.TextHeight(sMsg) /2 ' Get one-half height.
Printer.CurrentX = Printer.ScaleWidth / 2 - HWidth
Printer.CurrentY = Printer.ScaleHeight / 2 - HHeight
Printer.Print sMsg

โค้ดนี้ใช้ CurrentX และ CurrentY ในการเปลี่ยน ตำแหน่งที่ตัวอักษรจะถูกพิมพ์ สำหรับการตั้งค่า
CurrentX,CurrentY เป็นการจัดรูปแบบการพิมพ์อย่างง่ายๆ ครับ

และก็ยังมีคำสั่งวาดเส้นอีก สำหรับรูปแบบคำสั่งก็จะเป็นดังนี้ครับ

Printer.Line [Step] (x1, y1) [Step] - (x2, y2), [color], [B][F]

วิธีเรียกใช้ก็เช่น

Printer.Line (1000, 2000)-(5000, 3000), vbRed

สำหรับคำสั่งวาดวงกลมหรือวงรีก็จะใช้คำสั่งดังนี้ครับ

Printer.Circle [Step] (x, y), radius, [color, start, end, aspect]

ตัวอย่างการใช้เช่น

Printer.Circle (3000, 3000), 1000, RBG(100, 30, 30)

ก็จะได้รูปวงกลม

Const pi = 3.141592654
Printer.Circle (3000, 3000), 1000, vbBlue, 0, pi

ก็จะได้รูปครึ่งวงกลม

Printer.Circle (3000, 3000), 1000, vbBlue, , , 0.5

ก็จะได้รูปวงรีครับ

และยังสามารถพิมพ์รูปภาพได้โดยการใช้คำสั่งรูปแบบนี้ครับ

Printer.PaintPicture picture, x1, y1, [width1], [height1], [x2], [y2], [width2], [height2], [opcode]

ตัวอย่างการใช้ก็เช่น

Printer.PaintPicture Picture1.Picture, 1000, 1000, 2000, 2000

นอกเหนือจากคำสั่งที่ได้กล่าวมาข้างต้นก็ยังมี Properties อีกหลายตัวครับ

FillColor
This specifies the fill color (in RGB) used, if any. Use the RGB function
or vb colour constants to generate values for this.

FillMode
This sets the pattern used to fill any shapes drawn by the Printer object.
To see its possible values, hit F2, and select FillStyleConstants from the
Classes list.

DrawMode
The mode of drawing. Unless you know what you are doing, leave this to
13 - Copy Pen. Many of these settings yield unpredictable

results
depending on the colours on the screen. See DrawModeConstants in the

object
browser for its values.

DrawWidth
The width of the drawing line

DrawStyle
The style of the line. (ie solid, dotted etc). See DrawStyleConstants
in the object browser for its values.

ScaleMode
This specifies the units in which coordinates is measured. The default
is twips (1,440 twips equal one inch)

และ

Copies
The number of copies of your output that will be printed

DeviceName
The name of the printer device.

DriverName
The name of the driver for the printer

Duplex
Whether the printer is in Duplex mode or not (printing on both sides).

hDC
A handle to the printers device context

Orientation
The orientation of the paper. This can be vbPRORLandscape or

vbPRORPortrait.

Page
Returns the current page number (read-only)

PaperBin
The paper bin to use when printing the document. The values for this

property
can be found by hitting F2 (Object Browser), selecting PrinterObjectConstants
from the Classes list, and taking a look at all the constants beginning
with vbPRBN

PaperSize
The size of the paper that is being printed on. See PrinterObjectConstants
in the Object Browser, beginning with vbPRPS

Port
The name of the printer port (read-only)

PrintQuality
The quality of printing (printer resolution). See PrinterObjectConstants,
beginning with vbPRPQ

TrackDefault
Whether to automatically move to the default printer if this is changed
in Control Panel

Zoom
The percentage with which the output is scaled up or down. (Note that
this only works on printers that support it!)

Post new comment

คำนวณผลบวกด้านบนแล้วกรอกผลลัพธ์ลงในช่อง เช่น 2 ลบ 1 ให้พิมพ์ 1