This is a trial to print a Prescription using three controls: PrintPreviewDialog control, PrintDocument control and PrintDialog control.

Please read the code of Form1 then run the program to see the result.
If you have any idea or if you find any problems then please tell me.

My
Project has one form (Form1) and two classes (clsReport and Globals).
Form1: has three text boxes for entering the data of the patient, the patient number and visit date.
Another three text boxes for entering the medication.
Three controls for printing: PrintPreviewDialog, PrintDocument and PrintDialog.
clsReport: has procedures for printing.
Globals: for some variables.
clsReport has the following three procedures:
Form1: has three text boxes for entering the data of the patient, the patient number and visit date.
Another three text boxes for entering the medication.
Three controls for printing: PrintPreviewDialog, PrintDocument and PrintDialog.
clsReport: has procedures for printing.
Globals: for some variables.
clsReport has the following three procedures:
----------------------------------------------------------------------------------------------------
public void ReadReport()
{
try
{
// read data from Form1
// you can get the data from any data file
RepVisitDate = "Date" + ": " + Globals.VisitDate;
RepSickName = "Name" + ": " + Globals.illName;
RepSickNum = "File number" + ": " + Globals.illNum;
Medicine1 = Globals.FirstMidc;
Medicine2 = Globals.SecondMidc;
Medicine3 = Globals.ThirdMidc;
}
catch
{
MessageBox.Show("Reading Error.");
}
private void ComputeLeftAll(Graphics g)
{
//compute the left location of all lines for,
//Recipe head, Sick data and Medicine.
Marginleft = Globals.leftMargin;
Marginright = Globals.rightMargin;
RepWid = Globals.ReportWid;
int lenWork = (int)g.MeasureString(dcWork, ArialB14).Width;
int lenDocHead1 = (int)g.MeasureString(DocHead1, ArialB18).Width;
int lenDocHead2 = (int)g.MeasureString(DocHead2, ArialB16).Width;
int lenSickVisit = (int)g.MeasureString(RepVisitDate, ArialB14).Width;
int lenSickName = (int)g.MeasureString(RepSickName, ArialB14).Width;
int lenSickNum = (int)g.MeasureString(RepSickNum, ArialB14).Width;
int lenRepTail1 = (int)g.MeasureString(RepTail1, ArialB14).Width;
xWork = Marginleft + (RepWid - lenWork) / 2;
xDocHead1 = Marginleft + (RepWid - lenDocHead1) / 2;
xDocHead2 = Marginleft + (RepWid - lenDocHead2) / 2;
xSickName = Marginleft;
xSickNum = Marginleft + (Marginright - Marginleft - lenSickNum) / 2;
xSickVisit = Marginright - lenSickName;
xRepTail1 = Marginleft + (Marginright - Marginleft - lenRepTail1) / 2;
}
public void DrawReport(Graphics g)
{
int CurrentY = Globals.topMargin;
int CurrentX = Globals.leftMargin;
try
{
// for Recipe
ArialR14 = new Font("Arial", 14, FontStyle.Regular);
int ArialRH14 = (int)(ArialR14.GetHeight(g));
// for EyeHorus
Font CorsivaFont = new Font("Monotype Corsiva", 18, FontStyle.Italic);
int CorsivaHeight = (int)(CorsivaFont.GetHeight(g));
// for Name
ArialB14 = new Font("Arial", 14, FontStyle.Bold);
int ArialBH14 = (int)(ArialB14.GetHeight(g));
ArialB16 = new Font("Arial", 16, FontStyle.Bold);
int ArialBH16 = (int)(ArialB16.GetHeight(g));
ArialB18 = new Font("Arial", 18, FontStyle.Bold);
int ArialBH18 = (int)(ArialB18.GetHeight(g));
ArialB20 = new Font("Arial", 20, FontStyle.Bold);
int ArialBH20 = (int)(ArialB20.GetHeight(g));
ComputeLeftAll(g);
SolidBrush myBrush = new SolidBrush(Color.Black);
CurrentY = CurrentY + ArialRH14;
//Draw Report Head:
g.DrawString(dcWork, ArialB14, myBrush, xWork, CurrentY); //dcWorkAR
CurrentY = CurrentY + ArialBH14;
g.DrawString(DocHead1, ArialB18, myBrush, xDocHead1, CurrentY); //DocHeadAR1
CurrentY = CurrentY + ArialBH20;
g.DrawString(DocHead2, ArialB16, myBrush, xDocHead2, CurrentY); //DocHeadAR2
CurrentY = CurrentY + 2 * ArialBH14;
// Draw line
g.DrawLine(new Pen(Brushes.Black), Marginleft, CurrentY, Marginright, CurrentY);
CurrentY = CurrentY + ArialRH14;
// Draw Personal Data:
g.DrawString(RepSickName, ArialB14, myBrush, xSickName, CurrentY);
g.DrawString(RepSickNum, ArialB14, myBrush, xSickNum, CurrentY);
g.DrawString(RepVisitDate, ArialB14, myBrush, xSickVisit, CurrentY);
CurrentY = CurrentY + 2 * ArialBH14;
// Draw EyeHorus:
g.DrawString(EyeHorus, CorsivaFont, myBrush, CurrentX, CurrentY);
CurrentY = CurrentY + CorsivaHeight;
// Draw Recipe:
CurrentY = CurrentY + ArialRH14;
g.DrawString(Medicine1, ArialR14, myBrush, CurrentX, CurrentY);
CurrentY = CurrentY + ArialRH14;
g.DrawString(Medicine2, ArialR14, myBrush, CurrentX, CurrentY);
CurrentY = CurrentY + ArialRH14;
g.DrawString(Medicine3, ArialR14, myBrush, CurrentX, CurrentY);
// Go to end of page:
CurrentY = Globals.bottomMargin - 4 * ArialRH14;
// Draw line
g.DrawLine(new Pen(Brushes.Black), Marginleft, CurrentY, Marginright, CurrentY);
// Draw Report Tail:
CurrentY = CurrentY + ArialRH14;
g.DrawString(RepTail1, ArialB14, myBrush, xRepTail1, CurrentY); //RepTail1
ArialB14.Dispose();
ArialB16.Dispose();
ArialB18.Dispose();
ArialB20.Dispose();
ArialR14.Dispose();
CorsivaFont.Dispose();
g.Dispose();
}
catch
{
string Msg = "Can't print, no Font!";
MessageBox.Show(Msg, "Printing.", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
}
If you have any idea or if you find any problems then please tell me.
No comments:
Post a Comment