带爆炸效果的扫雷(源码4)

本文介绍了一个自定义计时器类的实现细节,包括如何启动、暂停、继续及停止计时器,并提供了绘图功能以显示计时结果。此外还涉及了计时器的状态管理和资源加载。

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

 计时器:由于需要暂停/恢复计时、绘图,所以专门作了一个类。 其中用了三个状态#define T_CLOSE 0
#define T_START 1
#define T_PAUSE 2

应该可以优化。

附 mytimer.h  mytimer.cpp

// 定时器
#ifndef __MYTIMER_H
#define  __MYTIMER_H

#define  T_CLOSE 0
#define  T_START 1
#define  T_PAUSE 2

class  MYTIMER
{
public:
    MYTIMER();
    
~MYTIMER();

    
void StartTimer();
    
void PauseTimer();
    
void StopTimer();
    
void GoOnTimer();
    
int GetTimeLong();
    
void TimerInc();
    
void Clear();
    
    
void Draw();
    
void LoadPic();
    
void SetHINSTANCE(HINSTANCE h);
    
void SetHWND(HWND h);
    
void SetHDC(HDC h);

    
void SetPos(int x,int y);

private:
    
int time_long;
    
int iState;
    POINT pos;

    HDC hdcmem;
    HDC hdc;
    HWND hwnd;
    HINSTANCE hinstance;

    HBITMAP hbm_number;
}
;

#endif

mytimer.cpp


#include 
" stdafx.h "
#include 
" resource.h "

#include 
" mytimer.h "

void  MYTIMER::StartTimer()
{
    
if(T_START==iState)
    
{
        
return;
    }


    time_long
=0;
    iState
=T_START;

    SetTimer(hwnd,
1,1000,NULL);

//    MessageBox(hwnd,"start timer","timer",MB_OK);
}


void  MYTIMER::PauseTimer()
{
    
if(T_START == iState)
    
{
        KillTimer(hwnd,
1);
        iState
=T_PAUSE;
    }

}

void  MYTIMER::StopTimer()
{
    KillTimer(hwnd,
1);

    iState
=T_CLOSE;
}


void  MYTIMER::Clear()
{
    time_long
=0;
}


int  MYTIMER::GetTimeLong()
{
    
return time_long;
}


void  MYTIMER::GoOnTimer()
{
    
if(T_PAUSE == iState)
    
{
        iState
=T_START;
        SetTimer(hwnd,
1,1000,NULL);
    }

//    MessageBox(hwnd,"Timer GO ON","timer",MB_OK);
}


void  MYTIMER::TimerInc()
{
    
if(time_long<=999)
        time_long
++;
}


void  MYTIMER::SetHWND(HWND h)
{
    hwnd
=h;
}


void  MYTIMER::SetHINSTANCE(HINSTANCE h)
{
    hinstance
=h;
}


void  MYTIMER::LoadPic( void )
{
    
if(!hinstance)
    
{
        MessageBox(hwnd,
"Timer:Load Picture:instance is null","bitmap",MB_OK);
    }


    hbm_number
=LoadBitmap(hinstance,MAKEINTRESOURCE(IDB_TIMER_NUMBER));    

    
if(!hbm_number)
    
{
        MessageBox(hwnd,
"Timer:Load Picture:bitmap handle is null","bitmap",MB_OK);
    }



    hdc
=GetDC(hwnd);
    hdcmem
=CreateCompatibleDC(hdc);
    
if(!hdcmem)
    
{
        MessageBox(hwnd,
"Timer:Load Picture:hdcmem is null","bitmap",MB_OK);
    }


    ReleaseDC(hwnd,hdc);
}


void  MYTIMER::SetHDC(HDC h)
{
    hdc
=h;
}

void  MYTIMER::Draw()
{
    
int i=0;
    
//hdc=GetDC(hwnd);
    if(!hdc)
    
{
        MessageBox(hwnd,
"Timer:Draw:DC is null","bitmap",MB_OK);
    }


    SelectObject(hdcmem,hbm_number);

    TextOut(hdc,pos.x,pos.y,
"TIME:",5);

    i
=time_long/100;    
    i
%=10;
    BitBlt(hdc,pos.x,pos.y
+16,16,32,hdcmem,i*16,0,SRCCOPY);
    
    i
=time_long/10;    
    i
%=10;
    BitBlt(hdc,pos.x
+16,pos.y+16,16,32,hdcmem,i*16,0,SRCCOPY);

    i
=time_long%10;    
    BitBlt(hdc,pos.x
+32,pos.y+16,16,32,hdcmem,i*16,0,SRCCOPY);

}


MYTIMER::MYTIMER()
{
    hwnd
=0;
    time_long
=0;
    iState
=T_CLOSE;
}


MYTIMER::
~ MYTIMER()
{
    DeleteObject(hbm_number);

    hwnd
=0;
    time_long
=0;
    
if(iState!=T_CLOSE)
    
{
        KillTimer(hwnd,
1);
    }

}


void  MYTIMER::SetPos( int  x, int  y)
{
    pos.x
=x;
    pos.y
=y;
}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值