C#下基于控件的系统托盘及退出保护设计源码及贴图
http://blog.C#下基于控件的系统托盘及退出保护设计源码及贴图
using System;
using System.Collections.Generic;
using
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 bool DllLoadResult = false;
public bool SystemExitFlag = false;//阻止关闭钮非法退出,只允许从托盘退出
public Form1()
{
InitializeComponent();
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{//捕捉窗体Close事件,关闭窗口时提示
if (SystemExitFlag && MessageBox.Show("请您确认是否退出(Y/N)", "系统提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes)
{
try
{
ShutdownWinIo();//卸载WinIO
}
catch (System.Exception error)
{//WinIO卸载失败异常
if (DllLoadResult)
{
MessageBox.Show(error.Message, "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
e.Cancel = false;//允许退出系统
}
else
{
e.Cancel = true;//阻止退出系统
this.WindowState = System.Windows.Forms.FormWindowState.Normal;
this.Visible = false;//隐藏窗体转换
notifyIcon1.Icon = System.Drawing.Icon.ExtractAssociatedIcon("Power.ico");//显示隐藏的托盘图标
// MessageBox.Show("请从系统托盘中退出!!!", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
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.Error);
}
}
catch (System.Exception error)
{//读出操作异常处理
MessageBox.Show(error.Message, "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
for (int i = 8; i >= 1; i--)
{
((CheckBox)this.Controls.Find("checkBox" + i, true)[0]).Checked = (pdwPortVal & (1 << (8 - i))) != 0;
}
/*
for (int i = 0; i < 8; i++)
{
switch (i)
{
case 0:
checkBox8.Checked = (pdwPortVal & (1 << i)) != 0;
break;
case 1:
checkBox7.Checked = (pdwPortVal & (1 << i)) != 0;
break;
case 2:
checkBox6.Checked = (pdwPortVal & (1 << i)) != 0;
break;
case 3:
checkBox5.Checked = (pdwPortVal & (1 << i)) != 0;
break;
case 4:
checkBox4.Checked = (pdwPortVal & (1 << i)) != 0;
break;
case 5:
checkBox3.Checked = (pdwPortVal & (1 << i)) != 0;
break;
case 6:
checkBox2.Checked = (pdwPortVal & (1 << i)) != 0;
break;
case 7:
checkBox1.Checked = (pdwPortVal & (1 << i)) != 0;
break;
}
}
*/
}
private void Form1_Load(object sender, EventArgs e)
{//delphi一般在此初始化
/*在窗体属性内设置下列3项,基于控件的设计应该在组建的属性栏内改写(以下是“VC转业兵的做法”)
this.ShowInTaskbar = false;//图标不出现任务条内
this.MaximizeBox = false;
this.MinimizeBox = false;
notifyIcon1.ContextMenuStrip = contextMenuStrip1;
notifyIcon1.Icon = System.Drawing.Icon.ExtractAssociatedIcon("Hot.ico");
*/
try
{
DllLoadResult = InitializeWinIo();//加载WinIO
}
catch (System.Exception error)
{//WinIO加载失败异常处理
MessageBox.Show(error.Message, "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
if (!DllLoadResult)
{//加载WinIO失败
Application.Exit();//退出系统,同Close()方法
}
}
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.Error);
}
}
catch (System.Exception error)
{//写入操作异常处理
MessageBox.Show(error.Message, "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
}
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{//与delphi一样的处理方法
char ch = char.ToUpper(e.KeyChar);//转换为大写字母
if (!((ch >= '0' && ch <= '9') || (ch >= 'A' && ch <= 'F')))
//或 if (!((ch >= (char)Keys.D0 && ch <= (char)Keys.D9) || (ch >= (char)Keys.A && ch <= (char)Keys.F)))
{
switch (ch)
{
case (char)Keys.Back://退格
case (char)Keys.Enter://回车
break;//放过
default:
e.KeyChar = '\x0';//放弃输入的非法字符
MessageBox.Show("请正确输入16进制数!!!", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
break;
}
}
else
{//合法16进制数
e.KeyChar = ch;//强制转换为大写字母
}
}
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.Error);
}
}
catch (System.Exception error)
{//读出操作异常处理
MessageBox.Show(error.Message, "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
for (int i = 9; i <= 16; i++)
{
((CheckBox)this.Controls.Find("checkBox" + i, true)[0]).Checked = (pdwPortVal & (1 << (i - 9))) != 0;
}
/*
for (int i = 0; i < 8; i++)
{
switch (i)
{
case 0:
checkBox9.Checked = (pdwPortVal & (1 << i)) != 0;
break;
case 1:
checkBox10.Checked = (pdwPortVal & (1 << i)) != 0;
break;
case 2:
checkBox11.Checked = (pdwPortVal & (1 << i)) != 0;
break;
case 3:
checkBox12.Checked = (pdwPortVal & (1 << i)) != 0;
break;
case 4:
checkBox13.Checked = (pdwPortVal & (1 << i)) != 0;
break;
case 5:
checkBox14.Checked = (pdwPortVal & (1 << i)) != 0;
break;
case 6:
checkBox15.Checked = (pdwPortVal & (1 << i)) != 0;
break;
case 7:
checkBox16.Checked = (pdwPortVal & (1 << i)) != 0;
break;
}
}
*/
}
private void textBox4_KeyPress(object sender, KeyPressEventArgs e)
{//与delphi一样的处理方法
char ch = char.ToUpper(e.KeyChar);//转换为大写字母
if (!((ch >= '0' && ch <= '9') || (ch >= 'A' && ch <= 'F')))
//或 if (!((ch >= (char)Keys.D0 && ch <= (char)Keys.D9) || (ch >= (char)Keys.A && ch <= (char)Keys.F)))
{
switch (ch)
{
case (char)Keys.Back://退格
case (char)Keys.Enter://回车
break;//放过
default://捕捉到的非法字符
e.KeyChar = '\x0';//放弃输入的非法字符
MessageBox.Show("请正确输入16进制数!!!", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
break;
}
}
else
{//合法16进制数
e.KeyChar = ch;//强制转换为大写字母
}
}
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.Error);
}
}
catch (System.Exception error)
{//写入操作异常处理
MessageBox.Show(error.Message, "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
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.Error);
}
}
catch (System.Exception error)
{//读出操作异常处理
MessageBox.Show(error.Message, "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
for (int i = 17; i <= 24; i++)
{
((CheckBox)this.Controls.Find("checkBox" + i, true)[0]).Checked = (pdwPortVal & (1 << (i - 17))) != 0;
}
/*
for (int i = 0; i < 8; i++)
{
switch (i)
{
case 0:
checkBox17.Checked = (pdwPortVal & (1 << i)) != 0;
break;
case 1:
checkBox18.Checked = (pdwPortVal & (1 << i)) != 0;
break;
case 2:
checkBox19.Checked = (pdwPortVal & (1 << i)) != 0;
break;
case 3:
checkBox20.Checked = (pdwPortVal & (1 << i)) != 0;
break;
case 4:
checkBox21.Checked = (pdwPortVal & (1 << i)) != 0;
break;
case 5:
checkBox22.Checked = (pdwPortVal & (1 << i)) != 0;
break;
case 6:
checkBox23.Checked = (pdwPortVal & (1 << i)) != 0;
break;
case 7:
checkBox24.Checked = (pdwPortVal & (1 << i)) != 0;
break;
}
}
*/
}
private void xToolStripMenuItem_Click(object sender, EventArgs e)
{//主菜单内退出系统
SystemExitFlag = false;//只能从托盘退出系统
Close();//警告后继续
//或 Application.Exit();
}
private void aToolStripMenuItem_Click(object sender, EventArgs e)
{//主菜单的帮助
MessageBox.Show("C#学习菜鸟作\nHotPower@, "并口调试帮助提示", MessageBoxButtons.OK);
}
private void textBox3_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar != (char)Keys.Back)//只允许退格建
{
MessageBox.Show("不能输入字符!!!", "系统提示", MessageBoxButtons.OK);
e.KeyChar = '\0';//不允许输入字符
}
}
private void textBox2_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar != (char)Keys.Back)//只允许退格建
{
MessageBox.Show("不能输入字符!!!", "系统提示", MessageBoxButtons.OK);
e.KeyChar = '\0';//不允许输入字符
}
}
private void textBox5_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar != (char)Keys.Back)//只允许退格建
{
MessageBox.Show("不能输入字符!!!", "系统提示", MessageBoxButtons.OK);
e.KeyChar = '\0';//不允许输入字符
}
}
private void notifyIcon1_MouseClick(object sender, MouseEventArgs e)
{//鼠标右键系统托盘事件处理
contextMenuStrip1.Visible = e.Button == MouseButtons.Right;//显示菜单
}
private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
{//鼠标双击系统托盘事件处理
this.Visible = !this.Visible;//隐藏/显示窗体转换
if (this.Visible)//准备显示窗体
{//防止最小化不正常显示窗体
notifyIcon1.Icon = System.Drawing.Icon.ExtractAssociatedIcon("Hot.ico");
this.WindowState = System.Windows.Forms.FormWindowState.Normal;
}
else
{
notifyIcon1.Icon = System.Drawing.Icon.ExtractAssociatedIcon("Power.ico");
}
}
private void ToolStripMenuItem_ExitClick(object sender, EventArgs e)
{
SystemExitFlag = true;//只能从托盘退出系统
Close();//真的退出系统
//或 Application.Exit();
}
private void 关于ToolStripMenuItem_Click(object sender, EventArgs e)
{
// System.Diagnostics.Process.Start("Explorer", "http:\\\\www.);//运行IE
System.Diagnostics.Process.Start("http:\\\\www.);//自动启动IE
}
private void Form1_Resize(object sender, EventArgs e)
{//在Resize事件处理中拦截最小最大消息到托盘
switch (this.WindowState)
{
case FormWindowState.Maximized:
case FormWindowState.Minimized:
this.WindowState = System.Windows.Forms.FormWindowState.Normal;
this.Visible = false;//隐藏窗体转换
notifyIcon1.Icon = System.Drawing.Icon.ExtractAssociatedIcon("Power.ico");//显示隐藏的托盘图标
break;
}
}
}
}