说三道四技术文摘-感悟人生的经典句子
说三道四 > 文档快照

一个小巧精致的CPU监视器 -VB资料

HTML文档下载 WORD文档下载 PDF文档下载
一个小巧精致的CPU监视器 -VB资料
我们在运行一些系统占用率比较高的程序时,为了避免出现资源不足而运行失败或者速度非常慢的情况,应该首先确定当前的CPU使用情况。我们一般是使用“系统工具”中的“资源状况”工具,它虽然比较全面和易用,但是自身却又消耗了较多资源,而且它的那些附属的功能都是我们所不需要的,因此,我们可以考虑自己编程实现对CPU资源状况的监视。笔者经过一番探索,开发了一个小巧精致的CPU监视器,它不仅界面友好,而且占用资源极少,确实可以取代windows的“资源状况”程序(见图1)。
  (一)编程原理;
  CPU的资源占用状况被记载在注册表的项目HKEY_DYN_DATA\PerfStats\StatData分支“KERNEL\CPUUsage"中,通过周期性的读取该键的键值,并且将参数传递到我们自己制作的监视器界面中,使之相应的变化,这就可以即时反映出CPU的使用状况了。本文程序中的关键是相关键值的读取和反映输出到监视器上。
  (二)编程实践;
  (1)设计程序界面;
  为了达到较小CPU占有率,必须将应用程序设计的既能够全面反映问题,又能够尽量的小。首先建立一个标准exe工程,将窗体命名为frmmain,borderstyle属性为3-fixed,caption属性为空白,添加一个frame控件“frame1”,caption属性为空白,添加picturebox控件,前景色为绿色,背景色为浅红色,添加一个定时器控件“timer”,interval设置为100,enabled为true,其他的属性采用系统的默认值即可,调整窗体和控件到适当位置和适当大小,效果如图2所示即可。
  双击窗体frmmain,写入以下代码:
  Option Explicit
  Private Const LP-HT-CAPTION = 2
  Private Const WM-NCLBUTTONDOWN = &HA1
  Private Declare Function ReleaseCapture Lib “user32" () As Long
  Private Declare Function SendMessage Lib “user32" Alias“SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
  Private CPU As New CPUUsage
  Private Avg As Long
  Private Sum As Long
  Private Index As Long
  Private Sub Form_Load()
  Me.Caption = App.Title ‘初始化应用程序标题栏
  CPU.InitCPUUsage
  End Sub
  Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer) ‘程序结束前关闭定时器和释放内存
  Timer.Enabled = False
  Set CPU = Nothing
  Cancel = 0
  End Sub
  Private Sub Frame1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single) ‘用左键可以拖动程序
  Call Form_MouseDown(Button, Shift, X, Y)
  End Sub
  Private Sub pctPrg_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single) ‘右键单击退出程序
  If Button = 2 Then
  Unload Me
  End
  End If
  Call Form_MouseDown(Button, Shift, X, Y)
  End Sub
  Private Sub Timer_Timer() ‘利用定时器周期性的读取资源状况
  Dim tmp As Long
  tmp = CPU.GetCPUUsage
  Me.Caption = App.Title & FormatPercent(tmp / 100) ‘
  ‘在标题栏用百分比数字形式反映占用率
  Sum = Sum + tmp
  Index = Index + 1
  Avg = Int(Sum / Index)
  pctPrg.Cls
  pctPrg.Line (0, 0)-(tmp, 18), , BF
  pctPrg.Line (Avg, 0)-(Avg, 18), &HFF
  pctPrg.Line (Avg + 1, 0)-(Avg + 1, 18), &HFF
  ‘用图式形式反映占用率
  End Sub
  Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single) ‘使程序可以被拖动
  Dim rc As Long
  rc = ReleaseCapture
  rc = SendMessage(hwnd, WM-NCLBUTTONDOWN, LP_HT-CAPTION, ByVal 0&)
  End Sub
  (2)编写读取注册表中CPU使用状况的类模块;
  添加一个类模块,命名为“CPUusage”,写入以下代码:
  Option Explicit
  Private Declare Function RegQueryValueEx Lib “advapi32.dll" Alias “RegQueryValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal lpReserved As Long, lpType As Long, lpData As Any, lpcbData As Long) As Long
  Private Declare Function RegOpenKey Lib“advapi32.dll" Alias “RegOpenKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long
  Private Declare Function RegCloseKey Lib“advapi32.dll" (ByVal hKey As Long) As Long
  Private Const REG_DWORD = 4
  Private Const HKEY_DYN_DATA = &H80000006
  Public Sub InitCPUUsage()
  Dim Data As Long, Typ As Long, Size As Long
  Dim hKey As Long, hRet As Long
  hRet = RegOpenKey(HKEY-DYN-DATA, “PerfStats\StartStat", hKey)
  hRet = Reg QueryValueEx(hKey, “KERNEL\CPUUsage", 0, REG-DWORD, Data, 4)
  hRet = RegCloseKey(hKey)
  End Sub
  Public Function GetCPUUsage() As Long
  On Error GoTo eee ‘如果注册表错误的话转到eee:
  Dim Data As Long, Typ As Long, Size As Long
  Dim hKey As Long
  Dim hRet As Long
  hRet = RegOpenKey(HKEY-DYN-DATA,“PerfStats\StatData", hKey) ‘打开主键
  hRet = RegQueryValueEx(hKey, “KERNEL\CPUUsage", 0&, REG_DWORD, Data, 4) ‘读取参数
  GetCPUUsage = Data
  hRet = RegCloseKey(hKey)
  Exit Function
  eee:
  MsgBox “注册表参数读取出现了错误!", vbExclamation
  End Function
  好了,一个装备精良的CPU监视器制造完毕,寥寥几行代码确可以完成一项大工作。另外,使用vb5的朋友注意了,本文主窗体中定时器运行过程函数中的FormatPercent函数只能在vb6中使用,它的作用是将一个表达式转化成百分数,你必须采用其他的变通方法,其他代码全部通用。
  对于国内的程序员来说,涉及windows高级编程的相关资料和示例代码非常少,这在一定程度上限制了程序员的开发高级应用程序能力。本文中的示例不仅仅涉及了注册表的读取,而且演示了图示的技巧,希望诸位编程高手能够有所借鉴和斧正。
  (河南 新天)
备案号:鲁ICP备13029499号-2 说三道四 www.s3d4.cn 说三道四技术文摘