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

📄 timer.cpp

📁 闹钟的c源程序
💻 CPP
字号:
#include "Timer.h"
#include "mmsystem.h"
#pragma comment(lib,"winmm.lib")
#define ID_TIMER_SYS 1
#define ID_TIMER_EVENT 2
//===================================================================================
static TCHAR szAppName[] = TEXT ("TIMER") ;
HWND hMainDlg, hChildDlg;
HINSTANCE hInstance;
TCHAR szTime[256];
int  ihour, imin, isecond;//定义响铃的小时和分钟
int iItem = 0; //记录数
BOOL bModify = FALSE;
EVENTLIST L;
//===================================================================================
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
                    PSTR szCmdLine, int iCmdShow)
{
	MSG          msg ;
	WNDCLASS     wndclass ;
	
    InitCommonControls();
	
	wndclass.style         = CS_HREDRAW | CS_VREDRAW ;
	wndclass.lpfnWndProc   = WndProc;
	wndclass.cbClsExtra    = 0 ;
	wndclass.cbWndExtra    = DLGWINDOWEXTRA ;
	wndclass.hInstance     = hInstance ;
	wndclass.hIcon         = LoadIcon (hInstance, szAppName);
	wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
	wndclass.hbrBackground = (HBRUSH) (COLOR_BTNFACE+1) ;
	wndclass.lpszMenuName  = szAppName ;
	wndclass.lpszClassName = szAppName ;
	
	if (!RegisterClass (&wndclass))
	{
		MessageBox (NULL, TEXT ("This program requires Windows NT!"), 
			szAppName, MB_ICONERROR) ;
		return 0 ;
	}
	
	hMainDlg = CreateDialog(hInstance,szAppName,NULL,(DLGPROC)WndProc);
	ShowWindow (hMainDlg, iCmdShow) ;
	UpdateWindow (hMainDlg) ;	
	while (GetMessage (&msg, NULL, 0, 0) )
	{
		if (!IsWindow(hMainDlg) || !IsDialogMessage(hMainDlg, &msg))
		{
			
			TranslateMessage (&msg) ;
			DispatchMessage (&msg) ;
		}
	}
	return msg.wParam ;
}
//===========================================================================================
LRESULT APIENTRY WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    static HINSTANCE hInstance;	
	LPEVENTNODE temp1 = NULL, temp2 = NULL;
	switch (message)
	{
	case  WM_CREATE:
		hInstance = ((LPCREATESTRUCT) lParam)->hInstance ;//初始化获得程序实例句柄		
		InitList(L);
        //设定一个程序记时器。使windows每隔1/10秒向程序发一个WM_TIMER消息,由函数TimeEventProc处理
	    SetTimer(hwnd,ID_TIMER_EVENT, 1000, TimeEventProc);
		return 0;
		
	case WM_INITDIALOG:
	    InitListViewColumns(GetDlgItem(hwnd,IDC_LISTVIEW), 0, 110, "时间");
		InitListViewColumns(GetDlgItem(hwnd,IDC_LISTVIEW), 1, 250, "内容");
		ListView_SetExtendedListViewStyle(GetDlgItem(hwnd,IDC_LISTVIEW),LVS_EX_FULLROWSELECT);
		return 0;

	case WM_COMMAND:
		switch(LOWORD(wParam))
		{
		case IDC_BTNEXIT :		
	    	DestroyWindow(hwnd);
			break;
		case IDC_BTNADD:
			bModify = FALSE;
		    DialogBox(hInstance,"SETTING",hMainDlg,lpDialogFunc);
			break;
		case IDC_BTNDEL:
			DelItem(GetDlgItem(hwnd,IDC_LISTVIEW), 
				    GetSelectItem(GetDlgItem(hwnd,IDC_LISTVIEW)), L);
			iItem -- ;
			break;			
		}
		 return 0;
		 
	case WM_NOTIFY:
			switch (((LPNMHDR) lParam)->code)
			{
			case NM_DBLCLK:
				if((L.size != 0) && (-1 != GetSelectItem(GetDlgItem(hMainDlg, IDC_LISTVIEW))) )
				{ 
					bModify = TRUE;				
					DialogBox(hInstance,"SETTING",hMainDlg,lpDialogFunc);
				}
				break;
			}
			break;
			
	case WM_DESTROY:
		CleanList(L);
		KillTimer(hwnd, ID_TIMER_EVENT);
		PostQuitMessage (0) ;
		return 0 ;
	}
	return DefWindowProc (hwnd, message, wParam, lParam) ;
}

