WinForm最小化到系统托盘后,再次激活需要验证的实现方法

主要思路:通过最小化来将任务放入系统托盘区。然后通过模式对话框的方法将验证用户输入的验证密码是否正确,正确则显示主窗体,否则忽略。我主要是使用了模式对话框。这里值得注意的就是验证模式对话框的返回值,如果用户输入密码正确,则验证窗体返回的值即为DialogResult.OK,反之则返回DialogResult.Cancel

主窗体:main
  控件:notifyIcon(notifyIcon,托盘显示)
验证窗体:Activate
  控件:Activate_txtB(Textbox,输入密码),btn_OK(Button,确认),btn_Cancel(Button,取消)

1、单击主窗体最小化按键,将任务放入系统托盘
private void main_SizeChanged(object sender, EventArgs e)
{
    if (this.WindowState == FormWindowState.Minimized)
    {
        this.WindowState = FormWindowState.Normal;
        notifyIcon.Visible = true;
        this.Hide();
        this.ShowInTaskbar = false;
    }
}

2、点击系统托盘区的任务图标,激活验证窗体,并根据验证结果做相应操作
private void notifyIcon_Click(object sender, EventArgs e)
{
        Activate frmActivate = new Activate();
        if (frmActivate.ShowDialog(this) == DialogResult.OK)
        {
                this.ShowInTaskbar = true;
                this.Show();
                this.Activate();
                notifyIcon.Visible = false;
                this.WindowState = FormWindowState.Maximized;
        }
        frmActivate.Dispose();
}

3、验证窗体返回验证结果,即写btn_OK(确认)的事件
private void btn_OK_Click(object sender, EventArgs e)
{
        //获取数据库中记忆的验证密码,采用MD5加密过的
        string tmp_pass = myData.GetFieldValue("SysInfo", "Activate", "");
        //获取用户输入的密码,采用MD5加密
        string tmp_input = myClass.returnMD5(this.Activate_txtB.Text.ToString().Trim());
        if (tmp_input == tmp_pass)
        {
                this.DialogResult = DialogResult.OK;
        }
        else
        {
        this.DialogResult = DialogResult.Cancel;
        }
}

这里是广告


posted @ 2006-12-27 11:37  antsoftcn  阅读(921)  评论(0编辑  收藏  举报