|
|
|
C# winApp printDocument PrintPage สั่งปริ้นแล้วมีข้อความที่ทับกันอยู่ ครับ |
|
|
|
|
|
|
|
โค้ด คลาสแม่
Code (C#)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Printing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace KidsLearning.Print
{
public partial class prnControl : UserControl
{
public prnControl()
{
InitializeComponent();
}
#region Variables
protected string ReportHeader;
protected string ReportToppic;
protected Font fontHeader = new Font("CordiaUPC", 18, FontStyle.Bold);
protected Font fontDetail = new Font("CordiaUPC", 18);
protected Font fontExpression = new Font("Microsoft Sans Serif", 18);
public string filePrintPre = "";
protected System.Drawing.StringFormat strFormat = new StringFormat()
{
Alignment = StringAlignment.Near,
LineAlignment = StringAlignment.Center,
Trimming = StringTrimming.EllipsisCharacter
}; //Used to format the grid rows.
protected int iPage = 1;
protected int iPageAll = 10;
protected bool bFirstPage = false; //Used to check whether we are printing first page
protected bool bNewPage = false;// Used to check whether we are printing a new page
//Whether more pages have to print or not
protected bool bMorePagesToPrint = false;
#endregion
private void printDocument1_BeginPrint(object sender, System.Drawing.Printing.PrintEventArgs e)
{
Cursor.Current = Cursors.WaitCursor;
bFirstPage = true;
bNewPage = true;
}
private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
}
internal void printDocumentNewPage(object sender, PrintPageEventArgs e)
{
// System.Windows.Forms.MessageBox.Show(_ReportHeader);
//Draw Header
// string strToppic = "แปลงหน่วยนับ ต่อไปนี้ให้ถูกต้อง";
e.Graphics.DrawString(ReportHeader, fontHeader, Brushes.Black, 300, e.MarginBounds.Top - e.Graphics.MeasureString(ReportHeader, fontHeader, e.MarginBounds.Width).Height - 13);
e.Graphics.DrawString(ReportToppic, fontHeader, Brushes.Black, 100, 90);
//Draw Footer
e.Graphics.DrawString($"test date ...../...../....... by....................... Print date {DateTime.Now.ToString("yyyyMMdd hhmmss")} Page{iPage}/{iPageAll}", fontDetail, Brushes.Black, e.MarginBounds.Left -40, e.MarginBounds.Bottom + 40);
}
private void prnControl_Load(object sender, EventArgs e)
{
this.Dock = DockStyle.Fill;
this.printDocument1.DefaultPageSettings.Landscape = false;
// groupBox1.Width = 250;
}
private void bntPrint_Click(object sender, EventArgs e)
{
if (System.Windows.Forms.DialogResult.OK == this.printDialog1.ShowDialog())
{
string filePre = Application.StartupPath + filePrintPre;
/* if ( File.Exists(filePre))
{
if (MessageBox.Show("คุณต้องการสั่งปริ้น เนื้อหา/บทเรียน ด้วยหรือไม่", "แจ้งเตือน", MessageBoxButtons.YesNo) == DialogResult.Yes)
{
TORServices.Drawings.PrintFile.Print(filePre);
}
}*/
bNewPage = true;
bMorePagesToPrint = true;
iPageAll = int.Parse(this.txtPageCount.Text);
iPage = 1;
this.printDocument1.PrinterSettings.PrinterName = printDialog1.PrinterSettings.PrinterName;
this.printDocument1.DocumentName = ReportHeader + string.Format("{0:yyyyMMdd hhmmss}", DateTime.Now);
this.printDocument1.Print();
}
}
private void printDocument1_EndPrint(object sender, PrintEventArgs e)
{
Cursor.Current = Cursors.Default;
}
}
}
คลาสลูก
Code (C#)
private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
//Loop till all the grid rows not get printed
if (bFirstPage) printDocumentNewPage(sender, e);
#region _Draw Detail
int yC = 120, xC = 100;
int a = 0, b = 0, _a = 0, _b = 0,
den_1 = 1, den_2 = 1, _den_1 = 1, _den_2 = 1,
ext_1 = 0, ext_2 = 0, _ext_1 = 0, _ext_2 = 0;
string _sop = Sop;
string _Den;
for (int i = 0; i < 6; i++)
{
_den_1 = 1;
_den_2 = 1;
_ext_1 = 0;
_ext_2 = 0;
if (Den == "E/NE")
{
switch (RandomNumber.Randomnumber(1, 4000))
{
case int n when n > 3000:
_Den = "E"; break;
case int n when n <= 3000 && n > 2000:
_Den = "NE"; break;
case int n when n <= 2000 && n > 1000:
_Den = "E_Int"; break;
case int n when n <= 1000:
_Den = "E_Flc"; break;
default:
_Den = "E"; break;
}
}
else
{
_Den = Den;
}
if (_Den == "E")//ส่วนเหมือนกัน
{
_den_1 = RandomNumber.Randomnumber(3, 10);
_den_2 = _den_1;
}
else if (_Den == "NE")//ส่วนต่างกัน
{
_den_1 = RandomNumber.Randomnumber(4, 10);
do
{
_den_2 = RandomNumber.Randomnumber(2, 10);
} while (_den_1 == _den_2);
}
else if (_Den == "E_Int")//แบบมีจำนวนเต็ม
{
int aa = RandomNumber.Randomnumber(1, 3000);
if (aa > 1500)
{
_den_2 = 1;
_den_1 = RandomNumber.Randomnumber(3, 10);
}
else
{
_den_1 = 1;
_den_2 = RandomNumber.Randomnumber(3, 10);
}
// MessageBox.Show(_den_1 + " " + _den_2);
}
else if (_Den == "E_Flc")//แบบมีจำนวนคละ
{
_den_1 = RandomNumber.Randomnumber(5, 10);
_den_2 = RandomNumber.Randomnumber(5, 10);
_ext_1 = RandomNumber.Randomnumber(0, 10);
_ext_2 = RandomNumber.Randomnumber(0, 10);
/*int aa = RandomNumber.Randomnumber(1, 3000) ;
if (aa > 2000)
{
_ext_1 = RandomNumber.Randomnumber(1, 10);
}
else if (aa < 1000)
{
_ext_2 = RandomNumber.Randomnumber(1, 10);
}
else
{
_ext_1 = RandomNumber.Randomnumber(1, 10);
_ext_2 = RandomNumber.Randomnumber(1, 10);
}*/
}
// MessageBox.Show(_sop + " " + Den);
//ถ้าเป็นจำนวนเต็ม _den จะ เท่ากับ 1
_a = (_den_1 == 1) ? RandomNumber.Randomnumber(1, 10) :
((_den_1 == 2) ? 1 : RandomNumber.Randomnumber(1, _den_1));
_b = (_den_2 == 1) ? RandomNumber.Randomnumber(1, 10) :
((_den_2 == 2) ? 1 : RandomNumber.Randomnumber(1, _den_2));
a = _a;
b = _b;
den_1 = _den_1;
den_2 = _den_2;
ext_1 = _ext_1;
ext_2 = _ext_2;
if (Sop == "+-x÷")
{
int aa = RandomNumber.Randomnumber(1, 4000);
switch (aa)
{
case int n when n > 3000:
_sop = "+";
break;
case int n when n <= 3000 && n > 2000:
_sop = "-";
break;
case int n when n <= 2000 && n > 1000:
_sop = "x";
break;
case int n when n <= 1000:
_sop = "÷";
break;
default:
_sop = "+";
break;
}
}
if (_sop == "-")
{
double v_a = (Convert.ToDouble(((_ext_1 == 0) ? 1d : _ext_1)) * Convert.ToDouble(_den_1) + Convert.ToDouble(_a)) / Convert.ToDouble(_den_1);
double v_b = (Convert.ToDouble(((_ext_2 == 0) ? 1d : _ext_2)) * Convert.ToDouble(_den_2) + Convert.ToDouble(_b)) / Convert.ToDouble(_den_2);
// MessageBox.Show($"{_a} + {_den_1}*{_ext_1} = {v_a}" + "\n" + $"{_b} + {_den_2}*{_ext_2} = {v_b}" );
if (v_a > v_b)
{
a = _b;
b = _a;
den_1 = _den_2;
den_2 = _den_1;
ext_1 = _ext_2;
ext_2 = _ext_1;
}
else if (v_a == v_b)
{
b += (b == 1) ? 1 : RandomNumber.Randomnumber(1, b);
}
}
if (den_2 > 1)
{
e.Graphics.DrawFraction(ext_2, b, den_2, xC + 10, yC);
}
else
{
e.Graphics.DrawString(b.ToString(), new Font("Angsana New", 22), new SolidBrush(Color.Black), xC + 60, yC + 15);
}
e.Graphics.DrawString(_sop, new Font("Angsana New", 22), new SolidBrush(Color.Black), xC + 80, yC + 15);
if (den_1 > 1)
{
e.Graphics.DrawFraction(ext_1, a, den_1, xC + 100, yC);
}
else
{
e.Graphics.DrawString(b.ToString(), new Font("Angsana New", 22), new SolidBrush(Color.Black), xC + 110, yC + 15);
}
e.Graphics.DrawString(" วิธีทำ# ", new Font("Angsana New", 20), new SolidBrush(Color.Black), xC + 200, yC + 5);
yC += 150;
}
#endregion
if (iPage > iPageAll - 1)
{
bNewPage = false;
bMorePagesToPrint = false;
}
if (bNewPage)
{
printDocumentNewPage(sender, e);
}
iPage++;
//If more lines exist, print another page.
e.HasMorePages = (bMorePagesToPrint) ? true : false;
}
จาก
Code (C#)
e.Graphics.DrawString($"test date ...../...../....... by....................... Print date {DateTime.Now.ToString("yyyyMMdd hhmmss")} Page{iPage}/{iPageAll}", fontDetail, Brushes.Black, e.MarginBounds.Left -40, e.MarginBounds.Bottom + 40);
พบว่าตรงเวลา มันแปลกๆ ครับ ออกมาไม่เต็ม
แบบนี้ต้องแก้ยังไง ครับ
Tag : .NET, Win (Windows App), C#
|
ประวัติการแก้ไข 2023-10-11 09:40:56
|
|
|
|
|
Date :
2023-10-11 09:39:50 |
By :
lamaka.tor |
View :
325 |
Reply :
1 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ความกว้างไม่สมดุล กับ font อักษร ปรับขนาด font ให้เล็กลง หรือขึ้นบันทัดใหม่
|
|
|
|
|
Date :
2023-10-12 09:56:51 |
By :
Chaidhanan |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 05
|