using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
//Private Declare Function APIBeep Lib "kernel32" Alias "Beep" (ByVal dwFreq As Long, ByVal dwDuration As Long) As Long
        [System.Runtime.InteropServices.DllImport("kernel32.dll")]
        public static extern int Beep(int a, int b);

        public Form1()
        {
            InitializeComponent();
        }

        private void button15_Click(object sender, EventArgs e)
        {
            listBox1.Items.Clear();
        }

        private void button16_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            string stmp,cPath;
            int nRet;
            cPath = "";
            nRet = axSynCardOcx1.SetPhotoPath(0, cPath);
            stmp = Convert.ToString(System.DateTime.Now) + "  照片存放路径设置为 C:,nRet =" + Convert.ToString(nRet);
            listBox1.Items.Add(stmp);

        }

        private void button5_Click(object sender, EventArgs e)
        {
            string stmp, cPath;
            int nRet;
            cPath = "";
            nRet = axSynCardOcx1.SetPhotoPath(1, cPath);
            stmp = Convert.ToString(System.DateTime.Now) + "  照片存放路径设置为当前路径,nRet =" + Convert.ToString(nRet);
            listBox1.Items.Add(stmp);
        }

        private void button6_Click(object sender, EventArgs e)
        {
            string stmp;
            int nRet;
            byte[] cPath = new byte[255];
            folderBrowserDialog1.SelectedPath = "c:\\";
            folderBrowserDialog1.ShowDialog();
            stmp = folderBrowserDialog1.SelectedPath;
            nRet = axSynCardOcx1.SetPhotoPath(1, stmp);
            stmp = Convert.ToString(System.DateTime.Now) + "  照片存放路径设置为" + folderBrowserDialog1.SelectedPath + ",nRet =" + Convert.ToString(nRet);
            listBox1.Items.Add(stmp);
        }

        private void button7_Click(object sender, EventArgs e)
        {
            string stmp;
            int nRet;
            nRet = axSynCardOcx1.SetPhotoType(0);
            stmp = Convert.ToString(System.DateTime.Now) + "  照片存放格式为Bmp，nRet =" + Convert.ToString(nRet);
            listBox1.Items.Add(stmp);
        }

        private void button8_Click(object sender, EventArgs e)
        {
            string stmp;
            int nRet;
            nRet = axSynCardOcx1.SetPhotoType(2);
            stmp = Convert.ToString(System.DateTime.Now) + "  照片存放格式为Jpeg，nRet =" + Convert.ToString(nRet);
            listBox1.Items.Add(stmp);
        }

        private void button9_Click(object sender, EventArgs e)
        {
            string stmp;
            int nRet;
            nRet = axSynCardOcx1.SetPhotoType(2);
            stmp = Convert.ToString(System.DateTime.Now) + "  照片存放格式为Base64，nRet =" + Convert.ToString(nRet);
            listBox1.Items.Add(stmp);
        }

        private void button10_Click(object sender, EventArgs e)
        {
            string stmp;
            int nRet;
            nRet = axSynCardOcx1.SetPhotoName(0);
            stmp = Convert.ToString(System.DateTime.Now) + "  照片保存文件名格式设置为 tmp，nRet =" + Convert.ToString(nRet);
            listBox1.Items.Add(stmp);
        }

        private void button11_Click(object sender, EventArgs e)
        {
            string stmp;
            int nRet;
            nRet = axSynCardOcx1.SetPhotoName(1);
            stmp = Convert.ToString(System.DateTime.Now) + "  照片保存文件名格式设置为 姓名，nRet =" + Convert.ToString(nRet);
            listBox1.Items.Add(stmp);
        }

        private void button12_Click(object sender, EventArgs e)
        {
            string stmp;
            int nRet;
            nRet = axSynCardOcx1.SetPhotoName(2);
            stmp = Convert.ToString(System.DateTime.Now) + "  照片保存文件名格式设置为 身份证号，nRet =" + Convert.ToString(nRet);
            listBox1.Items.Add(stmp);
        }

        private void button13_Click(object sender, EventArgs e)
        {
            string stmp;
            int nRet;
            nRet = axSynCardOcx1.SetPhotoName(3);
            stmp = Convert.ToString(System.DateTime.Now) + "  照片保存文件名格式设置为 姓名_身份证号，nRet =" + Convert.ToString(nRet);
            listBox1.Items.Add(stmp);

        }

        private void button1_Click(object sender, EventArgs e)
        {
            string stmp;
            int i;
            i = axSynCardOcx1.FindReader();
            if (i > 0)
            {
                Beep(4000, 200);
                button2.Enabled = true;
                button3.Enabled = true;
                button14.Enabled = true;
                stmp = Convert.ToString(i);
                if (i > 1000)
                {
                    stmp = Convert.ToString(System.DateTime.Now) + "  读卡器连接在USB " + stmp;
                    listBox1.Items.Add(stmp);
                }
                else
                {
                    stmp = Convert.ToString(System.DateTime.Now) + "  读卡器连接在COM " + stmp;
                    listBox1.Items.Add(stmp);

                }
            }
            else
            {
                button2.Enabled = false;
                button3.Enabled = false;
                button14.Enabled = false;
                stmp = Convert.ToString(System.DateTime.Now) + "  没有找到读卡器";
                listBox1.Items.Add(stmp);
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            string stmp; 
            int nRet;
            axSynCardOcx1.SetReadType(0);
            nRet = axSynCardOcx1.ReadCardMsg();
            if (nRet == 0)
            {
                stmp = Convert.ToString(System.DateTime.Now) + "  姓名:" + axSynCardOcx1.NameA;  
                listBox1.Items.Add(stmp);
                stmp = Convert.ToString(System.DateTime.Now) + "  性别:" + axSynCardOcx1.Sex;
                listBox1.Items.Add(stmp);
                stmp = Convert.ToString(System.DateTime.Now) + "  民族:" + axSynCardOcx1.Nation;
                listBox1.Items.Add(stmp);
                stmp = Convert.ToString(System.DateTime.Now) + "  出生日期:" + axSynCardOcx1.Born;
                listBox1.Items.Add(stmp);
                stmp = Convert.ToString(System.DateTime.Now) + "  地址:" + axSynCardOcx1.Address;
                listBox1.Items.Add(stmp);
                stmp = Convert.ToString(System.DateTime.Now) + "  身份证号:" + axSynCardOcx1.CardNo;
                listBox1.Items.Add(stmp);
                stmp = Convert.ToString(System.DateTime.Now) + "  发证机关:" + axSynCardOcx1.Police;
                listBox1.Items.Add(stmp);
                stmp = Convert.ToString(System.DateTime.Now) + "  有效期开始:" + axSynCardOcx1.UserLifeB;
                listBox1.Items.Add(stmp);
                stmp = Convert.ToString(System.DateTime.Now) + "  有效期结束:" + axSynCardOcx1.UserLifeE;
                listBox1.Items.Add(stmp);
                stmp = Convert.ToString(System.DateTime.Now) + "  照片文件名:" + axSynCardOcx1.PhotoName;
                listBox1.Items.Add(stmp);
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            string stmp;
            stmp = axSynCardOcx1.GetSAMID();
            stmp = Convert.ToString(System.DateTime.Now) + "  SAMID为:" + stmp;
            listBox1.Items.Add(stmp);
        }

        private void button14_Click(object sender, EventArgs e)
        {
            axSynCardOcx1.SetLoopTime(1000);
            axSynCardOcx1.SetReadType(1);
        }

        private void axSynCardOcx1_CardIn(object sender, AxSYNCARDOCXLib._DSynCardOcxEvents_CardInEvent e)
        {
            string stmp;
            if (e.state == 1)
            {
                stmp = Convert.ToString(System.DateTime.Now) + "  姓名:" + axSynCardOcx1.NameA;
                listBox1.Items.Add(stmp);
                stmp = Convert.ToString(System.DateTime.Now) + "  性别:" + axSynCardOcx1.Sex;
                listBox1.Items.Add(stmp);
                stmp = Convert.ToString(System.DateTime.Now) + "  民族:" + axSynCardOcx1.Nation;
                listBox1.Items.Add(stmp);
                stmp = Convert.ToString(System.DateTime.Now) + "  出生日期:" + axSynCardOcx1.Born;
                listBox1.Items.Add(stmp);
                stmp = Convert.ToString(System.DateTime.Now) + "  地址:" + axSynCardOcx1.Address;
                listBox1.Items.Add(stmp);
                stmp = Convert.ToString(System.DateTime.Now) + "  身份证号:" + axSynCardOcx1.CardNo;
                listBox1.Items.Add(stmp);
                stmp = Convert.ToString(System.DateTime.Now) + "  发证机关:" + axSynCardOcx1.Police;
                listBox1.Items.Add(stmp);
                stmp = Convert.ToString(System.DateTime.Now) + "  有效期开始:" + axSynCardOcx1.UserLifeB;
                listBox1.Items.Add(stmp);
                stmp = Convert.ToString(System.DateTime.Now) + "  有效期结束:" + axSynCardOcx1.UserLifeE;
                listBox1.Items.Add(stmp);
                stmp = Convert.ToString(System.DateTime.Now) + "  照片文件名:" + axSynCardOcx1.PhotoName;
                listBox1.Items.Add(stmp);
            }
        }
    }
}
