查看: 1594|回复: 0

VB.NET下基于控件的系统托盘及退出保护设计源码及贴图 [复制链接]

hotpower (离线)
积分
82850
帖子
313
发表于 2009-4-3 00:14:00 |显示全部楼层
关键词: NET , 控件 , 托盘 , 系统 , 源码
Public Class Form1
    Dim SystemExitFlag = False
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Resize
    End Sub
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        TextBox1.Text = "俺是农民,俺怕谁???"
        RichTextBox1.AppendText(TextBox1.Text)
    End Sub
    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        ToolStripStatusLabel3.Text = DateTime.Now.ToString()
        If ToolStripProgressBar1.Value < ToolStripProgressBar1.Maximum Then
            ToolStripProgressBar1.Value = ToolStripProgressBar1.Value + ToolStripProgressBar1.Step
        Else
            ToolStripProgressBar1.Value = ToolStripProgressBar1.Minimum
        End If
    End Sub
    Private Sub NotifyIcon1_MouseDoubleClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles NotifyIcon1.MouseDoubleClick
        Me.Visible = Not Me.Visible '隐藏/显示窗体转换
        If Me.Visible Then '准备显示窗体
            '防止最小化不正常显示窗体
            NotifyIcon1.Icon = System.Drawing.Icon.ExtractAssociatedIcon("Hot.ico")
            Me.WindowState = System.Windows.Forms.FormWindowState.Normal
        Else
            NotifyIcon1.Icon = System.Drawing.Icon.ExtractAssociatedIcon("Power.ico")
        End If
    End Sub
    Private Sub NotifyIcon1_MouseClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles NotifyIcon1.MouseClick
        '鼠标右键系统托盘事件处理
        ContextMenuStrip1.Visible = e.Button = MouseButtons.Right '点击鼠标右键后显示菜单
    End Sub
    Private Sub Form1_Resize(ByVal sender As System.Object, ByVal e As System.EventArgs)
        '在Resize事件处理中拦截最小最大消息到托盘
        Select Case Me.WindowState
            Case FormWindowState.Maximized, FormWindowState.Minimized
                Me.WindowState = System.Windows.Forms.FormWindowState.Normal
                Me.Visible = False '隐藏窗体转换
                NotifyIcon1.Icon = System.Drawing.Icon.ExtractAssociatedIcon("Power.ico") '显示隐藏的托盘图标
        End Select
    End Sub
    Private Sub Form1_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
        If SystemExitFlag = True AndAlso MessageBox.Show("请您确认是否退出(Y/N)", "系统提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information) = DialogResult.Yes Then
            e.Cancel = False '托盘退出菜单允许退出系统
        Else
            e.Cancel = True '最小最大关闭菜单退出钮都会阻止退出系统
            Me.WindowState = System.Windows.Forms.FormWindowState.Normal
            Me.Visible = False '隐藏窗体转换
            NotifyIcon1.Icon = System.Drawing.Icon.ExtractAssociatedIcon("Power.ico") '显示隐藏的托盘图标
            'MessageBox.Show("请从系统托盘中退出!!!", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
        End If
    End Sub
    Private Sub 退出ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 退出ToolStripMenuItem.Click
        ToolStripStatusLabel2.Text = "非法退出"
        SystemExitFlag = False
        Close() '退入托盘
    End Sub
    Private Sub 退出ToolStripMenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 退出ToolStripMenuItem1.Click
        ToolStripStatusLabel2.Text = "正常退出"
        SystemExitFlag = True
        Close() '退出系统
    End Sub
    Private Sub 关于ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 关于ToolStripMenuItem.Click
        'System.Diagnostics.Process.Start("Explorer", "http:\\www.hotpage.net.cn")'运行IE
        System.Diagnostics.Process.Start("http:\\www.hotpage.net.cn") '自动启动IE
    End Sub
    Private Sub 我不会VBToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 我不会VBToolStripMenuItem.Click
        System.Diagnostics.Process.Start("http:\\www.hotpower.org") '自动启动IE
    End Sub
    Private Sub RichTextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RichTextBox1.TextChanged
    End Sub
End Class









●█〓██▄▄▄▄▄▄ ●●●●●●→ ''''╭WWWW╮
▄▅██████▅▄▃▂ 传播非典灌水四方 ( ●_●)
███天█马█行█空████ '''',,,;,;,;'''/▇\''
◥⊙▲⊙▲⊙▲⊙▲⊙▲⊙▲◤ 群魔乱舞见阳光/MMMM\
您需要登录后才可以发表评论 登录 | 立即注册

关于我们  -  服务条款  -  使用指南  -  站点地图  -  友情链接  -  联系我们
电子工程网 © 版权所有   京ICP备11013910号 | 京公网安备11010502021702
回顶部