返回首页

如何显示时间,日期,大写和NumLock的状态在2008年在vb.net中的状态栏?

回答

评论会员:Slacker007 时间:2012/02/07
创建个人状态条项目,然后分配文本,并通过这些项目的代码值
评论会员:DaveAuld 时间:2012/02/07
参见下面的演示,让你去。你从来没有说过你这点净版本使用,信息上看到Tick事件。NET 3.5及以上

Public Class Form1

 

    Public Declare Function GetKeyState Lib "user32" (ByVal nVirtKey As Long) As Integer

 

    Private Sub Form1_Load(sender As Object, e As System.EventArgs) Handles Me.Load

 

        'Create a StatusBar

        Dim statusBarMain As New StatusBar

 

        statusBarMain.Name = "StatusBar"

        statusBarMain.ShowPanels = True

 

        'Create the panels

        Dim statusBarDate = New StatusBarPanel

        statusBarDate.Name = "StatusBarDate"

        statusBarDate.Text = FormatDateTime(Now(), DateFormat.ShortDate)

        statusBarDate.AutoSize = StatusBarPanelAutoSize.Contents

        statusBarMain.Panels.Add(statusBarDate)

 

        Dim statusBarTime = New StatusBarPanel

        statusBarTime.Name = "StatusBarTime"

        statusBarTime.Text = FormatDateTime(Now(), DateFormat.LongTime)

        statusBarTime.AutoSize = StatusBarPanelAutoSize.Contents

        statusBarMain.Panels.Add(statusBarTime)

 

        Dim statusBarCAPS = New StatusBarPanel

        statusBarCAPS.Name = "StatusBarCAPS"

 

        If GetKeyState(Keys.CapsLock) = 1 Then

            statusBarCAPS.Text = "CAPS ON"

        Else

            statusBarCAPS.Text = "CAPS OFF"

        End If

 

        statusBarCAPS.AutoSize = StatusBarPanelAutoSize.Contents

        statusBarMain.Panels.Add(statusBarCAPS)

 

        Dim statusBarNUMS = New StatusBarPanel

        statusBarNUMS.Name = "StatusBarNUMS"

 

        If GetKeyState(Keys.NumLock) = 1 Then

            statusBarNUMS.Text = "NumLock ON"

        Else

            statusBarNUMS.Text = "NumLock OFF"

        End If

 

        statusBarNUMS.AutoSize = StatusBarPanelAutoSize.Contents

        statusBarMain.Panels.Add(statusBarNUMS)

 

        'Add all teh controls to the form

        Me.Controls.Add(statusBarMain)

 

        'Set up a refresh timer

        Dim timer As New Timer

        timer.Interval = 1000

        timer.Start()

        AddHandler timer.Tick, AddressOf timer_Tick

 



    End Sub

 

        Private Sub timer_Tick()

 

        Dim status As StatusBar = CType(Me.Controls.Find("statusBar", True)(0), StatusBar)

 

        status.Panels("statusBarDate").Text = FormatDateTime(Now(), DateFormat.ShortDate)

        status.Panels("statusBarTime").Text = FormatDateTime(Now(), DateFormat.LongTime)

 

        'Rather than set up message listeners etc, are you really going to notice much if the caps lock/nums lock is

        'upto 1 seconds out from true state?? doubt it much. (you decrease the timer tick of course.

        If GetKeyState(Keys.NumLock) = 1 Then

            status.Panels("statusBarNUMS").Text = "NumLock ON"

        Else

            status.Panels("statusBarNUMS").Text = "NumLock OFF"

        End If

 

        If GetKeyState(Keys.CapsLock) = 1 Then

            status.Panels("statusBarCAPS").Text = "CAPS ON"

        Else

            status.Panels("statusBarCAPS").Text = "CAPS OFF"

        End If

 

        'NOTE: IN .Net Version 3.5 and above you can use the;

        '        My.Computer.Keyboard.CapsLock

        '        My.Computer.Keyboard.NumLock

        ' to read the status without an unmanaged call



        End Sub

End Class