编程实现测试CPU的速度
CPU的速度随温度和电压的变化而变化,如何随时查看CPU的速度?下面我们通过编程实现。在这个过程中,要用到汇编语言的知识。
第一步:生成一个基于对话框的工程CPUSpeed。其他选项我们可以都取其默认值。
第二步:在对话框上添加一个按钮,名称为"测试CPU速度",双击此按钮生成此按钮的处理函数,OnButton1。
第三步:在CPUSpeedDlg.cpp文件中定义类Ctime,在OnButton1中添加处理代码,最后文件CPUSpeedDlg.cpp变成如下:
-
-
- #include "stdafx.h"
- #include "CPUSpeed.h"
- #include "CPUSpeedDlg.h"
-
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
-
-
-
- inline unsigned __int64 theCycleCount(void)
- {
-
- _asm _emit 0x0F
- _asm _emit 0x31
-
-
-
- }
-
- class CTimer
- {
- unsigned __int64 m_start;
-
- public:
-
- unsigned __int64 m_overhead;
-
- CTimer(void)
- {
- m_overhead = 0;
- Start();
- m_overhead = Stop();
-
- }
-
- void Start(void)
- {
- m_start = theCycleCount();
- }
-
- unsigned __int64 Stop(void)
- {
-
- return theCycleCount()-m_start-m_overhead;
- }
- };
-
- void CCPUSpeedDlg::OnButton1()
- {
-
- CString strRawClockFrequency;
-
- CTimer timer;
-
- long tickslong;
- long tickslongextra;
- timer.Start();
- Sleep(1000);
- unsigned cpuspeed100 = (unsigned)(timer.Stop()/10000);
-
-
- tickslong = cpuspeed100/100;
- tickslongextra = cpuspeed100-(tickslong*100);
- strRawClockFrequency.Format("%d.%d MHZ estimate ", tickslong,tickslongextra );
- AfxMessageBox("CPU速度为"+strRawClockFrequency);
- }
- class CAboutDlg : public CDialog
- {
- ……以下为编程环境生成时自动生成的代码。
好了,现在点击按钮"测试CPU速度"就可以弹出对话框告诉我们CPU的速度了。
程序中使用自定义的鼠标
?建立工程与一个资源档
用Image Editor编辑一个鼠游标
(Fild | New | Resource File)
新建一个 CURSOR_1 的 CURSOR, 设定好它的 Hot Spot
(Cursor | Set Hot Spot)
存档时注意要和建立的Project存在同一个目录在本例我们先假定为 MyCursor.res
二. 程序部分
定义一个常数crMyCursor, 这个常数您必须设成大於零的任何整数, 以 LoadCursor() 函数将自订的鼠标资源 load 进来, 以下为源代码:
-
- unit Unit1;
- interface
- uses
- SysUtils, WinTypes, WinProcs, Messages, Classes,
- Graphics, Controls, Forms, Dialogs;
-
- const crMyCursor = 1; (* 宣告一个常数 *)
- type
- TForm1 = class(TForm)
- procedure FormCreate(Sender: TObject);
- private
- { Private declarations }
- public
- { Public declarations }
- end;
- var
-
- Form1: TForm1;
- {$R mycursor.res}
- implementation
- {$R *.DFM}
-
- procedure TForm1.FormCreate(Sender: TObject);
- begin
-
- Screen.Cursors[crMyCursor] := LoadCursor
- (hInstance,CURSOR_1);
- Cursor := crMyCursor;
- Button1.Cursor := crMyCursor;
- end;
- end.
(wiseman) |