 |
|
ต้องการพิมพ์ใบสลิปโดยใช้เครื่องพิมพ์ใบเสร็จอย่างย่อ โดยใช้ C# |
|
 |
|
|
 |
 |
|
แนะนำว่า......ทำมาก่อนครับ ติดตรงไหนค่อยเข้ามาถามครับ
|
 |
 |
 |
 |
Date :
2011-11-26 16:23:32 |
By :
Dragons_first |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
เท่าที่เคยทำคือมันจะต้องมีรุ่นของ Printer ก่อนครับ จากนั้นก็ออกแบบตาม Template ที่ Driver รุ่น นั้น ๆ มีมาให้ครับ 
|
 |
 |
 |
 |
Date :
2011-11-27 08:04:34 |
By :
webmaster |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ท่าน mr.win ช่วยแนะนำ code คร่าวๆ ให้หน่อยครับ
|
 |
 |
 |
 |
Date :
2011-12-04 07:20:44 |
By :
sseiyatep |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
หวัดดีครับส่วนตัวผมไม่เคยเขียนติดต่อกับเครื่องพิมพ์แบบใบเสร็จนะครับแต่คิดว่าน่าจะเหมือนกันเครื่องพิมพ์ทั่วๆไป ผมลองหาดู .Net support เรื่องพวกนี้อยู่ครับตามลิงค์ด้านล่างเลยครับ
http://msdn.microsoft.com/en-us/library/system.drawing.printing.printdocument.aspx
http://support.microsoft.com/kb/322091
ลิงค์อันสองจะยากหน่อยผมคิดว่ามันเป็นการส่ง Raw data ไปที่เครื่อง printer เลย ให้ลองทำอันลิงค์แรกดีกว่าง่ายกว่าครับ
ก่อนอื่นเลยต้อง Set printer และ call back function ก่อนครับ
Code (C#)
printFont = new Font("Arial", 10);
PrintDocument pd = new PrintDocument();
pd.PrintPage += new PrintPageEventHandler
(this.pd_PrintPage);
pd.Print();
pd_PrintPage จะถูกเรียกเมื่อแต่ละ page จะถูก print อ่าครับเพื่อที่จะเอาไว้ Set พวกขอบบนขอบลาง และเราต้องการ print ข้อมูลอะไรออกที่เครื่องพิมพ์ อะไรประมาณนี้อ่าครับ
Code (C#)
// The PrintPage event is raised for each page to be printed.
private void pd_PrintPage(object sender, PrintPageEventArgs ev)
{
float linesPerPage = 0;
float yPos = 0;
int count = 0;
float leftMargin = ev.MarginBounds.Left;
float topMargin = ev.MarginBounds.Top;
string line = null;
// Calculate the number of lines per page.
linesPerPage = ev.MarginBounds.Height /
printFont.GetHeight(ev.Graphics);
// Print each line of the file.
while (count < linesPerPage &&
((line = streamToPrint.ReadLine()) != null))
{
yPos = topMargin + (count *
printFont.GetHeight(ev.Graphics));
ev.Graphics.DrawString(line, printFont, Brushes.Black,
leftMargin, yPos, new StringFormat());
count++;
}
// If more lines exist, print another page.
if (line != null)
ev.HasMorePages = true;
else
ev.HasMorePages = false;
}
เมื่อ call back function ถูกเรียกเราก็แค่บอกว่าต้องการ print อะไร เช่นในกรณีนี้เราต้องการจะ print ข้อมูลที่อยู่ใน Text file "C:\\My Documents\\MyFile.txt" ที่เก็บอยู่ใน streamToPrint
Code (C#)
// Print each line of the file.
while (count < linesPerPage &&
((line = streamToPrint.ReadLine()) != null))
{
yPos = topMargin + (count *
printFont.GetHeight(ev.Graphics));
ev.Graphics.DrawString(line, printFont, Brushes.Black,
leftMargin, yPos, new StringFormat());
count++;
}
ตัวอย่าง Code ทั้งหมด
Code (C#)
using System;
using System.IO;
using System.Drawing;
using System.Drawing.Printing;
using System.Windows.Forms;
public partial class Form1 : System.Windows.Forms.Form
{
private System.ComponentModel.Container components;
private System.Windows.Forms.Button printButton;
private Font printFont;
private StreamReader streamToPrint;
public Form1()
{
// The Windows Forms Designer requires the following call.
InitializeComponent();
}
// The Click event is raised when the user clicks the Print button.
private void printButton_Click(object sender, EventArgs e)
{
try
{
streamToPrint = new StreamReader
("C:\\My Documents\\MyFile.txt");
try
{
printFont = new Font("Arial", 10);
PrintDocument pd = new PrintDocument();
pd.PrintPage += new PrintPageEventHandler
(this.pd_PrintPage);
pd.Print();
}
finally
{
streamToPrint.Close();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
// The PrintPage event is raised for each page to be printed.
private void pd_PrintPage(object sender, PrintPageEventArgs ev)
{
float linesPerPage = 0;
float yPos = 0;
int count = 0;
float leftMargin = ev.MarginBounds.Left;
float topMargin = ev.MarginBounds.Top;
string line = null;
// Calculate the number of lines per page.
linesPerPage = ev.MarginBounds.Height /
printFont.GetHeight(ev.Graphics);
// Print each line of the file.
while (count < linesPerPage &&
((line = streamToPrint.ReadLine()) != null))
{
yPos = topMargin + (count *
printFont.GetHeight(ev.Graphics));
ev.Graphics.DrawString(line, printFont, Brushes.Black,
leftMargin, yPos, new StringFormat());
count++;
}
// If more lines exist, print another page.
if (line != null)
ev.HasMorePages = true;
else
ev.HasMorePages = false;
}
// The Windows Forms Designer requires the following procedure.
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.printButton = new System.Windows.Forms.Button();
this.ClientSize = new System.Drawing.Size(504, 381);
this.Text = "Print Example";
printButton.ImageAlign =
System.Drawing.ContentAlignment.MiddleLeft;
printButton.Location = new System.Drawing.Point(32, 110);
printButton.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
printButton.TabIndex = 0;
printButton.Text = "Print the file.";
printButton.Size = new System.Drawing.Size(136, 40);
printButton.Click += new System.EventHandler(printButton_Click);
this.Controls.Add(printButton);
}
}
ใน .Net มี class ให้ใช้มากครับลองศึกษาดูนะครับ
ฝาก Like ด้วยนะครับ
http://www.facebook.com/pages/PStudio-Development-Store/284410714927365?sk=wall
|
ประวัติการแก้ไข 2011-12-05 15:35:32 2011-12-05 15:36:33
 |
 |
 |
 |
Date :
2011-12-05 15:33:08 |
By :
pStudio |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ถ้ามีปัญหาเรื่องเครื่องพิมพ์ใบเสร็จอย่างย่อย สามารถโทรมาปรึกษาได้ที่ 081-6103941 ทางเรามีเจ้าหน้าที่ที่มีความชำนาญเฉพาะทางแนะนำได้ครับ
|
 |
 |
 |
 |
Date :
2015-02-11 11:40:36 |
By :
ทรัตน์ |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|