前些天闲着没事,做个定时提醒的小工具。主要是想巩固下Win32SDK的学习,同时提醒下自己。功能没有完全写完,有些功能想美化,美工不好。处于其他事情就先搁置了。编码可能不是很好。
源代码:http://download.csdn.net/detail/wangqiulin123456/4907291
功能点:
- 定时消息提醒
- 定时音乐提醒
- 定时关机
- 获取开机时间
截图:
源代码:
#include <windows.h>
#include <WindowsX.h>
#include <CommCtrl.h>
#include "resource.h"
#include "mmsystem.h"
#pragma comment(lib,"winmm.lib")
#define Method_AlertMessage 50000
#define Method_ShutDown 50001
#define Method_AlertMusic1 50002
#define Method_AlertMusic2 50003
#define IDT_TIME1 101
// Data
HICON g_hIconLarge;
HICON g_hIconSmall;
BSTR g_strStarTime; //get Start Computer Time
BSTR g_strSpaceTime; // set the alert Time
BSTR g_strAlertMessage; //the alert message variable
NOTIFYICONDATA g_pnid; //the notify icon data variable
HWND g_hWndBegin, g_hWndStop, g_hWndSet,g_hWndMessage; // take turns the HWND begin, stop, set, Dialog
HINSTANCE g_hInstance; // the instance of the Main Windows
HWND g_hWndMain; // the HWND of the Main Windows
int g_iSpaceTime,g_iSpaceTimeForTimer,g_iAlertMethod; //take turns is the space time, space time for Timer, alert method
HWND hWndSetSpTime,hWndAlertMetod; // the the set Dialog two combobox HWND
LRESULT CALLBACK DialogProc(HWND ,UINT,WPARAM,LPARAM) ;
BOOL CALLBACK SetDlgProc(HWND ,UINT,WPARAM,LPARAM);
void OnInitDialog(HWND hDlg);
void WMSet(HWND hDlg); //设置
void WMBegin(HWND hDlg); //开始
void WMStop(HWND hDlg); //停止
void ToOurTray(HWND hDlg); //托盘最小化
void DeleteOurTray(HWND hDlg); //删除托盘最小化
void ShutDown(HWND hDlg); //关机
void AlertMessageFunc(int ); //消息提醒
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
g_hIconLarge = static_cast<HICON>(LoadImage(hInstance, TEXT("IDI_ICON1"), IMAGE_ICON, //set large ico
GetSystemMetrics(SM_CXICON), GetSystemMetrics(SM_CXICON), 0));
g_hIconSmall = static_cast<HICON>(LoadImage(hInstance, TEXT("IDI_ICON1"), IMAGE_ICON, //set small ico
GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CXSMICON), 0));
g_hInstance = hInstance;
MSG msg;
HWND hwnd = CreateDialog(hInstance, MAKEINTRESOURCE(IDD_DIALOG1), NULL, (DLGPROC)DialogProc);
g_hWndMain = hwnd;
ShowWindow(hwnd, SW_SHOW);
UpdateWindow(hwnd);
while(GetMessage(&msg, NULL, 0, 0))
{
if( !IsDialogMessage( hwnd, &msg ) )
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
// Exit
DestroyIcon(g_hIconLarge);
DestroyIcon(g_hIconSmall);
return msg.wParam;
}
LRESULT CALLBACK DialogProc(HWND hWnd,UINT uMsg,WPARAM wParam,LPARAM lParam)
{
HWND hTrayWnd ;
HMENU hMenu;
int iHours, iMin, iSec;
switch(uMsg)
{
case WM_INITDIALOG:
OnInitDialog(hWnd);
SetWindowPos(hWnd,NULL,500,200,0,0,SWP_NOSIZE);
break;
case WM_MY_NOTIFYICON:
{
switch(lParam)
{
case WM_RBUTTONUP:
MessageBox(hWnd,TEXT("Reight Button up"),TEXT("Message"),MB_OK);
//hMenu = CreateMenu();
//AppendMenu(hMenu,
break;
case WM_LBUTTONDBLCLK:
ShowWindow(hWnd,SW_SHOWNORMAL);
SetWindowLong(hWnd,GWL_EXSTYLE, WS_EX_APPWINDOW );//隐藏任务拦按钮
hTrayWnd = FindWindow(TEXT("Shell_TrayWnd"),NULL);
if(::IsWindow(hTrayWnd))
{
ShowWindow(hTrayWnd,SW_HIDE);
ShowWindow(hTrayWnd,SW_SHOW);
}
//DeleteOurTray(hWnd);
break;
default:
break;
}
}
break;
case WM_PAINT:
break;
case WM_TIMER:
{
if (wParam == IDT_TIME1)
{
if(g_iSpaceTimeForTimer--)
{
iHours = g_iSpaceTimeForTimer/3600;
iMin = (g_iSpaceTimeForTimer - 3600*iHours)/60;
iSec = (g_iSpaceTimeForTimer - 3600*iHours - 60*iMin);
wsprintf(g_strSpaceTime,TEXT("%02d:%02d:%02d"),iHours,iMin,iSec);
// set default Value
SetDlgItemText(g_hWndMain,IDC_REVERSETIME,g_strSpaceTime);
}else{
KillTimer(hWnd,IDT_TIME1);
g_iSpaceTimeForTimer = g_iSpaceTime*60;
EnableWindow(g_hWndBegin,TRUE);
EnableWindow(g_hWndSet,TRUE);
EnableWindow(g_hWndMessage,TRUE);
EnableWindow(g_hWndStop,FALSE);
AlertMessageFunc(g_iAlertMethod);
}
}
}
break;
case WM_SYSCOMMAND:
if (wParam == SC_MINIMIZE)
{
ToOurTray(hWnd);
SetWindowLong(hWnd,GWL_EXSTYLE,WS_EX_TOOLWINDOW);//隐藏任务拦按钮
return 0;
}
break;
case WM_COMMAND:
switch(LOWORD(wParam))
{
case IDC_SET:
WMSet(hWnd);
break;
case IDC_BEGIN:
WMBegin(hWnd);
break;
case IDC_STOP:
WMStop(hWnd);
break;
case IDCANCEL:
PostQuitMessage(0);
break;
default: break;
}
break;
case WM_DESTROY:
SysFreeString(g_strSpaceTime);
SysFreeString(g_strStarTime);
SysFreeString(g_strAlertMessage);
KillTimer(hWnd,IDT_TIME1);
PostQuitMessage(0);
break;
}
return FALSE;
}
void OnInitDialog(HWND hDlg)
{
SendMessage(hDlg,WM_SETICON,FALSE,reinterpret_cast<LPARAM>(g_hIconSmall));
SendMessage(hDlg,WM_SETICON,TRUE,reinterpret_cast<LPARAM>(g_hIconLarge));
g_strStarTime = SysAllocString(OLESTR("00:00:00"));
g_strSpaceTime = SysAllocString(OLESTR("00:00:00"));
g_strAlertMessage = SysAllocStringLen(NULL,200);
g_hWndBegin = GetDlgItem(hDlg,IDC_BEGIN);
g_hWndSet = GetDlgItem(hDlg,IDC_SET);
g_hWndStop = GetDlgItem(hDlg,IDC_STOP);
g_hWndMessage = GetDlgItem(hDlg,IDC_ALERTCONTENT);
EnableWindow(g_hWndStop,FALSE);
//get the Start time
DWORD dwRunTime = GetTickCount();
SYSTEMTIME st;
GetLocalTime(&st);
DWORD dwCurTime = (st.wHour*3600+st.wMinute*60+st.wSecond)*1000 + st.wMilliseconds;
DWORD dwStartTime = dwCurTime - dwRunTime;
int hm = dwStartTime/3600000;
int ms = (dwStartTime - 3600000*hm)/60000;
int se = (dwStartTime - 3600000*hm - 60000*ms)/1000;
wsprintf(g_strStarTime,TEXT("%02d:%02d:%02d"),hm,ms,se);
// set default Value
SetDlgItemText(hDlg,IDC_STARTTIME,g_strStarTime);
SetDlgItemText(hDlg,IDC_SPACETIME,TEXT("00:00:00"));
SetDlgItemText(hDlg,IDC_REVERSETIME,TEXT("00:00:00"));
SetDlgItemText(hDlg,IDC_ALERTCONTENT,TEXT("注意休息哟!"));
// init the tray information
g_pnid.cbSize = (DWORD)sizeof(NOTIFYICONDATA); // 该结构体大小
g_pnid.hWnd = hDlg; // 窗口句柄
g_pnid.uID = IDI_ICON1; // 图标句柄
// 图标有效|自定义消息有效|鼠标指向显示文字有效
g_pnid.uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP ;
g_pnid.uCallbackMessage = WM_MY_NOTIFYICON;//自定义的消息名称
g_pnid.hIcon = LoadIcon(GetModuleHandle(NULL),MAKEINTRESOURCE(IDI_ICON1)); // 图标句柄
wcscpy(g_pnid.szTip, TEXT("TimeControl")); // 鼠标指到那所显示的文字
}
void WMSet(HWND hDlg)
{
DialogBox(g_hInstance,MAKEINTRESOURCE(IDD_DIALOG2),hDlg,SetDlgProc);
}
void WMBegin(HWND hDlg)
{
EnableWindow(g_hWndBegin,FALSE);
EnableWindow(g_hWndSet,FALSE);
EnableWindow(g_hWndMessage, FALSE);
EnableWindow(g_hWndStop,TRUE);
GetDlgItemText(hDlg,IDC_ALERTCONTENT,g_strAlertMessage,SysStringLen(g_strAlertMessage));
SetTimer(hDlg,IDT_TIME1,1000,NULL);//设定一个编号为1的时钟,1s
}
void WMStop(HWND hDlg)
{
EnableWindow(g_hWndBegin,TRUE);
EnableWindow(g_hWndSet,TRUE);
EnableWindow(g_hWndMessage, TRUE);
EnableWindow(g_hWndStop,FALSE);
KillTimer(hDlg,IDT_TIME1);
}
void ToOurTray(HWND hDlg)
{
Shell_NotifyIcon(NIM_ADD, &g_pnid); // 在托盘区添加图标
ShowWindow(hDlg,SW_HIDE);
}
void DeleteOurTray(HWND hDlg)
{
Shell_NotifyIcon(NIM_DELETE,&g_pnid); //在托盘区删除图标
ShowWindow(hDlg,SW_SHOW);
ShowWindow(hDlg,SW_SHOWNORMAL);
}
void ShutDown(HWND hDlg)
{
HANDLE hToken;
TOKEN_PRIVILEGES tkp;
DWORD dwVersion;
dwVersion = GetVersion(); // 得到WINDOWS NT或Win32的版本号
if(dwVersion < 0x80000000) // 用于判断WIN系列,从而设置相应的权限
{
OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken);
LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME, &tkp.Privileges[0].Luid);
tkp.PrivilegeCount = 1;
tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
AdjustTokenPrivileges(hToken, FALSE, &tkp, 0, (PTOKEN_PRIVILEGES)NULL, 0);
ExitWindowsEx(EWX_SHUTDOWN | EWX_FORCE, 0);
}
else
{
ExitWindowsEx(EWX_FORCE | EWX_SHUTDOWN, 0);
}
}
void SetOK(HWND hSetDlg)
{
int i;
i = ComboBox_GetCurSel(hWndSetSpTime);
g_iSpaceTime = ComboBox_GetItemData(hWndSetSpTime,i);
i = ComboBox_GetCurSel(hWndAlertMetod);
g_iAlertMethod = ComboBox_GetItemData(hWndAlertMetod,i);
int iHours,iMin;
iHours = g_iSpaceTime/60;
iMin = g_iSpaceTime%60;
wsprintf(g_strSpaceTime,TEXT("%02d:%02d:00"),iHours,iMin);
// set default Value
SetDlgItemText(g_hWndMain,IDC_STARTTIME,g_strStarTime);
SetDlgItemText(g_hWndMain,IDC_SPACETIME,g_strSpaceTime);
//
g_iSpaceTimeForTimer = g_iSpaceTime*60;
}
BOOL CALLBACK SetDlgProc(HWND hWnd,UINT uMsg,WPARAM wParam,LPARAM lParam)
{
switch(uMsg)
{
case WM_INITDIALOG:
{
// set space time
hWndSetSpTime = GetDlgItem(hWnd,IDC_SETAPCAETIME);
int i = ComboBox_AddString(hWndSetSpTime,TEXT("10分钟"));
ComboBox_SetItemData(hWndSetSpTime,i,1);
i = ComboBox_AddString(hWndSetSpTime,TEXT("20分钟"));
ComboBox_SetItemData(hWndSetSpTime,i,20);
i = ComboBox_AddString(hWndSetSpTime,TEXT("30分钟"));
ComboBox_SetItemData(hWndSetSpTime,i,30);
i = ComboBox_AddString(hWndSetSpTime,TEXT("45分钟"));
ComboBox_SetItemData(hWndSetSpTime,i,45);
i = ComboBox_AddString(hWndSetSpTime,TEXT("1小时"));
ComboBox_SetItemData(hWndSetSpTime,i,60);
i = ComboBox_AddString(hWndSetSpTime,TEXT("2小时"));
ComboBox_SetItemData(hWndSetSpTime,i,120);
i = ComboBox_AddString(hWndSetSpTime,TEXT("3小时"));
ComboBox_SetItemData(hWndSetSpTime,i,180);
ComboBox_SetCurSel(hWndSetSpTime,0);
//set alert method
hWndAlertMetod = GetDlgItem(hWnd,IDC_SETALERTMETHOD);
i = ComboBox_AddString(hWndAlertMetod,TEXT("消息提醒"));
ComboBox_SetItemData(hWndAlertMetod,i,Method_AlertMessage);
i = ComboBox_AddString(hWndAlertMetod,TEXT("关机"));
ComboBox_SetItemData(hWndAlertMetod,i,Method_ShutDown);
i = ComboBox_AddString(hWndAlertMetod,TEXT("音乐1"));
ComboBox_SetItemData(hWndAlertMetod,i,Method_AlertMusic1);
i = ComboBox_AddString(hWndAlertMetod,TEXT("音乐2"));
ComboBox_SetItemData(hWndAlertMetod,i,Method_AlertMusic2);
ComboBox_SetCurSel(hWndAlertMetod,0);
}
return TRUE;
case WM_COMMAND:
switch(LOWORD(wParam))
{
case IDC_SETOK:
SetOK(hWnd);
EndDialog(hWnd,0);
return TRUE;
case IDCANCEL:
EndDialog(hWnd,0);
return TRUE;
}
break;
}
return FALSE;
}
void AlertMessageFunc(int index)
{
UINT nRet;
switch(index)
{
case Method_AlertMessage:
MessageBox(g_hWndMain,g_strAlertMessage,TEXT("Message"),MB_OK);
break;
case Method_AlertMusic1:
mciSendString(TEXT("open music\\Halo.mp3 alias music"),NULL,0,NULL);
mciSendString(TEXT("play music"),NULL,0,NULL);
nRet =MessageBox(g_hWndMain,TEXT("音乐提醒1"),TEXT("音乐提醒"),MB_OK);
if(nRet == IDOK)
mciSendString(TEXT("close music"),NULL,0,(HWND)GetModuleHandle(NULL));
break;
case Method_AlertMusic2:
mciSendString(TEXT("open music\\江南Style.mp3 alias music"),NULL,0,NULL);
mciSendString(TEXT("play music"),NULL,0,NULL);
nRet =MessageBox(g_hWndMain,TEXT("音乐提醒2"),TEXT("音乐提醒"),MB_OK);
if(nRet == IDOK)
mciSendString(TEXT("close music"),NULL,0,(HWND)GetModuleHandle(NULL));
break;
case Method_ShutDown:
MessageBox(g_hWndMain,TEXT("关机"),TEXT("Message"),MB_OK);
break;
default:
break;
}
}