⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 alarm.cpp

📁 从“闹钟时”下拉框选择要闹铃的小时(0-23)
💻 CPP
字号:
//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop
#include "Alarm.h"
#include "ABOUTCLK.h"
#include "bar.h"
#include <stdlib.h>
#include <mmsystem.h>

extern PACKAGE int HInstance;
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TAlarmClock *AlarmClock;

typedef AnsiString  str;     //AnsiString类型替代为str

SYSTEMTIME nowtime;        //声明一个系统时间变量nowtime

WORD hour,minute;

//---------------------------------------------------------------------------
__fastcall TAlarmClock::TAlarmClock(TComponent* Owner)
    : TForm(Owner)
{
}
//---------------------------------------------------------------------------






void __fastcall TAlarmClock::Timer1Timer(TObject *Sender)
{
        //得到系统时间:GetSystemTime
        //设置系统时间:SetSystemTime,这两上API函数可参考SDK文档很简单。
        //关于如何比较两上两个时间值,有很多种方法,
        //你可用TDateTime数据类型,将它们相减,如果结果为0,则表明相等。
        //或化成字符串来进行比较。

        GetLocalTime(&nowtime);     //得到系统当前时间
        float t,t1,t2;                //t1为系统当前时间,t2为用户设置的时间
        t1=float(nowtime.wHour)+float(nowtime.wMinute/60.0)+
                float(nowtime.wSecond/3600.0);    //转化成浮点数的和,单位是小时
        t2=float(hour)+float(minute/60.0);
        t=int((t1-t2)*60);

        if( (t>=0) && (t<=3) && (stp==1) )
        {
                Form2->Show();
                Form2->L2->Caption=FloatToStr(hour)+"时"+FloatToStr(minute)+"分";
                Form2->ProgressBar1->Position=3-t;
                Form2->ProgressBar1->StepIt();
                if(Form2->ProgressBar1->Position==3)
                {
                        stp=0;
                        Form2->Close();
                }
                if(hour>=0 && hour<12)
                        sndPlaySound("gotup.wav", SND_SYNC);
                if(hour>=12 && hour<24)
                        sndPlaySound("wolf.wav", SND_SYNC);
        }
}
//---------------------------------------------------------------------------

void __fastcall TAlarmClock::About1Click(TObject *Sender)
{
    AboutBox->ShowModal();
}
//---------------------------------------------------------------------------


void __fastcall TAlarmClock::N5Click(TObject *Sender)
{
    Close();
}
//---------------------------------------------------------------------------
AnsiString TAlarmClock::GetFormatTime()         //把系统时间格式化输出的函数
{
        GetLocalTime(&nowtime);

        str year,month,day,hour,minute,second;

        year=IntToStr(nowtime.wYear);   //把系统时间的年转化成字符串传给year;

        if(nowtime.wMonth<10)
                month="0"+IntToStr(nowtime.wMonth);   //如果是个位前面就加个0
        else
                month=IntToStr(nowtime.wMonth);         //传给month

        if(nowtime.wDay<10)
                day="0"+IntToStr(nowtime.wDay);
        else
                day=IntToStr(nowtime.wDay);     //传给day

        if(nowtime.wHour<10)
                hour="0"+IntToStr(nowtime.wHour);
        else
                hour=IntToStr(nowtime.wHour);   //传给hour

        if(nowtime.wMinute<10)
                minute="0"+IntToStr(nowtime.wMinute);
        else
                minute=IntToStr(nowtime.wMinute);      //传给minute

        if(nowtime.wSecond<10)
                second="0"+IntToStr(nowtime.wSecond);
        else
                second=IntToStr(nowtime.wSecond);       //传给second

        //格式化返回年月日时分秒
        return(year+"年"+month+"月"+day+"日"+" "+hour+"时"+minute+"分"+second
                +"秒");
}
//--------------------------------------------------------------------------




void __fastcall TAlarmClock::FormCreate(TObject *Sender)
{
        Label2->Caption=GetFormatTime();
        stp=1;
        Label1->Caption=FloatToStr(hour)+":"+FloatToStr(minute);

        //char *wav_handle=NULL;
        //装入Wav文件(装入资源文件gotup.wav)
        //HRSRC h = FindResource(AfxGetResourceHandle(),"S1","WAV");
        //HGLOBAL h1 = LoadResource(AfxGetResourceHandle(), h);
        //wav_handle = (char *)LockResource(h1);


        /*/装入资源文件wolf.wav
        HRSRC h = FindResource(Hinstance,"S2","WAV");
        HGLOBAL h1 = LoadResource(Hinstance, h);
        wav_handle = (char *)LockResource(h1);*/
}
//---------------------------------------------------------------------------

void __fastcall TAlarmClock::Timer2Timer(TObject *Sender)
{
        Label2->Caption=GetFormatTime();
}
//---------------------------------------------------------------------------



void __fastcall TAlarmClock::Timer3Timer(TObject *Sender)
{
        Label1->Caption=FloatToStr(hour)+"时"+FloatToStr(minute)+"分";

        switch(ComboBox1->ItemIndex)
        {
                case 1: hour=0;break;
                case 2: hour=1;break;
                case 3: hour=2;break;
                case 4: hour=3;break;
                case 5: hour=4;break;
                case 6: hour=5;break;
                case 7: hour=6;break;
                case 8: hour=7;break;
                case 9: hour=8;break;
                case 10: hour=9;break;
                case 11: hour=10;break;
                case 12: hour=11;break;
                case 13: hour=12;break;
                case 14: hour=13;break;
                case 15: hour=14;break;
                case 16: hour=15;break;
                case 17: hour=16;break;
                case 18: hour=17;break;
                case 19: hour=18;break;
                case 20: hour=19;break;
                case 21: hour=20;break;
                case 22: hour=21;break;
                case 23: hour=22;break;
                case 24: hour=23;
        }

        switch(ComboBox2->ItemIndex)
        {
                case 1: minute=0;break;
                case 2: minute=5;break;
                case 3: minute=10;break;
                case 4: minute=15;break;
                case 5: minute=20;break;
                case 6: minute=25;break;
                case 7: minute=30;break;
                case 8: minute=35;break;
                case 9: minute=40;break;
                case 10: minute=45;break;
                case 11: minute=50;break;
                case 12: minute=55;
        }
}
//---------------------------------------------------------------------------


void __fastcall TAlarmClock::N2Click(TObject *Sender)
{
        Application->HelpCommand(HELP_FINDER,0);        
}
//---------------------------------------------------------------------------

void __fastcall TAlarmClock::N3Click(TObject *Sender)
{
        Application->Terminate();        
}
//---------------------------------------------------------------------------


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -