[VB.NET]获得Windows运行时间

本实例介绍如何使用Visual Basic和GetTickCount函数获取Windows系统的运行时间,并进行时间单位转换。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

获得Windows运行时间

实例说明

在本实例中,我们将制作一个能够获得自开机以来Windows运行的时间。程序运行结果如图87-1所示。

图87-1 运行结果

技术要点

<script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>

l GetTickCount函数

l 换算时间

实现过程

■ 新建项目

打开Visual Studio.NET,选择"新建项目",在项目类型窗口中选择"Visual Basic项目",在模板窗口中选择"Windows应用程序",在名称域中输入"GetRunTime",然后选择保存路径。单击"确认"。

■ 添加控件和设置属性

向窗体上添加两个Button控件,将窗体和控件的Text属性改为和界面一致。

■ 添加代码

Private Declare Function GetTickCount Lib "kernel32" () As Integer

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim resRunTime As Long

Dim temp As Double

Dim tempHour, tempMinute, tempSecond As Integer

'获取自windows启动以来经历的时间长度(毫秒)

resRunTime = GetTickCount

'计算秒

temp = resRunTime / 1000

'计算小时

tempHour = temp / 3600

'计算分

tempMinute = (temp / 3600 - tempHour) * 60

'计算秒

tempSecond = ((temp / 3600 - tempHour) * 60 - tempMinute) * 60

MsgBox("Windows运行时间:" & tempHour & ":" & tempMinute & ":" & tempSecond)

End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

End

End Sub

■ 运行程序

单击菜单"调试|启动"或单击 图标运行程序。

小结

本实例通过调用GetTickCount()函数来得到系统运行的时间,但是使用该函数返回的是毫秒,因此需要经过转化后才能为我们所使用。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值