定时提醒小程序

来源:互联网 发布:英语软件哪个好 编辑:程序博客网 时间:2024/04/30 04:50

源代码:http://download.csdn.net/detail/wangqiulin123456/4907291

功能点:

  1. 定时消息提醒
  2. 定时音乐提醒
  3. 定时关机
  4. 获取开机时间

截图:



源代码:

[cpp] view plain copy
 print?
  1. #include <windows.h>  
  2. #include <WindowsX.h>  
  3. #include <CommCtrl.h>  
  4. #include "resource.h"  
  5.   
  6. #include "mmsystem.h"   
  7. #pragma comment(lib,"winmm.lib")  
  8.   
  9. #define Method_AlertMessage  50000  
  10. #define Method_ShutDown      50001  
  11. #define Method_AlertMusic1   50002  
  12. #define Method_AlertMusic2   50003  
  13.   
  14. #define IDT_TIME1            101  
  15.   
  16. // Data  
  17. HICON g_hIconLarge;  
  18. HICON g_hIconSmall;  
  19. BSTR g_strStarTime;   //get Start Computer Time  
  20. BSTR g_strSpaceTime;  // set the alert Time  
  21. BSTR g_strAlertMessage; //the alert message variable  
  22. NOTIFYICONDATA g_pnid;  //the notify icon data variable  
  23. HWND g_hWndBegin, g_hWndStop, g_hWndSet,g_hWndMessage;          // take turns the HWND begin, stop, set, Dialog  
  24. HINSTANCE g_hInstance;   // the instance of the Main Windows  
  25. HWND g_hWndMain;         // the HWND of the Main Windows  
  26. int g_iSpaceTime,g_iSpaceTimeForTimer,g_iAlertMethod;    //take turns is the space time, space time for Timer, alert method  
  27. HWND hWndSetSpTime,hWndAlertMetod;   // the the set Dialog two combobox HWND  
  28.   
  29.   
  30.   
  31. LRESULT CALLBACK DialogProc(HWND ,UINT,WPARAM,LPARAM) ;  
  32. BOOL    CALLBACK SetDlgProc(HWND ,UINT,WPARAM,LPARAM);  
  33. void OnInitDialog(HWND hDlg);  
  34. void WMSet(HWND hDlg);    //设置  
  35. void WMBegin(HWND hDlg);   //开始  
  36. void WMStop(HWND hDlg);    //停止  
  37. void ToOurTray(HWND hDlg);  //托盘最小化  
  38. void DeleteOurTray(HWND hDlg); //删除托盘最小化  
  39. void ShutDown(HWND hDlg);      //关机  
  40. void AlertMessageFunc(int );    //消息提醒  
  41.   
  42.   
  43. int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)         
  44. {   
  45.     g_hIconLarge = static_cast<HICON>(LoadImage(hInstance, TEXT("IDI_ICON1"), IMAGE_ICON,  //set large ico  
  46.         GetSystemMetrics(SM_CXICON), GetSystemMetrics(SM_CXICON), 0));  
  47.     g_hIconSmall = static_cast<HICON>(LoadImage(hInstance, TEXT("IDI_ICON1"), IMAGE_ICON,   //set small ico  
  48.         GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CXSMICON), 0));  
  49.     g_hInstance = hInstance;  
  50.   
  51.     MSG msg;  
  52.   
  53.     HWND hwnd = CreateDialog(hInstance, MAKEINTRESOURCE(IDD_DIALOG1), NULL, (DLGPROC)DialogProc);             
  54.     g_hWndMain = hwnd;  
  55.     ShowWindow(hwnd, SW_SHOW);         
  56.     UpdateWindow(hwnd);             
  57.   
  58.     while(GetMessage(&msg, NULL, 0, 0))         
  59.     {   
  60.         if( !IsDialogMessage( hwnd, &msg ) )  
  61.         {  
  62.             TranslateMessage(&msg);  
  63.             DispatchMessage(&msg);    
  64.         }  
  65.     }      
  66.   
  67.     // Exit  
  68.     DestroyIcon(g_hIconLarge);  
  69.     DestroyIcon(g_hIconSmall);    
  70.     return msg.wParam;      
  71. }  
  72.   
  73. LRESULT CALLBACK DialogProc(HWND hWnd,UINT uMsg,WPARAM wParam,LPARAM lParam)         
  74. {    
  75.     HWND hTrayWnd ;  
  76.     HMENU hMenu;  
  77.     int iHours, iMin, iSec;  
  78.     switch(uMsg)      
  79.     {      
  80.     case WM_INITDIALOG:    
  81.         OnInitDialog(hWnd);  
  82.         SetWindowPos(hWnd,NULL,500,200,0,0,SWP_NOSIZE);   
  83.         break;   
  84.     case WM_MY_NOTIFYICON:  
  85.         {  
  86.             switch(lParam)  
  87.             {  
  88.             case WM_RBUTTONUP:  
  89.                 MessageBox(hWnd,TEXT("Reight Button up"),TEXT("Message"),MB_OK);  
  90.                 //hMenu = CreateMenu();  
  91.                 //AppendMenu(hMenu,  
  92.                 break;  
  93.             case WM_LBUTTONDBLCLK:  
  94.                 ShowWindow(hWnd,SW_SHOWNORMAL);  
  95.                 SetWindowLong(hWnd,GWL_EXSTYLE, WS_EX_APPWINDOW );//隐藏任务拦按钮   
  96.                 hTrayWnd = FindWindow(TEXT("Shell_TrayWnd"),NULL);  
  97.                 if(::IsWindow(hTrayWnd))  
  98.                 {  
  99.                     ShowWindow(hTrayWnd,SW_HIDE);  
  100.                     ShowWindow(hTrayWnd,SW_SHOW);  
  101.                 }  
  102.                 //DeleteOurTray(hWnd);  
  103.                 break;  
  104.             default:  
  105.                 break;  
  106.             }  
  107.         }  
  108.         break;  
  109.     case WM_PAINT:  
  110.   
  111.         break;  
  112.     case WM_TIMER:  
  113.         {  
  114.             if (wParam == IDT_TIME1)  
  115.             {  
  116.                 if(g_iSpaceTimeForTimer--)  
  117.                 {  
  118.                     iHours = g_iSpaceTimeForTimer/3600;  
  119.                     iMin   = (g_iSpaceTimeForTimer - 3600*iHours)/60;  
  120.                     iSec   = (g_iSpaceTimeForTimer - 3600*iHours - 60*iMin);  
  121.   
  122.                     wsprintf(g_strSpaceTime,TEXT("%02d:%02d:%02d"),iHours,iMin,iSec);  
  123.                     // set default Value  
  124.                     SetDlgItemText(g_hWndMain,IDC_REVERSETIME,g_strSpaceTime);  
  125.                 }else{  
  126.                     KillTimer(hWnd,IDT_TIME1);  
  127.                     g_iSpaceTimeForTimer = g_iSpaceTime*60;  
  128.                     EnableWindow(g_hWndBegin,TRUE);  
  129.                     EnableWindow(g_hWndSet,TRUE);  
  130.                     EnableWindow(g_hWndMessage,TRUE);  
  131.                     EnableWindow(g_hWndStop,FALSE);  
  132.                     AlertMessageFunc(g_iAlertMethod);  
  133.                 }  
  134.                   
  135.             }  
  136.         }  
  137.         break;  
  138.     case WM_SYSCOMMAND:  
  139.         if (wParam == SC_MINIMIZE)  
  140.         {  
  141.             ToOurTray(hWnd);  
  142.             SetWindowLong(hWnd,GWL_EXSTYLE,WS_EX_TOOLWINDOW);//隐藏任务拦按钮   
  143.             return 0;  
  144.         }  
  145.         break;  
  146.   
  147.     case WM_COMMAND:      
  148.         switch(LOWORD(wParam))  
  149.         {  
  150.         case IDC_SET:  
  151.             WMSet(hWnd);  
  152.             break;  
  153.         case IDC_BEGIN:  
  154.             WMBegin(hWnd);  
  155.             break;  
  156.         case IDC_STOP:  
  157.             WMStop(hWnd);  
  158.             break;  
  159.         case IDCANCEL:  
  160.             PostQuitMessage(0);  
  161.             break;  
  162.         defaultbreak;  
  163.         }  
  164.         break;              
  165.   
  166.     case WM_DESTROY:   
  167.         SysFreeString(g_strSpaceTime);  
  168.         SysFreeString(g_strStarTime);  
  169.         SysFreeString(g_strAlertMessage);  
  170.         KillTimer(hWnd,IDT_TIME1);  
  171.         PostQuitMessage(0);      
  172.         break;      
  173.   
  174.     }      
  175.   
  176.     return FALSE;  
  177. }  
  178.   
  179. void OnInitDialog(HWND hDlg)  
  180. {  
  181.     SendMessage(hDlg,WM_SETICON,FALSE,reinterpret_cast<LPARAM>(g_hIconSmall));  
  182.     SendMessage(hDlg,WM_SETICON,TRUE,reinterpret_cast<LPARAM>(g_hIconLarge));  
  183.   
  184.     g_strStarTime = SysAllocString(OLESTR("00:00:00"));  
  185.     g_strSpaceTime = SysAllocString(OLESTR("00:00:00"));  
  186.     g_strAlertMessage = SysAllocStringLen(NULL,200);  
  187.   
  188.     g_hWndBegin = GetDlgItem(hDlg,IDC_BEGIN);  
  189.     g_hWndSet   = GetDlgItem(hDlg,IDC_SET);  
  190.     g_hWndStop  = GetDlgItem(hDlg,IDC_STOP);  
  191.     g_hWndMessage = GetDlgItem(hDlg,IDC_ALERTCONTENT);  
  192.   
  193.     EnableWindow(g_hWndStop,FALSE);  
  194.   
  195.     //get the Start time  
  196.     DWORD dwRunTime = GetTickCount();  
  197.     SYSTEMTIME st;  
  198.     GetLocalTime(&st);  
  199.     DWORD dwCurTime = (st.wHour*3600+st.wMinute*60+st.wSecond)*1000 + st.wMilliseconds;  
  200.     DWORD dwStartTime = dwCurTime - dwRunTime;  
  201.     int hm = dwStartTime/3600000;  
  202.     int ms = (dwStartTime - 3600000*hm)/60000;  
  203.     int se = (dwStartTime - 3600000*hm - 60000*ms)/1000;  
  204.     wsprintf(g_strStarTime,TEXT("%02d:%02d:%02d"),hm,ms,se);  
  205.   
  206.   
  207.     // set default Value  
  208.     SetDlgItemText(hDlg,IDC_STARTTIME,g_strStarTime);  
  209.     SetDlgItemText(hDlg,IDC_SPACETIME,TEXT("00:00:00"));  
  210.     SetDlgItemText(hDlg,IDC_REVERSETIME,TEXT("00:00:00"));  
  211.     SetDlgItemText(hDlg,IDC_ALERTCONTENT,TEXT("注意休息哟!"));  
  212.   
  213.     // init the  tray information  
  214.     g_pnid.cbSize = (DWORD)sizeof(NOTIFYICONDATA); // 该结构体大小  
  215.     g_pnid.hWnd = hDlg;         // 窗口句柄  
  216.     g_pnid.uID  = IDI_ICON1;  // 图标句柄  
  217.     // 图标有效|自定义消息有效|鼠标指向显示文字有效  
  218.     g_pnid.uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP ;   
  219.     g_pnid.uCallbackMessage = WM_MY_NOTIFYICON;//自定义的消息名称  
  220.     g_pnid.hIcon = LoadIcon(GetModuleHandle(NULL),MAKEINTRESOURCE(IDI_ICON1));    // 图标句柄  
  221.     wcscpy(g_pnid.szTip, TEXT("TimeControl")); // 鼠标指到那所显示的文字  
  222. }  
  223.   
  224. void WMSet(HWND hDlg)  
  225. {  
  226.     DialogBox(g_hInstance,MAKEINTRESOURCE(IDD_DIALOG2),hDlg,SetDlgProc);  
  227. }  
  228.   
  229. void WMBegin(HWND hDlg)  
  230. {  
  231.      EnableWindow(g_hWndBegin,FALSE);  
  232.      EnableWindow(g_hWndSet,FALSE);  
  233.      EnableWindow(g_hWndMessage, FALSE);  
  234.      EnableWindow(g_hWndStop,TRUE);  
  235.       
  236.      GetDlgItemText(hDlg,IDC_ALERTCONTENT,g_strAlertMessage,SysStringLen(g_strAlertMessage));  
  237.      SetTimer(hDlg,IDT_TIME1,1000,NULL);//设定一个编号为1的时钟,1s  
  238. }  
  239.   
  240. void WMStop(HWND hDlg)  
  241. {  
  242.     EnableWindow(g_hWndBegin,TRUE);  
  243.     EnableWindow(g_hWndSet,TRUE);  
  244.     EnableWindow(g_hWndMessage, TRUE);  
  245.     EnableWindow(g_hWndStop,FALSE);  
  246.     KillTimer(hDlg,IDT_TIME1);  
  247. }  
  248.   
  249. void ToOurTray(HWND hDlg)  
  250. {  
  251.     Shell_NotifyIcon(NIM_ADD, &g_pnid); // 在托盘区添加图标  
  252.     ShowWindow(hDlg,SW_HIDE);  
  253. }  
  254.   
  255. void DeleteOurTray(HWND hDlg)  
  256. {    
  257.     Shell_NotifyIcon(NIM_DELETE,&g_pnid);    //在托盘区删除图标   
  258.     ShowWindow(hDlg,SW_SHOW);  
  259.     ShowWindow(hDlg,SW_SHOWNORMAL);  
  260. }  
  261.   
  262. void ShutDown(HWND hDlg)  
  263. {  
  264.     HANDLE hToken;  
  265.     TOKEN_PRIVILEGES tkp;  
  266.     DWORD dwVersion;  
  267.   
  268.     dwVersion = GetVersion();         // 得到WINDOWS NT或Win32的版本号  
  269.   
  270.     if(dwVersion < 0x80000000)        // 用于判断WIN系列,从而设置相应的权限  
  271.     {  
  272.         OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken);  
  273.         LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME, &tkp.Privileges[0].Luid);  
  274.         tkp.PrivilegeCount = 1;  
  275.         tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;  
  276.         AdjustTokenPrivileges(hToken, FALSE, &tkp, 0, (PTOKEN_PRIVILEGES)NULL, 0);  
  277.         ExitWindowsEx(EWX_SHUTDOWN | EWX_FORCE, 0);  
  278.     }  
  279.     else  
  280.     {  
  281.         ExitWindowsEx(EWX_FORCE | EWX_SHUTDOWN, 0);  
  282.     }  
  283. }  
  284.   
  285. void SetOK(HWND hSetDlg)  
  286. {  
  287.     int i;  
  288.     i = ComboBox_GetCurSel(hWndSetSpTime);  
  289.     g_iSpaceTime = ComboBox_GetItemData(hWndSetSpTime,i);  
  290.   
  291.     i = ComboBox_GetCurSel(hWndAlertMetod);  
  292.     g_iAlertMethod = ComboBox_GetItemData(hWndAlertMetod,i);  
  293.   
  294.     int iHours,iMin;  
  295.     iHours = g_iSpaceTime/60;  
  296.     iMin   = g_iSpaceTime%60;  
  297.   
  298.     wsprintf(g_strSpaceTime,TEXT("%02d:%02d:00"),iHours,iMin);  
  299.     // set default Value  
  300.     SetDlgItemText(g_hWndMain,IDC_STARTTIME,g_strStarTime);  
  301.     SetDlgItemText(g_hWndMain,IDC_SPACETIME,g_strSpaceTime);  
  302.   
  303.     //  
  304.     g_iSpaceTimeForTimer = g_iSpaceTime*60;  
  305. }  
  306.   
  307. BOOL CALLBACK SetDlgProc(HWND hWnd,UINT uMsg,WPARAM wParam,LPARAM lParam)    
  308. {  
  309.     switch(uMsg)  
  310.     {  
  311.     case WM_INITDIALOG:  
  312.         {  
  313.             // set space time  
  314.             hWndSetSpTime = GetDlgItem(hWnd,IDC_SETAPCAETIME);  
  315.             int i = ComboBox_AddString(hWndSetSpTime,TEXT("10分钟"));  
  316.             ComboBox_SetItemData(hWndSetSpTime,i,1);  
  317.             i = ComboBox_AddString(hWndSetSpTime,TEXT("20分钟"));  
  318.             ComboBox_SetItemData(hWndSetSpTime,i,20);  
  319.             i = ComboBox_AddString(hWndSetSpTime,TEXT("30分钟"));  
  320.             ComboBox_SetItemData(hWndSetSpTime,i,30);  
  321.             i = ComboBox_AddString(hWndSetSpTime,TEXT("45分钟"));  
  322.             ComboBox_SetItemData(hWndSetSpTime,i,45);  
  323.             i = ComboBox_AddString(hWndSetSpTime,TEXT("1小时"));  
  324.             ComboBox_SetItemData(hWndSetSpTime,i,60);  
  325.             i = ComboBox_AddString(hWndSetSpTime,TEXT("2小时"));  
  326.             ComboBox_SetItemData(hWndSetSpTime,i,120);  
  327.             i = ComboBox_AddString(hWndSetSpTime,TEXT("3小时"));  
  328.             ComboBox_SetItemData(hWndSetSpTime,i,180);  
  329.             ComboBox_SetCurSel(hWndSetSpTime,0);  
  330.   
  331.             //set alert method  
  332.             hWndAlertMetod = GetDlgItem(hWnd,IDC_SETALERTMETHOD);  
  333.             i = ComboBox_AddString(hWndAlertMetod,TEXT("消息提醒"));  
  334.             ComboBox_SetItemData(hWndAlertMetod,i,Method_AlertMessage);  
  335.             i = ComboBox_AddString(hWndAlertMetod,TEXT("关机"));  
  336.             ComboBox_SetItemData(hWndAlertMetod,i,Method_ShutDown);  
  337.             i = ComboBox_AddString(hWndAlertMetod,TEXT("音乐1"));  
  338.             ComboBox_SetItemData(hWndAlertMetod,i,Method_AlertMusic1);  
  339.             i = ComboBox_AddString(hWndAlertMetod,TEXT("音乐2"));  
  340.             ComboBox_SetItemData(hWndAlertMetod,i,Method_AlertMusic2);  
  341.             ComboBox_SetCurSel(hWndAlertMetod,0);  
  342.         }  
  343.         return TRUE;  
  344.     case WM_COMMAND:  
  345.         switch(LOWORD(wParam))  
  346.         {  
  347.         case IDC_SETOK:  
  348.             SetOK(hWnd);  
  349.             EndDialog(hWnd,0);  
  350.             return TRUE;  
  351.         case IDCANCEL:  
  352.             EndDialog(hWnd,0);  
  353.             return TRUE;  
  354.         }  
  355.         break;  
  356.     }  
  357.     return FALSE;  
  358. }  
  359.   
  360. void AlertMessageFunc(int index)  
  361. {  
  362.     UINT nRet;  
  363.     switch(index)  
  364.     {  
  365.     case Method_AlertMessage:  
  366.         MessageBox(g_hWndMain,g_strAlertMessage,TEXT("Message"),MB_OK);  
  367.         break;  
  368.     case Method_AlertMusic1:  
  369.         mciSendString(TEXT("open music\\Halo.mp3 alias music"),NULL,0,NULL);  
  370.         mciSendString(TEXT("play music"),NULL,0,NULL);  
  371.         nRet =MessageBox(g_hWndMain,TEXT("音乐提醒1"),TEXT("音乐提醒"),MB_OK);  
  372.         if(nRet == IDOK)  
  373.             mciSendString(TEXT("close music"),NULL,0,(HWND)GetModuleHandle(NULL));  
  374.         break;  
  375.     case Method_AlertMusic2:  
  376.         mciSendString(TEXT("open music\\江南Style.mp3 alias music"),NULL,0,NULL);  
  377.         mciSendString(TEXT("play music"),NULL,0,NULL);  
  378.         nRet =MessageBox(g_hWndMain,TEXT("音乐提醒2"),TEXT("音乐提醒"),MB_OK);  
  379.         if(nRet == IDOK)  
  380.             mciSendString(TEXT("close music"),NULL,0,(HWND)GetModuleHandle(NULL));  
  381.         break;  
  382.     case Method_ShutDown:  
  383.         MessageBox(g_hWndMain,TEXT("关机"),TEXT("Message"),MB_OK);  
  384.         break;  
  385.     default:  
  386.         break;  
  387.     }  
  388. }  
原创粉丝点击