[VB.net]Timer核心源代码(时间使者)

前端之家收集整理的这篇文章主要介绍了[VB.net]Timer核心源代码(时间使者)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

这个程序用于定时操作。

其实这份代码个人觉得效率不够高,本人暂时没空优化,不好意思,那位朋友看后有建议的可以联系我。

mailto:wangjingkaibear@live.cn


以下是代码

原本是在Visual Basic 2008 环境下编写的,但回去时装了Visual Basic 2010,它把代码升级了,我看起来好像没变化,不知道怎样。代码有很多用了.net Framework 3.5的标准,所以需要.net Framework3.5支持,请注意。

Public Class Main

    '添加工具提示
    Private Sub Me_Load(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles MyBase.Load
        Me.Hide()
        If My.Computer.FileSystem.FileExists(Application.StartupPath & "/zh-CN/时间使者 Free Edition.resources.dll") = True Then
            Me.Show()
        Else
            MsgBox("程序检测到程序文件夹‘zh-CN’中的‘时间使者 Free Edition.resources.dll’文件不存在或名称改变,请将文件名改回‘时间使者 Free Edition.resources.dll’或重新安装软件",MsgBoxStyle.OkOnly,"启动错误")
            Me.Close()
        End If
        Dim tooltip1 As New ToolTip
        tooltip1.SetToolTip(TX_T_SJ1,"这里应该输入完整的时间,即闹钟响起的时间。(输入格式:HH:mm:ss,24小时制)")
        tooltip1.SetToolTip(TX_T_JG1,"输入一个时间间隔,即在从现在起再过这个时间后,闹钟响起。(单位:秒)")
        tooltip1.SetToolTip(DSGJ_T_SJ1,"这里应该输入完整的时间,即关机的时间。(输入格式:HH:mm:ss,24小时制)")
        tooltip1.SetToolTip(DSGJ_T_YC1,"非必要")
    End Sub

    '获取系统时间并显示
    Private Sub HQSJ11_Tick(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles HQSJ1.Tick
        Dim time1 As DateTime
        time1 = My.Computer.Clock.LocalTime.ToLongTimeString
        DQSJ1.Text = time1
    End Sub

    '闹钟提醒键变化
    Private Sub Tixing1_Click(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles Tixing1.Click
        If Me.Height = 178 Then
            Me.Height = 420
            Tixing1.Text = "闹钟提醒 ∧"
            DSGJ1.Enabled = False
        Else
            Me.Height = 178
            If DSGJ_B_QX1.Enabled = False Then '判断定时关机是否未开启
                DSGJ1.Enabled = True
            End If
            TX_C_FS1.Checked = False
            TX_C_FS2.Checked = False
            TX_T_SJ1.Text = "请填入完整时间"
            TX_T_JG1.Text = "请填入时间间隔"
            TX_T_XX1.Text = ""
            Tixing1.Text = "闹钟提醒 ∨"
        End If
    End Sub
    '定时关机键变化
    Private Sub DSGJ1_Click(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles DSGJ1.Click
        Dim XT2 As String
        If Me.Width = 317 Then
            Me.Width = 553
            XT2 = My.Settings.GJ_XT1
            If XT2 <> "" Then
                If XT2 = "1" Then
                    DSGJ_C_XT1.Checked = True
                Else
                    DSGJ_C_XT2.Checked = True
                End If
            End If
            Tixing1.Enabled = False
            DSGJ1.Text = "定时关机<"
        Else
            Me.Width = 317
            DSGJ_T_SJ1.Text = "请填入完整时间"
            DSGJ_T_YC1.Text = ""
            DSGJ_C_XT1.Checked = False
            DSGJ_C_XT1.Checked = False
            If TX_B_QX1.Enabled = False Then '判断闹钟提醒是否未开启
                Tixing1.Enabled = True
            End If
            DSGJ1.Text = "定时关机>"
        End If
    End Sub
    '设置按钮
    Private Sub Setting1_Click(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles Setting1.Click
        Me.Enabled = False
        Setting.Show()
    End Sub
    '时间计算器按钮变化
    Private Sub TX_B_SJZHQ1_Click(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles TX_B_SJZHQ1.Click
        If Me.Height = 420 Then
            Me.Height = 517
        Else
            Me.Height = 420
        End If
        TX_T_XS1.Text = ""
        TX_T_FZ1.Text = ""
    End Sub


    '方式选择及控件开启
    Private Sub TX_C_FS1_CheckedChanged(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles TX_C_FS1.CheckedChanged
        If TX_C_FS1.Checked = True Then
            TX_T_SJ1.Enabled = True
            TX_T_XX1.Enabled = True
        Else
            TX_T_SJ1.Text = "请填入完整时间"
            TX_T_SJ1.ForeColor = Color.Gray
            TX_T_SJ1.Enabled = False
            TX_B_KQ.Enabled = False
            TX_T_XX1.Enabled = False
        End If
    End Sub
    Private Sub TX_C_FS2_CheckedChanged(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles TX_C_FS2.CheckedChanged
        If TX_C_FS2.Checked = True Then
            TX_T_JG1.Enabled = True
            TX_T_XX1.Enabled = True
        Else
            TX_T_JG1.Text = "请填入时间间隔"
            TX_T_JG1.ForeColor = Color.Gray
            TX_T_JG1.Enabled = False
            TX_B_KQ.Enabled = False
            TX_T_XX1.Enabled = False
        End If
    End Sub

    '提醒时间文本框变化的相关影响
    Private Sub TX_T_SJ1_TextChanged(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles TX_T_SJ1.TextChanged
        If TX_T_SJ1.Text = "" Or TX_T_SJ1.Text = "请填入完整时间" Then
            TX_B_KQ.Enabled = False
        Else
            TX_B_KQ.Enabled = True
        End If
    End Sub
    '提醒间隔文本框变化的相关影响
    Private Sub TX_T_JG1_TextChanged(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles TX_T_JG1.TextChanged
        If TX_T_JG1.Text = "" Or TX_T_JG1.Text = "请填入时间间隔" Then
            TX_B_KQ.Enabled = False
        Else
            TX_B_KQ.Enabled = True
        End If
    End Sub

    '闹钟提醒俩文本框鼠标按下事件
    Private Sub TX_T_SJ1_MouseDown(ByVal sender As System.Object,ByVal e As System.Windows.Forms.MouseEventArgs) Handles TX_T_SJ1.MouseDown
        If TX_T_SJ1.Text = "请填入完整时间" Then
            TX_T_SJ1.Text = ""
            TX_T_SJ1.ForeColor = Color.RoyalBlue
        End If
    End Sub
    Private Sub TX_T_JG1_MouseDown(ByVal sender As System.Object,ByVal e As System.Windows.Forms.MouseEventArgs) Handles TX_T_JG1.MouseDown
        If TX_T_JG1.Text = "请填入时间间隔" Then
            TX_T_JG1.Text = ""
            TX_T_JG1.ForeColor = Color.RoyalBlue
        End If
    End Sub

    '闹钟提醒开启事件
    Private Sub TX_B_KQ_Click(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles TX_B_KQ.Click
        Dim instance As DateTime
        Dim value As Double
        Dim returnValue As DateTime
        If TX_C_FS1.Checked = True Then
            My.Settings.TX_SJ1 = TX_T_SJ1.Text
            My.Settings.TX_XX1 = TX_T_XX1.Text
            TX_L_Show.Text = "已开启的闹钟提醒:" & TX_T_SJ1.Text
            TX_T_SJ1.Text = "请填入完整时间"
        Else
            instance = DQSJ1.Text
            value = TX_T_JG1.Text
            returnValue = instance.AddMinutes(value)
            My.Settings.TX_JG1 = returnValue
            My.Settings.TX_XX1 = TX_T_XX1.Text
            TX_L_Show.Text = "已开启的闹钟提醒:" & returnValue
            TX_T_JG1.Text = "请填入时间间隔"
        End If
        Me.Height = 176
        TX_T_XX1.Text = ""
        TX_C_FS1.Checked = False
        TX_C_FS2.Checked = False
        Tixing1.Text = "闹钟提醒 ∨"
        Tixing1.Enabled = False
        TX_B_QX1.Enabled = True
        If DSGJ_B_QX1.Enabled = False Then
            DSGJ1.Enabled = True
        End If
    End Sub

    Private Sub TX_Timer1_Tick(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles TX_Timer1.Tick
        If DQSJ1.Text = My.Settings.TX_SJ1 Or DQSJ1.Text = My.Settings.TX_JG1 Then
            If My.Settings.MusicPath1 = "" Then
                My.Computer.Audio.Play(Application.StartupPath & "/Resources/Background1.wav")
            Else
                My.Computer.Audio.Play(My.Settings.MusicPath1)
            End If
            If My.Settings.TX_XX1 = "" Then
                MsgBox("提醒信息:" & "喂喂,时间到了!该干啥干啥去。","闹钟响起")
            Else
                MsgBox("提醒信息:" & My.Settings.TX_XX1,"闹钟响起")
            End If
            Tixing1.Enabled = True
            TX_L_Show.Text = "已开启的闹钟提醒:无"
            My.Settings.TX_SJ1 = ""
            My.Settings.TX_JG1 = ""
            My.Settings.TX_XX1 = ""
        End If
    End Sub


    Private Sub DSGJ_T_SJ1_MouseDown(ByVal sender As System.Object,ByVal e As System.Windows.Forms.MouseEventArgs) Handles DSGJ_T_SJ1.MouseDown
        If DSGJ_T_SJ1.Text = "请填入完整时间" Then
            DSGJ_T_SJ1.Text = ""
            DSGJ_T_SJ1.ForeColor = Color.RoyalBlue
        End If
    End Sub
    Private Sub DSGJ_T_SJ1_TextChanged(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles DSGJ_T_SJ1.TextChanged
        If DSGJ_C_XT1.Checked = True Or DSGJ_C_XT2.Checked = True Then
            DSGJ_B_DSKQ1.Enabled = True
        Else
            DSGJ_B_DSKQ1.Enabled = False
        End If
    End Sub

    Private Sub DSGJ_C_XT1_CheckedChanged(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles DSGJ_C_XT1.CheckedChanged
        If DSGJ_C_XT1.Checked = True And DSGJ_T_SJ1.Text = "" Or DSGJ_C_XT1.Checked = True And DSGJ_T_SJ1.Text = "请填入完整时间" Then
            DSGJ_B_DSKQ1.Enabled = False
        Else
            DSGJ_B_DSKQ1.Enabled = True
        End If
    End Sub
    Private Sub DSGJ_C_XT2_CheckedChanged(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles DSGJ_C_XT2.CheckedChanged
        If DSGJ_C_XT2.Checked = True And DSGJ_T_SJ1.Text = "" Or DSGJ_C_XT2.Checked = True And DSGJ_T_SJ1.Text = "请填入完整时间" Then
            DSGJ_B_DSKQ1.Enabled = False
        Else
            DSGJ_B_DSKQ1.Enabled = True
        End If
    End Sub
    Private Sub DSGJ_B_DSKQ1_Click(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles DSGJ_B_DSKQ1.Click
        If DSGJ_T_SJ1.Text = "" Then
            MsgBox("请填入时间!","定时关机")
        Else
            My.Settings.GJ_SJ1 = DSGJ_T_SJ1.Text
            My.Settings.GJ_YC1 = DSGJ_T_YC1.Text
            If DSGJ_C_XT1.Checked = True Then
                My.Settings.GJ_XT1 = "1"
            Else
                My.Settings.GJ_XT1 = "2"
            End If
            DSGJ_L_Show.Text = "已开启的定时关机:" & DSGJ_T_SJ1.Text
            Me.Width = 315
            DSGJ1.Text = "定时关机>"
            DSGJ1.Enabled = False
            If TX_B_QX1.Enabled = False Then
                Tixing1.Enabled = True
            End If
            DSGJ_B_QX1.Enabled = True
        End If
        DSGJ_T_SJ1.Text = "请填入完整时间"
        DSGJ_T_YC1.Text = ""
    End Sub

    Private Sub DSGJ_Timer1_Tick(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles DSGJ_Timer1.Tick
        Dim systemD As String
        Dim arguments As String
        Dim returnValue As Process
        If DQSJ1.Text = My.Settings.GJ_SJ1 Then
            If My.Settings.GJ_XT1 = "1" Then
                systemD = System.Environment.SystemDirectory
                arguments = "-s -t " & My.Settings.GJ_YC1
                My.Settings.GJ_SJ1 = ""
                My.Settings.GJ_YC1 = ""
                returnValue = Process.Start(systemD & "/shutdown.exe",arguments)
            Else
                systemD = System.Environment.SystemDirectory
                arguments = "/s /t " & My.Settings.GJ_YC1
                My.Settings.GJ_SJ1 = ""
                My.Settings.GJ_YC1 = ""
                returnValue = Process.Start(systemD & "/shutdown.exe",arguments)
            End If
        End If
    End Sub


    Private Sub TX_B_SJZHQ2_Click(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles TX_B_SJZHQ2.Click
        If TX_T_XS1.Text = "" Then
            MsgBox("请输入值!","错误")
        Else
            TX_T_FZ1.Text = TX_T_XS1.Text * 60
        End If
    End Sub
    Private Sub TX_T_XS1_TextChanged(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles TX_T_XS1.TextChanged
        If TX_T_XS1.Text = "" Then
            TX_T_FZ1.Text = ""
        End If
    End Sub

    Private Sub TX_B_QX1_Click(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles TX_B_QX1.Click
        TX_L_Show.Text = "已开启的闹钟提醒:无"
        Tixing1.Enabled = True
        My.Settings.TX_SJ1 = ""
        My.Settings.TX_JG1 = ""
        My.Settings.TX_XX1 = ""
        TX_B_QX1.Enabled = False
    End Sub
    Private Sub DSGJ_B_QX1_Click(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles DSGJ_B_QX1.Click
        DSGJ_L_Show.Text = "已开启的定时关机:无"
        DSGJ1.Enabled = True
        My.Settings.GJ_SJ1 = ""
        My.Settings.GJ_XT1 = ""
        My.Settings.GJ_YC1 = ""
        DSGJ_B_QX1.Enabled = False
    End Sub
    Private Sub AboutButton1_Click(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles AboutButton1.Click
        About.Show()
    End Sub
End Class
原文链接:https://www.f2er.com/vb/261416.html

猜你在找的VB相关文章