首先把VB动态库引用到BIN里,然后写一个类,领进动态库方法,接着在程序里调用,如下所示:
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
namespace BusinessEntity.GuestOrderInfo
{
public class RefVBDLL
{
//连接机器
int getOldSn = 0;
[DllImport("CT_ReWrite.dll")]
public static extern int ReWriteStartUp();
[DllImport("CT_ReWrite.dll")]
public static extern int ReWriteConnect(string str);
[DllImport("CT_ReWrite.dll")]
public static extern int ReWriteDisConnect(int dhl);
[DllImport("CT_ReWrite.dll")]
public static extern long ReWriteWaitForCard(int dhl);
[DllImport("CT_ReWrite.dll")]
public static extern long ReWriteCardCancel(int dhl);
[DllImport("CT_ReWrite.dll")]
public static extern long ReWriteCardOut(int dhl);
[DllImport("CT_ReWrite.dll")]
public static extern long ReWriteEraseCard(int dhl);
[DllImport("CT_ReWrite.dll")]
public static extern long ReWriteClearBuffer(int dhl);
[DllImport("CT_ReWrite.dll")]
public static extern long ReWriteTransferPrintDataEnglish(int dhl, int potx, int poty, int stye, string str);
[DllImport("CT_ReWrite.dll")]
public static extern long ReWritePrint(int dhl);
[DllImport("CT_ReWrite.dll")]
public static extern int ReWritePrintDotImage(int dhl, int x, int y, string str);
[DllImport("CT_ReWrite.dll")]
public static extern long ReWriteWriteMagnetic(int dhl, int tract, int count, ref byte data);
[DllImport("CT_ReWrite.dll")]
public static extern long ReWriteReadMagnetic(int dhl, ref int tract, ref int count, ref byte data);
[DllImport("CT_ReWrite.dll")]
public static extern long P_ReWriteTextOut(int PosX, int PosY, string StringName, int FontSize, int FontStyle, int FontAngle, string Data);
[DllImport("CT_ReWrite.dll")]
public static extern long P_ReWriteImageOut(int PosX, int PosY, string FilePath);
[DllImport("CT_ReWrite.dll")]
public static extern long P_ReWritePrint(int dhl);
[DllImport("CT_ReWrite.dll")]
public static extern long P_ReWriteClear();
}
}
接着在程序里的按钮事件里调用动态方法
/// <summary>
/// 刷卡信息allyn2008-5-12
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnFlushCard_Click(object sender, EventArgs e)
{
try
{
int totalNo = 0;
int getOldSn = 0;
// int writerFlag = 0;
string index = dgvShowMemeber[1, dgvShowMemeber.CurrentRow.Index].Value.ToString();
btnFlushCard.Enabled = false;
//依据选定的项得到那一条会员的信息allyn2008-5-16
ArrayList alst = md.getArrayListForMember(index);
string lblMemberNo = alst[0].ToString();
string lblName = alst[1].ToString();
string lblSex = alst[2].ToString();
string lblBalance = alst[3].ToString();
string lblTotal = alst[4].ToString();
string lblCardType = alst[5].ToString();
//看看本条是否有记录
string hascard = dgvShowMemeber["hasCard", dgvShowMemeber.CurrentRow.Index].Value.ToString();
if (Convert.ToBoolean(hascard) == true)
{
MessageBox.Show("对不起,你已经发卡!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
btnFlushCard.Enabled = true;
}
else
{
//连接
string machinaSn = "16A00032";
getOldSn = RefVBDLL.ReWriteConnect(machinaSn);
if (getOldSn > 0)
{
RefVBDLL.ReWriteStartUp();
RefVBDLL.P_ReWriteClear();
}
//插卡
RefVBDLL.ReWriteWaitForCard(getOldSn);
//打印第一个会员号allyn20086-12
long resNo;
int PosxNo;
int PosyNo;
PosxNo = 130;
PosyNo = 35;
resNo = RefVBDLL.P_ReWriteTextOut(PosxNo, PosyNo, "Aril", 20, 1, 1, lblMemberNo);
if (resNo == 0)
MessageBox.Show("会员号Error", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
else
{
totalNo = totalNo + 1;
MessageBox.Show("会员号OK", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
long resNob;
resNob = RefVBDLL.P_ReWritePrint(getOldSn);
//打印第二个名字allyn2008-6-12
long resName;
int PosxName;
int PosyName;
PosxName = 130;
PosyName = 90;
resName = RefVBDLL.P_ReWriteTextOut(PosxName, PosyName, "Aril", 20, 1, 1, lblName);
if (resName == 0)
MessageBox.Show("名字Error", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
else
{
totalNo = totalNo + 1;
MessageBox.Show("名字OK", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
long resNameB;
resNameB = RefVBDLL.P_ReWritePrint(getOldSn);
//打印性别
long resSex;
int PosxSex;
int PosySex;
PosxSex = 130;
PosySex = 150;
resSex = RefVBDLL.P_ReWriteTextOut(PosxSex, PosySex, "Aril", 20, 1, 1, lblSex);
if (resSex == 0)
MessageBox.Show("性别Error", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
else
{
totalNo = totalNo + 1;
MessageBox.Show("性别OK", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
long resSexB;
resSexB = RefVBDLL.P_ReWritePrint(getOldSn);
//打印余额
long resBancle;
int PosxBancle;
int PosyBancle;
PosxBancle = 120;
PosyBancle = 200;
resBancle = RefVBDLL.P_ReWriteTextOut(PosxBancle, PosyBancle, "Aril", 20, 1, 1, lblBalance);
if (resBancle == 0)
MessageBox.Show("打印余额Error", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
else
MessageBox.Show("打印余额OK", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
//打印积分
long resTotal;
int PosxTotal;
int PosyTotal;
PosxTotal = 130;
PosyTotal = 260;
resTotal = RefVBDLL.P_ReWriteTextOut(PosxTotal, PosyTotal, "Aril", 20, 1, 1, lblTotal);
if (resTotal == 0)
MessageBox.Show("打印积分Error", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
else
MessageBox.Show("打印积分OK", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
//打印会员卡类型
long resType;
int PosxType;
int PosyType;
PosxType = 130;
PosyType = 310;
resType = RefVBDLL.P_ReWriteTextOut(PosxType, PosyType, "Aril", 20, 1, 1, lblCardType);
if (resType == 0)
MessageBox.Show("Error", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
else
MessageBox.Show("打印会员卡类型OK", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
//打印图片
try
{
string Path = Application.StartupPath + @"/8.bmp";
long resImage = 100;
int PosXImage = 0;
int PosYImage = 0;
resImage = RefVBDLL.P_ReWriteImageOut(PosXImage, PosYImage, Path);
if (resImage == 0)
MessageBox.Show("Image+OK", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
else
MessageBox.Show("Image+Error","提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
long resSexBImage;
resSexBImage = RefVBDLL.P_ReWritePrint(getOldSn);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "错误运行", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
//最后打印allyn2008-6-12
long resPrint;
resPrint = RefVBDLL.ReWritePrint(getOldSn);
//然后写到磁卡里allyn2008-6-18
try
{
int trackWriter = 8;
long resWriter;
int CountWriter;
byte[] BdataWriter = new byte[255];
CountWriter = lblMemberNo.Length;
BdataWriter = System.Text.Encoding.Default.GetBytes(lblMemberNo);
resWriter = RefVBDLL.ReWriteWriteMagnetic(getOldSn, trackWriter, CountWriter, ref BdataWriter[0]);
}
catch (Exception ex) { ex.GetBaseException(); }
//出卡
RefVBDLL.ReWriteCardOut(getOldSn);
//断开连接
int number = RefVBDLL.ReWriteDisConnect(getOldSn);
btnFlushCard.Enabled = true;
}
}
catch (Exception ex)
{
ex.GetBaseException();
}
}