📄 form1.cs
字号:
using GrabbaDotNet;
using System;
using System.Drawing;
using System.Collections;
using System.Windows.Forms;
using System.Data;
using BachmannSoftware.ReportLayout_NET;
namespace MagStripe_Demo_CSharp
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button clearBtn;
private System.Windows.Forms.Button enableBtn;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.TextBox textBox2;
private System.Windows.Forms.TextBox textBox3;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label3;
private Button printBtn;
Grabba myGrabba;
public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
myGrabba = new Grabba();
if (myGrabba.Open() != myGrabba.errNone)
{
//problem opening grabba
}
Grabba.GrabbaModelType myGrabbaModel = new Grabba.GrabbaModelType();
myGrabba.GetModel(ref myGrabbaModel);
// check that the barcode device was detected as a T-x020
if (!(myGrabbaModel.magStripeDevice.Equals(true)))
{
// failure do something here since it wasnt detected properly
}
// add event handler
Grabba.GrabbaMagStripeEvent += new EventHandler(onMagStripeEvent);
Grabba.GrabbaDisconnectedEvent += new EventHandler(onGrabbaDisconnectedEvent);
Grabba.GrabbaReconnectedEvent += new EventHandler(onGrabbaReconnectedEvent);
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.clearBtn = new System.Windows.Forms.Button();
this.enableBtn = new System.Windows.Forms.Button();
this.textBox1 = new System.Windows.Forms.TextBox();
this.textBox2 = new System.Windows.Forms.TextBox();
this.textBox3 = new System.Windows.Forms.TextBox();
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.label3 = new System.Windows.Forms.Label();
this.printBtn = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// clearBtn
//
this.clearBtn.Location = new System.Drawing.Point(148, 8);
this.clearBtn.Name = "clearBtn";
this.clearBtn.Size = new System.Drawing.Size(60, 24);
this.clearBtn.TabIndex = 5;
this.clearBtn.Text = "Clear";
this.clearBtn.Click += new System.EventHandler(this.clearBtn_Click);
//
// enableBtn
//
this.enableBtn.Location = new System.Drawing.Point(16, 8);
this.enableBtn.Name = "enableBtn";
this.enableBtn.Size = new System.Drawing.Size(64, 24);
this.enableBtn.TabIndex = 6;
this.enableBtn.Text = "Enable";
this.enableBtn.Click += new System.EventHandler(this.enableBtn_Click);
//
// textBox1
//
this.textBox1.AcceptsReturn = true;
this.textBox1.Location = new System.Drawing.Point(16, 56);
this.textBox1.Multiline = true;
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(192, 52);
this.textBox1.TabIndex = 7;
//
// textBox2
//
this.textBox2.AcceptsReturn = true;
this.textBox2.Location = new System.Drawing.Point(16, 128);
this.textBox2.Multiline = true;
this.textBox2.Name = "textBox2";
this.textBox2.Size = new System.Drawing.Size(192, 52);
this.textBox2.TabIndex = 4;
//
// textBox3
//
this.textBox3.AcceptsReturn = true;
this.textBox3.Location = new System.Drawing.Point(16, 200);
this.textBox3.Multiline = true;
this.textBox3.Name = "textBox3";
this.textBox3.Size = new System.Drawing.Size(192, 52);
this.textBox3.TabIndex = 3;
//
// label1
//
this.label1.Location = new System.Drawing.Point(24, 40);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(96, 16);
this.label1.Text = "Track 1";
//
// label2
//
this.label2.Location = new System.Drawing.Point(24, 112);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(96, 16);
this.label2.Text = "Track 2";
//
// label3
//
this.label3.Location = new System.Drawing.Point(24, 184);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(96, 16);
this.label3.Text = "Track 3";
//
// printBtn
//
this.printBtn.Location = new System.Drawing.Point(86, 8);
this.printBtn.Name = "printBtn";
this.printBtn.Size = new System.Drawing.Size(56, 24);
this.printBtn.TabIndex = 8;
this.printBtn.Text = "Print";
this.printBtn.Click += new System.EventHandler(this.printBtn_Click);
//
// Form1
//
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Inherit;
this.ClientSize = new System.Drawing.Size(226, 272);
this.Controls.Add(this.printBtn);
this.Controls.Add(this.label3);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Controls.Add(this.textBox3);
this.Controls.Add(this.textBox2);
this.Controls.Add(this.clearBtn);
this.Controls.Add(this.enableBtn);
this.Controls.Add(this.textBox1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
static void Main()
{
Application.Run(new Form1());
}
private void clearBtn_Click(object sender, System.EventArgs e)
{
textBox1.Text = "";
textBox2.Text = "";
textBox3.Text = "";
}
private void onMagStripeEvent(object sender, System.EventArgs e)
{
Grabba.MagStripeRawDataType myMagStripeData = new Grabba.MagStripeRawDataType();
myGrabba.MagStripeGetData(ref myMagStripeData);
// since magstripe data is BYTE ARRAY not char array - convert it for this demo to display as text
// normally magstripe data should be dealt with as data not string, since there are non-ascii characters throughout
// the data.
char[] track1CharArray = new char[Grabba.LENGTH_MAGSTRIPE_TRACK];
char[] track2CharArray = new char[Grabba.LENGTH_MAGSTRIPE_TRACK];
char[] track3CharArray = new char[Grabba.LENGTH_MAGSTRIPE_TRACK];
convertByteArrayToCharArray(ref myMagStripeData.track1, ref track1CharArray, Grabba.LENGTH_MAGSTRIPE_TRACK);
convertByteArrayToCharArray(ref myMagStripeData.track2, ref track2CharArray, Grabba.LENGTH_MAGSTRIPE_TRACK);
convertByteArrayToCharArray(ref myMagStripeData.track3, ref track3CharArray, Grabba.LENGTH_MAGSTRIPE_TRACK);
string track1Str = new string(track1CharArray);
string track2Str = new string(track2CharArray);
string track3Str = new string(track3CharArray);
textBox1.Text = track1Str;
textBox2.Text = track2Str;
textBox3.Text = track3Str;
}
private void onGrabbaReconnectedEvent(object sender, System.EventArgs e)
{
textBox1.Text += "Grabba Reconnected!\r\n";
}
private void onGrabbaDisconnectedEvent(object sender, System.EventArgs e)
{
textBox1.Text += "Grabba Disconnected!\r\n";
}
private void enableBtn_Click(object sender, System.EventArgs e)
{
if (enableBtn.Text.Equals("Enable"))
{
myGrabba.MagStripeEnable();
enableBtn.Text = "Disable";
}
else
{
myGrabba.MagStripeDisable();
enableBtn.Text = "Enable";
}
}
private void convertByteArrayToCharArray(ref byte[] byteArray, ref char[] charArray, uint length)
{
for (uint i=0;i<length;i++)
{
charArray[i] = (char) byteArray[i];
}
}
private void printBtn_Click(object sender, EventArgs e)
{
ReportLayout rpt = myGrabba.PrintBoyReportLayoutCreate();
// Save the intermediate file
rpt.SetOption( 0, "CSSaveTestFile");
//Begin report
rpt.Begin("Various Tests");
// print text using raw command
rpt.AddRaw("Track 1: ");
rpt.AddRaw(textBox1.Text);
rpt.AddRaw("\r\n");
// print text using raw command
rpt.AddRaw("Track 2: ");
rpt.AddRaw(textBox2.Text);
rpt.AddRaw("\r\n");
// print text using raw command
rpt.AddRaw("Track 3: ");
rpt.AddRaw(textBox3.Text);
rpt.AddRaw("\r\n");
// end the report layout
rpt.End();
// now execute the report!
rpt.Execute();
// clean up
rpt.Destroy();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -