public partial class Form1 : Form
{
string filename;
int pIndex, Index;
System.Collections.ArrayList al;
QRCodeEncoder qrCodeEncoder;
string x12;
public Form1()
{
InitializeComponent();
}
bool printState;
private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
if (al.Count == 0 || pIndex >= al.Count) return;
qrCodeEncoder = new QRCodeEncoder();
qrCodeEncoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.BYTE;
qrCodeEncoder.QRCodeVersion = 4;
qrCodeEncoder.QRCodeScale = 2;
qrCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.L;
Image image;
string url = tb网址.Text != string.Empty ? tb网址.Text : string.Empty;
String data;
SizeF sf,gsf;
float w, h, x, y;
//w = e.MarginBounds.Width / 4 ;
//h = e.MarginBounds.Height - 7;
int temp;
int.TryParse(tb字号.Text, out temp);
Font f = new System.Drawing.Font(System.Drawing.FontFamily.GenericSerif, temp == 0 ? 3 : temp);
StringFormat format1 = new StringFormat(StringFormatFlags.DirectionVertical);
sf = e.Graphics.MeasureString((url == string.Empty ? "#" : "") + al[pIndex].ToString(),
f,0,format1); sf.Width += 1;
string gurl = this.Text = new System.Uri(tb网址.Text).GetComponents(UriComponents.SchemeAndServer, UriFormat.SafeUnescaped);
gsf = e.Graphics.MeasureString(gurl,f, 0, format1);
data = url + al[pIndex].ToString();
image = qrCodeEncoder.Encode(data);
w = (int)(image.Width );
h = (int)(image.Height );
int offset_x = (int)nudX.Value;
int offset_y = (int)nudY.Value;
x = e.PageBounds.Width / 4 - (w+(int)sf.Width+gsf.Width) / 2 + offset_x;//偏移量;
y = e.PageBounds.Height / 2 - h / 2 + offset_y; //h / 2 + e.MarginBounds.Y;
//e.Graphics.DrawImage(image, new Rectangle(x, y, w, h-(int)sf.Height));
e.Graphics.DrawString(gurl, f, new SolidBrush(System.Drawing.Color.Black), new RectangleF(
new PointF(x, y + (h - gsf.Height) / 2), gsf
),format1);
e.Graphics.DrawImage(image, new RectangleF(x+gsf.Width, y, w, h ));//- (int)sf.Height)
e.Graphics.DrawString((url == string.Empty ? "#" : "") + al[pIndex].ToString(),
f, new SolidBrush(System.Drawing.Color.Black), new RectangleF
(
new PointF(x + w +gsf.Width
, y + (h - sf.Height)/2),//-(int)sf.Height
sf
),format1);
if (pIndex + 1 < al.Count)
{
//(url == string.Empty ? "*" : "")+;
data = url + al[1 + pIndex].ToString();
image = qrCodeEncoder.Encode(data);
e.Graphics.DrawString(gurl, f, new SolidBrush(System.Drawing.Color.Black), new RectangleF(
new PointF(x + e.PageBounds.Width / 2, y + (h - gsf.Height) / 2), gsf
), format1);
e.Graphics.DrawImage(image, new RectangleF(x + e.PageBounds.Width / 2 + gsf.Width,y , w, h));
e.Graphics.DrawString((url == string.Empty ? "#" : "") + al[pIndex + 1].ToString(), f, new SolidBrush(System.Drawing.Color.Black), new RectangleF
(
/*
new PointF((sf.Width > w ? x - (sf.Width - w) / 2 : x + (w - sf.Width) / 2)
+ e.MarginBounds.Width / 2, y + (h)),
*/
new PointF(x + e.PageBounds.Width / 2 + w + gsf.Width,
y + (h - sf.Height) / 2
),
sf
),format1);
}
if (pIndex + 2 < al.Count)
{
pIndex = pIndex + 2; e.HasMorePages = true;
}
else
{
e.HasMorePages = false;
}
printState = true;
}
void PrintInit()
{
pIndex = 0;
al = new System.Collections.ArrayList();
x12 = string.Empty;
int temp, start, end;
//x12 = tb网址.Text != string.Empty ? tb网址.Text : string.Empty;
int.TryParse(tb组织编号.Text, out temp);
x12 += temp.ToString().PadLeft(3, '0');
int.TryParse(tb年度.Text, out temp);
x12 += temp.ToString().PadLeft(2, '0') ;
int.TryParse(tb批次.Text, out temp);
x12 += temp.ToString().PadLeft(4, '0') ;
if (tb瓶号2.Text != string.Empty)
{
int.TryParse(tb瓶号1.Text, out temp);
start = temp;
int.TryParse(tb瓶号2.Text, out temp);
end = temp;
for (; start <= end; start++)
{
string str = x12 + start.ToString().PadLeft(3, '0');
al.Add(str);
}
}
else
{
string[] theStr = tb瓶号1.Text.Split(new char[]{','},StringSplitOptions.RemoveEmptyEntries);
foreach (string s in theStr)
{
string str = x12 + s.PadLeft(3, '0');
al.Add(str);
}
}
pageSetupDialog1.PageSettings.Margins.Top = 0;
pageSetupDialog1.PageSettings.Margins.Bottom = 0;
pageSetupDialog1.PageSettings.Margins.Left = 0;
pageSetupDialog1.PageSettings.Margins.Right = 0;
}
private void button1_Click(object sender, EventArgs e)
{
PrintInit();
if ( pageSetupDialog1.ShowDialog()== DialogResult.OK)
if (printDialog1.ShowDialog() == DialogResult.OK)
{
printDocument1.Print();
if (printState)
{
filename = Application.StartupPath + @"\记录\" + DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString().PadLeft(2, '0') + ".TXT";
if (!System.IO.Directory.Exists(filename)) System.IO.Directory.CreateDirectory(System.IO.Path.GetDirectoryName(filename));
System.IO.File.AppendAllText(filename, DateTime.Now.ToString("yyyy-MM-dd&HH:mm:ss.fff") +
"\r\n");
foreach (object obj in al)
System.IO.File.AppendAllText(filename, obj.ToString() + "\r\n");
}
}
printState = false;
}
private void Form1_Load(object sender, EventArgs e)
{
toolStripStatusLabel1.Text ="Ver: "+ Application.ProductVersion;
filename = Application.StartupPath + @"\记录\" + DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString().PadLeft(2, '0') + ".TXT";
this.Text = DateTime.Now.ToString("yyyy-MM-dd&HH:mm:ss.fff");
tb字号.Text = "7";
tb组织编号.Text = "1**";
tb年度.Text = DateTime.Now.Year.ToString().Substring(2, 2);
tb批次.Text = "0000";
tb瓶号1.Text = "601";
tb瓶号2.Text = "602";
tb网址.Text = "http://*****.cn/publicQuality/pcQuality?code=";
if (!System.IO.File.Exists("offset") )return;
string[] ss = System.IO.File.ReadAllLines("offset");
nudX
.Value = int.Parse(ss[0]);
nudY
.Value = int.Parse(ss[1]);
}
private void button1_Click_1(object sender, EventArgs e)
{
}
private void button2_Click(object sender, EventArgs e)
{
PrintInit();
if (pageSetupDialog1.ShowDialog() == DialogResult.OK)
{
if (printPreviewDialog1.IsDisposed)
{
printPreviewDialog1 = new PrintPreviewDialog();
printPreviewDialog1.Document = printDocument1;
}
printPreviewDialog1.Show();
printState = false;
}
}
private void bt记录_Click(object sender, EventArgs e)
{
string path = System.IO.Path.GetDirectoryName(filename);
if (System.IO.Directory.Exists(path)){
System.Diagnostics.Process.Start(path);
}
}
private void bt偏移_Click(object sender, EventArgs e)
{
System.IO.File.WriteAllLines("offset", new string[] { nudX.Value.ToString(), nudY.Value.ToString() });
}
}