当前位置: 首页 > 学习 > 电脑学习 > 程序设计 > C++ > 系统控制 > 正文

一个使用多媒体定时器的简单例子

http://www.zk168.com.cn  招考学习网 2006-4-11 6:09:16
-----------------------------------------------------------[交流]-[打印]-[发送]-[收藏]--
1)新建一个工程,保存

2)添加一个Button和一个Label

3)修改unit1.h代码如下:
// ---------------------------------------------------------------------------
#ifndef Unit1H
#define Unit1H
// ---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
// ---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published: // IDE-managed Components
    TButton *Button1;
    TLabel *Label1;
    void __fastcall Button1Click(TObject *Sender);
    void __fastcall FormCloseQuery(TObject *Sender, bool &CanClose);
private: // User declarations
public:  // User declarations
    __fastcall TForm1(TComponent* Owner);
    static void CALLBACK TimeProc(UINT uID,UINT uMsg,
        DWORD dwUser,DWORD dw1,DWORD dw2); // 定时器回调函数
    int TimerID; // 定时器ID
};
// ---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
// ---------------------------------------------------------------------------
#endif

4)unit1.cpp代码如下:
// ---------------------------------------------------------------------------

#include <vcl.h>
#include "mmsystem.h"
#pragma hdrstop
#include "Unit1.h"
// ---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
// ---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
    : TForm(Owner)
{
    TimerID = 0;
}
// ---------------------------------------------------------------------------
void CALLBACK TForm1::TimeProc(UINT uID,UINT uMsg, DWORD dwUser,DWORD dw1,DWORD dw2)
{
    Form1->Label1->Caption = Now();
}
// ---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
    TimerID = timeSetEvent(1000, 0, (LPTIMECALLBACK)TimeProc, 0,
        TIME_PERIODIC|TIME_CALLBACK_FUNCTION); // 设定多媒体定时器,1000ms
    if(TimerID == 0)    {
        ShowMessage("创建失败");
    }
}
// ---------------------------------------------------------------------------
void __fastcall TForm1::FormCloseQuery(TObject *Sender, bool &CanClose)
{
    if(TimerID != 0)    {
        timeKillEvent(TimerID); // 释放定时器
    }
}
// ---------------------------------------------------------------------------

运行效果:点击按钮后,Label1开始显示时间
测试环境:BCB5 + WIN98

作者信息:
"Mao Yanwei(citiz)" <myw_fool@citiz.net>

-----------------------------------------------------------[交流]-[打印]-[发送]-[收藏]--
最新入库:
 
·实质、过程及意义——阿多尔诺“否定的辩证法”探微
·从Ontology的译名之争看哲学术语的翻译原则
·论马克思主义哲学经典的解释——解释学方法及其在马克
·中国哲学当前的核心与周边问题
·和合学与21世纪文化价值和科技
·中国文化的和合精神与21世纪
·宗教之间理当相互宽容
·上半个世纪的自由主义
·殷周至春秋时期神人关系之演进
·大学之道:构建以“三纲八目”为核心的道德修养体系
相关内容:
 
网友点评:
 
会员名称:
密码:匿名 ·注册·忘记密码?
评论内容:
(最多300个字符)
  查看评论
友情提醒:
 1.库中的资料大都来自互联网、网友上传、各类书籍,在录入的过程中难免会出现错误,恳请网
 友来信指正!
 2.如果网友在本库中未能找到所需要的材料,请登陆到我们的论坛《招考学习网》版块!
 3.考友想加入招考学习网的编辑部,请发信到XueXiWang#Gmail.com(#改为@)附带个人简历
 4.如需转载请注明出处及作者,谢谢合作!
 5.如果您有更好的建议或意见请EMAIL:XueXiWang#Gmail.com (#改为@)
 6.凡标题中有注有“[NO]”字样均不含答案且答案整理中.
 7.如本库中转载文章涉及版权等问题,请相关网站或作者在两周内发邮件通知(EMAIL:  XueXiWang#Gmail.com (#改为@))我们,我们接到通知后立即删除该文章及链接!
你问我答 更多>>