Program.cs文件,主要是建立互斥,阻止多实例运行(方法还有许多) using System; using System.Collections.Generic; using System.Linq; using System.Windows.Forms; using System.Threading;//Mutex类需要
namespace WindowsFormsApplication2 { static class Program { /// <summary> /// 应用程序的主入口点。 /// </summary> [STAThread] static void Main() { bool createdNew; Mutex mutexFile = new Mutex(false, "WindowsFormsApplication2", out createdNew);//创建互斥 if (createdNew) {//成功创建无进程运行 Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new Form1()); } else {//创建失败有进程在运行 MessageBox.Show("对不起,有一进程在工作", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } } } }
Form1.cs文件,程序的主体,主要是VS如何调用DLL,关闭窗口提示,异常的捕捉等.
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; using System.Runtime.InteropServices;//加动态链接库需要
namespace WindowsFormsApplication2 { public partial class Form1 : Form { [DllImport("winio.dll")] public static extern bool InitializeWinIo(); [DllImport("winio.dll")] public static extern bool GetPortVal(IntPtr wPortAddr, out int pdwPortVal, byte bSize); [DllImport("winio.dll")] public static extern bool SetPortVal(uint wPortAddr, IntPtr dwPortVal, byte bSize); [DllImport("winio.dll")] public static extern byte MapPhysToLin(byte pbPhysAddr, uint dwPhysSize, IntPtr PhysicalMemoryHandle); [DllImport("winio.dll")] public static extern bool UnmapPhysicalMemory(IntPtr PhysicalMemoryHandle, byte pbLinAddr); [DllImport("winio.dll")] public static extern bool GetPhysLong(IntPtr pbPhysAddr, byte pdwPhysVal); [DllImport("winio.dll")] public static extern bool SetPhysLong(IntPtr pbPhysAddr, byte dwPhysVal); [DllImport("winio.dll")] public static extern void ShutdownWinIo(); [DllImport("user32.dll")] public static extern int MapVirtualKey(uint Ucode, uint uMapType);
public Form1() { InitializeComponent(); }
private void Form1_FormClosing(object sender, FormClosingEventArgs e) {//捕捉窗体Close事件,关闭窗口时提示 if (MessageBox.Show("请您确认是否退出(Y/N)", "系统提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes) { try { ShutdownWinIo(); } catch (System.Exception error) {//WinIO卸载失败异常 MessageBox.Show(error.Message, "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } e.Cancel = false;//允许退出系统 } else { e.Cancel = true;//阻止退出系统 } }
private void timer1_Tick(object sender, EventArgs e) {//Timer定时到事件 toolStripStatusLabel3.Text = System.DateTime.Now.ToString(); if (toolStripProgressBar1.Value < toolStripProgressBar1.Maximum) { toolStripProgressBar1.Value += toolStripProgressBar1.Step; } else { toolStripProgressBar1.Value = toolStripProgressBar1.Minimum; } }
private void button2_Click(object sender, EventArgs e) {//WinIO读操作 int pdwPortVal = 0; try { uint wPortAddr = Convert.ToUInt32(comboBox1.Text, 16); if (GetPortVal((IntPtr)wPortAddr, out pdwPortVal, sizeof(byte))) { textBox2.Text = string.Format("{0:x02}", (byte)pdwPortVal); } else { MessageBox.Show("WinIO读操作失败!!!", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } } catch (System.Exception error) {//读出操作异常处理 MessageBox.Show(error.Message, "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } for (int i = 0; i < 8; i ++) { switch (i) { case 0: checkBox8.Checked = ((pdwPortVal & (1 << i)) != 0) ? true : false; break; case 1: checkBox7.Checked = ((pdwPortVal & (1 << i)) != 0) ? true : false; break; case 2: checkBox6.Checked = ((pdwPortVal & (1 << i)) != 0) ? true : false; break; case 3: checkBox5.Checked = ((pdwPortVal & (1 << i)) != 0) ? true : false; break; case 4: checkBox4.Checked = ((pdwPortVal & (1 << i)) != 0) ? true : false; break; case 5: checkBox3.Checked = ((pdwPortVal & (1 << i)) != 0) ? true : false; break; case 6: checkBox2.Checked = ((pdwPortVal & (1 << i)) != 0) ? true : false; break; case 7: checkBox1.Checked = ((pdwPortVal & (1 << i)) != 0) ? true : false; break; } } }
private void Form1_Load(object sender, EventArgs e) {//delphi一般在此初始化 try { InitializeWinIo(); } catch (System.Exception error) {//WinIO加载失败异常处理 MessageBox.Show(error.Message, "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } }
private void button1_Click(object sender, EventArgs e) {//WinIO写操作 try { uint wPortAddr = Convert.ToUInt32(comboBox1.Text, 16); int pdwPortVal = Convert.ToInt32(textBox1.Text, 16); if (!SetPortVal(wPortAddr, (IntPtr)pdwPortVal, sizeof(byte))) { MessageBox.Show("WinIO写入操作失败!!!", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } } catch (System.Exception error) {//写入操作异常处理 MessageBox.Show(error.Message, "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } }
private void textBox1_KeyPress(object sender, KeyPressEventArgs e) {//与delphi一样的处理方法 char ch = e.KeyChar; if (!((ch >= '0' && ch <= '9') || (ch >= 'a' && ch <= 'f') || (ch >= 'A' && ch <= 'F'))) { switch (ch) { case '\x08'://退格 case '\x0d'://回车 break;//放过 default: e.KeyChar = '\x0';//放弃输入的非法字符 MessageBox.Show("请正确输入16进制数!!!", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); break; } } else {//合法16进制数 e.KeyChar |= ' ';//强制转换为小写字母 } }
private void button3_Click(object sender, EventArgs e) {//WinIO读操作 int pdwPortVal = 0; try { uint wPortAddr = Convert.ToUInt32(comboBox1.Text, 16) + 1;//状态口 if (GetPortVal((IntPtr)wPortAddr, out pdwPortVal, sizeof(byte))) { textBox3.Text = string.Format("{0:x02}", (byte)pdwPortVal); } else { MessageBox.Show("WinIO读操作失败!!!", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } } catch (System.Exception error) {//读出操作异常处理 MessageBox.Show(error.Message, "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } for (int i = 0; i < 8; i++) { switch (i) { case 0: checkBox9.Checked = ((pdwPortVal & (1 << i)) != 0) ? true : false; break; case 1: checkBox10.Checked = ((pdwPortVal & (1 << i)) != 0) ? true : false; break; case 2: checkBox11.Checked = ((pdwPortVal & (1 << i)) != 0) ? true : false; break; case 3: checkBox12.Checked = ((pdwPortVal & (1 << i)) != 0) ? true : false; break; case 4: checkBox13.Checked = ((pdwPortVal & (1 << i)) != 0) ? true : false; break; case 5: checkBox14.Checked = ((pdwPortVal & (1 << i)) != 0) ? true : false; break; case 6: checkBox15.Checked = ((pdwPortVal & (1 << i)) != 0) ? true : false; break; case 7: checkBox16.Checked = ((pdwPortVal & (1 << i)) != 0) ? true : false; break; } } }
private void textBox4_KeyPress(object sender, KeyPressEventArgs e) {//与delphi一样的处理方法 char ch = e.KeyChar; if (!((ch >= '0' && ch <= '9') || (ch >= 'a' && ch <= 'f') || (ch >= 'A' && ch <= 'F'))) { switch (ch) { case '\x08'://退格 case '\x0d'://回车 break;//放过 default://捕捉到的非法字符 e.KeyChar = '\x0';//放弃输入的非法字符 MessageBox.Show("请正确输入16进制数!!!", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); break; } } else {//合法16进制数 e.KeyChar |= ' ';//强制转换为小写字母 } }
private void button4_Click(object sender, EventArgs e) {//控制口WinIO写操作 try { uint wPortAddr = Convert.ToUInt32(comboBox1.Text, 16) + 2;//控制口 int pdwPortVal = Convert.ToInt32(textBox4.Text, 16); if (!SetPortVal(wPortAddr, (IntPtr)pdwPortVal, sizeof(byte))) { MessageBox.Show("WinIO写入操作失败!!!", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } } catch (System.Exception error) {//写入操作异常处理 MessageBox.Show(error.Message, "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } }
private void button5_Click(object sender, EventArgs e) {//控制口WinIO读操作 int pdwPortVal = 0; try { uint wPortAddr = Convert.ToUInt32(comboBox1.Text, 16) + 2;//控制口 if (GetPortVal((IntPtr)wPortAddr, out pdwPortVal, sizeof(byte))) { textBox5.Text = string.Format("{0:x02}", (byte)pdwPortVal); } else { MessageBox.Show("WinIO读操作失败!!!", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } } catch (System.Exception error) {//读出操作异常处理 MessageBox.Show(error.Message, "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } for (int i = 0; i < 8; i++) { switch (i) { case 0: checkBox17.Checked = ((pdwPortVal & (1 << i)) != 0) ? true : false; break; case 1: checkBox18.Checked = ((pdwPortVal & (1 << i)) != 0) ? true : false; break; case 2: checkBox19.Checked = ((pdwPortVal & (1 << i)) != 0) ? true : false; break; case 3: checkBox20.Checked = ((pdwPortVal & (1 << i)) != 0) ? true : false; break; case 4: checkBox21.Checked = ((pdwPortVal & (1 << i)) != 0) ? true : false; break; case 5: checkBox22.Checked = ((pdwPortVal & (1 << i)) != 0) ? true : false; break; case 6: checkBox23.Checked = ((pdwPortVal & (1 << i)) != 0) ? true : false; break; case 7: checkBox24.Checked = ((pdwPortVal & (1 << i)) != 0) ? true : false; break; } } }
private void xToolStripMenuItem_Click(object sender, EventArgs e) {//主菜单内退出系统 Close(); }
private void aToolStripMenuItem_Click(object sender, EventArgs e) {//主菜单的帮助 MessageBox.Show("C#学习菜鸟作\nHotPower@126.com", "并口调试帮助提示", MessageBoxButtons.OK); }
} }
|
|