//------------------------------------------------------------------------------------------
BOOL APIENTRY lpDialogFunc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{

	TCHAR szEditContent[256]; //响铃事件内容 
	LPEVENTNODE  pAdd= NULL, pMod = NULL;
	switch(message)
	{
	case WM_INITDIALOG :
		hChildDlg = hDlg;
		int ci;
		for(ci=0;ci<24;ci++)//用循环语句将24个小时加入到combo box中。
		{ 
			if(ci<=9)
				wsprintf(szTime,"0%d",ci);
			else
				wsprintf(szTime,"%d",ci);
			SendMessage(GetDlgItem(hChildDlg,IDC_COMBO_HOUR),CB_ADDSTRING,0,LPARAM(szTime));//向combo box发送CB_ADDSTRING消息加入字串。
		}
		for(ci=0;ci<60;ci++)//用循环语句将60分钟加入到combo box中。
		{
			if(ci<=9)
				wsprintf(szTime,"0%d",ci);
			else
				wsprintf(szTime,"%d",ci);
			SendMessage(GetDlgItem(hChildDlg,IDC_COMBO_SECOND),CB_ADDSTRING,0,LPARAM(szTime));//同上
		}
		for(ci=0;ci<60;ci++)//用循环语句将60秒加入到combo box中。
		{
			if(ci<=9)
				wsprintf(szTime,"0%d",ci);
			else
				wsprintf(szTime,"%d",ci);
			SendMessage(GetDlgItem(hChildDlg,IDC_COMBO_MIN),CB_ADDSTRING,0,LPARAM(szTime));//同上
		}
		
		if( TRUE == bModify)
		{
			MordifySetting(hMainDlg, hDlg);
			SetDlgItemText(hDlg, IDOK, "修改");

		}
		else
			SetDlgItemText(hDlg, IDOK, "确定");
		return TRUE;
		
	case WM_COMMAND :
		switch (LOWORD (wParam))
		{
		case IDC_COMBO_HOUR:
			if(HIWORD(wParam)==LBN_SELCHANGE)//当用户改变了小时combo box的选项。
				ihour = SendMessage((HWND)lParam,CB_GETCURSEL,0,0); //将小时存储到变量hour中
			return 0;

		case IDC_COMBO_MIN://当用户改变了分钟combo box的选项后,将关机分钟存储到变量minute中
			if(HIWORD(wParam)==LBN_SELCHANGE)
				imin = (WORD)SendMessage((HWND)lParam,CB_GETCURSEL,0,0) ;
			return 0;
		case IDC_COMBO_SECOND:
			if(HIWORD(wParam)==LBN_SELCHANGE)
				isecond = (WORD)SendMessage((HWND)lParam,CB_GETCURSEL,0,0) ;
			return 0;

		case IDOK :
			lstrcpy(szEditContent, "");
			GetDlgItemText(hChildDlg,IDC_EDIT , szEditContent, sizeof(szEditContent)) ;

			if(lstrcmp(szEditContent, "") == 0 )
			{
				MessageBox(hChildDlg,"请输入事件?", "" ,16);
				break;
			}
			wsprintf(szTime, "%d时%d分%d秒", ihour, imin, isecond );

            if(FALSE == bModify)//添加新信息
			{
				AddItem(GetDlgItem(hMainDlg,IDC_LISTVIEW), iItem, 0, szTime);
			    AddItem(GetDlgItem(hMainDlg,IDC_LISTVIEW), iItem,1,szEditContent);
                iItem++;

				pAdd = NULL;
       			CreateNode(pAdd,ihour, imin, isecond, szEditContent);//创建事件结点
	    		AddListNode(L, pAdd);//添加到链表上
			}
			else//俢改原信息
			{ 
				pAdd = NULL;
				pAdd = GetMordifyNode(hMainDlg,hChildDlg,L);
				MordifyNode(pAdd,ihour,imin,isecond,szEditContent);
				ListView_SetItemText(GetDlgItem(hMainDlg,IDC_LISTVIEW), GetSelectItem(GetDlgItem(hMainDlg,IDC_LISTVIEW)),
				                0, szTime);
				ListView_SetItemText(GetDlgItem(hMainDlg,IDC_LISTVIEW), GetSelectItem(GetDlgItem(hMainDlg,IDC_LISTVIEW)),
									 1, szEditContent);
			}

			ihour = 0;
			imin = 0;
			isecond = 0;

			EndDialog (hDlg, 0) ;
			return 0;

		case IDCANCEL :
			EndDialog (hDlg, 0) ;
			return 0 ;
		}
		break ;
	case WM_DESTROY:
		EndDialog(hDlg,0);
		break;
	}
	return FALSE ;
	
}
//---------------------------------------------------------------------------------------------
VOID APIENTRY TimeEventProc(HWND hwnd, UINT message,UINT iTimerID, DWORD dwTime )
{
		SYSTEMTIME st;//定义一个时间结构用来储存GetLocalTime函数得到的时间
		GetLocalTime(&st);//获得系统时间
		GetTimeFormat(NULL,TIME_FORCE24HOURFORMAT,&st,"HH':'mm':'ss",
			szTime+wsprintf(szTime," %d-%d-%d  ",st.wYear,st.wMonth,st.wDay),50);//将时间转换为标准显示格式存储到time数组中
		SetWindowText(GetDlgItem(hwnd,IDC_STATIC_SYSTIME),szTime);//将时间显示到静态框中
        LPEVENTNODE temp = NULL;
		HMODULE pInst = NULL;
		LPSTR lpWav;
		HRSRC hRsr = FindResource(NULL, "#103", "WAVE");
		HGLOBAL hGol = LoadResource(pInst, hRsr);
        lpWav =(LPSTR) LockResource(hGol);	
		if( L.size != 0 )
		{
			temp = L.head;
			while (NULL != temp)
			{
				if( (temp->iHour == st.wHour) && (temp->iMin == st.wMinute) && 
					(temp->iSecond == st.wSecond))
				{				
					PlaySound((char *)lpWav, NULL, SND_ASYNC | SND_MEMORY);			
					MessageBox(NULL, temp->szConten, "Warning", MB_OK);
				}
				temp = temp->next;
			}
		}	  
}
//=============================================================================================
/*BOOL PlayResource(LPSTR lpName) 
{ 
    BOOL bRtn; 
    LPSTR lpRes; 
    HANDLE hResInfo, hRes; 
	
    // Find the WAVE resource. 
	
    hResInfo = FindResource(hInst, lpName, "WAVE"); 
    if (hResInfo == NULL) 
        return FALSE; 
	
    // Load the WAVE resource. 
	
    hRes = LoadResource(hInst, hResInfo); 
    if (hRes == NULL) 
        return FALSE; 
	
    // Lock the WAVE resource and play it. 
	
    lpRes = LockResource(hRes); 
    if (lpRes != NULL) { 
        bRtn = sndPlaySound(lpRes, SND_MEMORY | SND_SYNC | 
            SND_NODEFAULT); 
        UnlockResource(hRes); 
    } 
    else 
        bRtn = 0; 
	
    // Free the WAVE resource and return success or failure. 
	
    FreeResource(hRes); 
    return bRtn; 
} 


*/






































⌨️ 快捷键说明

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