timer.cpp

来自「一个小小的计时器游戏.」· C++ 代码 · 共 83 行

CPP
83 行
字号
// Timer.cpp : Defines the entry point for the application.
//

#include "stdafx.h"
#include <string.h>
#include "Timer.h"

UINT	Counter = 0;
HINSTANCE hInst;

int APIENTRY WinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR     lpCmdLine,
                     int       nCmdShow)
{
	hInst = hInstance;

	DialogBox(hInstance, (LPCTSTR)"TimerDlgBox", NULL, (DLGPROC)TimerDlgProc);
	return(FALSE);
}

BOOL APIENTRY TimerDlgProc(HWND hDlg,UINT message,WPARAM wParam,LPARAM lParam)
{
	static BOOL fShowSun = TRUE;
	static BOOL fStartTimer = TRUE;
	static HWND hSun,hMoon;

	switch (message)
	{
	case WM_INITDIALOG:
		hSun = GetDlgItem(hDlg,IDC_SUN);
		hMoon = GetDlgItem(hDlg,IDC_MOON);
		SetTimer(hDlg, SUNTIMER, 1000, (TIMERPROC)NULL);
		return (TRUE);

	case WM_TIMER:
		if (fShowSun)
		{
			ShowWindow(hMoon,SW_HIDE);
			ShowWindow(hSun,SW_SHOW);
		}
		else
		{
			ShowWindow(hSun,SW_HIDE);
			ShowWindow(hMoon,SW_SHOW);
		}
		fShowSun = !fShowSun;
		break;

	case WM_COMMAND:
		switch(LOWORD(wParam))
		{
		case IDC_STARTTIMER:
			if(fStartTimer)
			{
				SetDlgItemText(hDlg,IDC_STARTTIMER,"停止下面的计时器");
				SetTimer(hDlg,MYTIMER,300,(TIMERPROC)MyTimerProc);
			}
			else
			{
				KillTimer(hDlg,MYTIMER);
				SetDlgItemText(hDlg,IDC_STARTTIMER,"启动下面的计时器");
				Counter = 0;
			}
			fStartTimer = !fStartTimer;
			break;

		case IDCANCEL:
			KillTimer(hDlg,SUNTIMER);
			if(!fStartTimer)
				KillTimer(hDlg,MYTIMER);
			EndDialog(hDlg,TRUE);
			return (TRUE);
		}
		break;
	}
	return (FALSE);
}

VOID CALLBACK MyTimerProc(HWND hWnd, UINT message,UINT idTimer, DWORD dwTime)
{
	SetDlgItemInt(hWnd, IDC_TIMECOUNTER, ++Counter, FALSE);
}

⌨️ 快捷键说明

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