发布网友 发布时间:2022-04-21 17:37
共3个回答
热心网友 时间:2023-10-02 09:10
留下邮箱吧 我给你程序 一个定时器 时间到可以用MP3提示 而且记录可以保存~~
void CMFC_TimerDlg::OnTimer(UINT nIDEvent) //判断时间到了吗?
{
// TODO: Add your message handler code here and/or call default
GetLocalTime( &m_nowTime );
if( nIDEvent == 100 )
{
for( vector<Time>::iterator iter = m_timeIter; iter != m_time.end() ; iter ++)
{
if( m_nowTime.wHour == iter->hour && m_nowTime.wMinute == iter->minute && iter->isShow == 1)
{
if( iter->isCloseCptr != 0 )
{
KillTimer( 100 );
KillTimer( 101 );
ExitWindowsEx( EWX_SHUTDOWN, NULL);
}
else
{
//时间到了~~~
if( strcmp( iter->message, "\0" ) != 0 && m_isShowMsg == true )
{
m_isShowMsg = false;
CShowMsg dlg;
dlg.m_msg = iter->message;
dlg.m_musicPath = iter->musicPath;
dlg.DoModal();
m_isShowMsg = true;
iter->isShow = 0;
}
}
}
}
}
if( nIDEvent == 101 ) //在Edit上面显示时间
{
CString time;
time.Format( "%d:%d:%d", m_nowTime.wHour, m_nowTime.wMinute, m_nowTime.wSecond );
SetDlgItemText( IDC_EDIT4, time);
}
CDialog::OnTimer(nIDEvent);
}
void CMFC_TimerDlg::OnAdd() //增加记录
{
// TODO: Add your control notification handler code here
Time t;
CString str,str1;
UpdateData();
GetDlgItemText( IDC_EDIT1, str);
GetDlgItemText( IDC_EDIT2, str1);
if( str == "" || str1 == "" )
{
MessageBox( "请输入时间,然后继续." );
return ;
}
t.hour = GetDlgItemInt( IDC_EDIT1 );
t.minute = GetDlgItemInt( IDC_EDIT2 );
GetDlgItemText( IDC_EDIT3, str );
GetDlgItemText( IDC_EDIT5, str1 );
//输入的时间是否合法?
if( t.hour >= 0 && t.hour <= 23 && t.minute >= 0 && t.minute <=59 && str != "" )
{
memset( t.message, 0, sizeof( t.message ) );
for(int i=0; i<str.GetLength(); i++)
{
t.message[i] = str[i];
}
memset( t.musicPath, 0, sizeof( t.musicPath ) );
for( i=0; i<str1.GetLength(); i++)
{
t.musicPath[i] = str1[i];
}
UpdateData(FALSE);
if(m_isCloseCptr)
t.isCloseCptr = 1; //时间到了,是否要关机
else
t.isCloseCptr = 0;
t.isShow = 1;
m_time.push_back( t ); //增加一条记录
m_timeIter = m_time.begin();
if( MessageBox( "这个设置是否要保存?", "消息提示", MB_YESNO ) == IDYES )
{
WriteConfig(); //保存记录
m_time.clear(); //删除全部记录
ReadConfig(); //再更新记录
}
m_timeIter = m_time.begin();
MoveList();
}
else
{
MessageBox( "请填写正确的时间.", "错误提示" );
}
SetDlgItemText( IDC_EDIT1, "" );
SetDlgItemText( IDC_EDIT2, "" );
}
void CMFC_TimerDlg::WriteConfig() //把记录写到文件中
{
m_timeIter = m_time.begin();
char file[150];
memset( file, 0, 150);
strcat( file, m_myPath);
strcat( file, "\\config.czw");
ifstream in(file);
ofstream out(file,ios::trunc);
for( vector<Time>::iterator iter = m_timeIter; iter != m_time.end() ; iter ++)
{
iter->isShow = 1;
out.write( (char*)iter, sizeof( Time));
}
out.close();
}
void CMFC_TimerDlg::ReadConfig() //读取文件中的全部记录
{
char file[150];
memset( file, 0, 150);
strcat( file, m_myPath);
strcat( file, "\\config.czw");
ifstream in(file);
Time t;
m_timeIter = NULL;
while( !in.eof() )
{
memset( &t, 0, sizeof( Time ) );
in.read( (char*)&t, sizeof( Time ));
if( strcmp( t.message, "\0" ) == 0)
{
break;
}
m_time.push_back( t );
}
m_timeIter = m_time.begin();
in.close();
}
void CMFC_TimerDlg::MoveList() //在ListBox显示所以的记录
{
((CListBox*)GetDlgItem(IDC_LIST1))->ResetContent();
m_timeIter = m_time.begin();
int i =0;
CString str;
for( vector<Time>::iterator iter = m_timeIter; iter != m_time.end() ; iter ++)
{
i++;
str.Format( "%d-> 时间:%d:%d, 提示:%s ", i, iter->hour, iter->minute, iter->message );
((CListBox*)GetDlgItem(IDC_LIST1))->AddString( str );
UpdateData( false );
}
}
void CMFC_TimerDlg::OnButton2() //删除一跳记录
{
// TODO: Add your control notification handler code here
int pos =-1;
pos = m_list.GetCurSel();
if( pos >= 0)
{
if( MessageBox( "真的要删除这个设置吗?", "消息提示", MB_YESNO ) == IDYES )
{
vector<Time>::iterator t = &m_time[pos];
m_time.erase( t );
m_timeIter = m_time.begin();
WriteConfig();
m_time.clear();
ReadConfig();
m_timeIter = m_time.begin();
MoveList();
}
}
}
void CMFC_TimerDlg::OnButton3() //增加音乐的路径
{
// TODO: Add your control notification handler code here
CFileDialog fileDlg(TRUE,_T("mp3"),_T("*.mp3"),OFN_HIDEREADONLY||OFN_OVERWRITEPROMPT,"MP3文件(*.mp3)|*.mp3|");
fileDlg.DoModal();
CString fileName=fileDlg.GetPathName();
// ((CEdit*)GetDlgItem(IDC_EDIT5))->SetWindowText(m_fileName);
m_musicPath = fileName;
UpdateData(false);
}
void CMFC_TimerDlg::OnOK()
{
// TODO: Add extra validation here
}
void CMFC_TimerDlg::OnClose() //退出程序
{
// TODO: Add your message handler code here and/or call default
if( MessageBox( "真的要退出程序吗?", "退出提示", MB_YESNO ) ==IDYES )
{
Shell_NotifyIcon(NIM_DELETE,&nid);//在托盘区添加图标
CDialog::OnClose();
}
}
LRESULT CMFC_TimerDlg::onShowTask(WPARAM wParam,LPARAM lParam) //CMainFrame是自己的类名
//wParam接收的是图标的ID,而lParam接收的是鼠标的行为
{
if(wParam!=IDR_MAINFRAME)
return 1;
switch(lParam)
{
case WM_RBUTTONUP://右键起来时弹出快捷菜单,这里只有一个“关闭”
{
LPPOINT lpoint=new tagPOINT;
::GetCursorPos(lpoint);//得到鼠标位置
CMenu menu;
menu.CreatePopupMenu();//声明一个弹出式菜单
menu.TrackPopupMenu(TPM_RIGHTALIGN,lpoint->x,lpoint->y,this);
HMENU hmenu=menu.Detach();
menu.DestroyMenu();
delete lpoint;
}
break;
case WM_LBUTTONDOWN://双击左键的处理
{
this->ShowWindow(SW_SHOW);//简单的显示主窗口完事儿
this->OpenIcon();
}
break;
}
return 0;
}
void CMFC_TimerDlg::toTray() //CMainFrame是自己的类名
{
nid.cbSize=(DWORD)sizeof(NOTIFYICONDATA);
nid.hWnd=this->m_hWnd;
nid.uID=IDR_MAINFRAME;
nid.uFlags=NIF_ICON|NIF_MESSAGE|NIF_TIP ;
nid.uCallbackMessage=WM_RBUTTONDOWN;//自定义的消息名称
nid.hIcon=LoadIcon(AfxGetInstanceHandle(),MAKEINTRESOURCE(IDR_MAINFRAME));//IDI_ICON1 是在任务栏的图标
strcpy(nid.szTip,"CIW 制作");//信息提示条为“计划任务提醒”
Shell_NotifyIcon(NIM_ADD,&nid);//在托盘区添加图标
ShowWindow(SW_HIDE);//隐藏主窗口
}
void CMFC_TimerDlg::OnMenuExit() //CMainFrame是自己的类名
{
}
void CMFC_TimerDlg::OnSize(UINT nType, int cx, int cy)
{
CDialog::OnSize(nType, cx, cy);
// TODO: Add your message handler code here
if(nType==SIZE_MINIMIZED)
{
toTray();
}
}
void CMFC_TimerDlg::WriteReg() //开机启动
{
unsigned char ch[100];
char ch1[300]; //要写入的内容
GetMoleFileName(NULL,ch1,100);
HKEY hkey=HKEY_LOCAL_MACHINE;
strcpy((char*)ch,ch1);
if(RegOpenKeyEx(hkey,"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", //要写人的键值
NULL,KEY_ALL_ACCESS,&hkey)==ERROR_SUCCESS)
{
RegSetValueEx
(hkey,
"CMM",
NULL,
REG_SZ,
ch,
100
);
}
}
void CMFC_TimerDlg::OnMenu1()
{
// TODO: Add your command handler code here
WriteReg();
}
void CMFC_TimerDlg::OnButton4() //"显示任务"的功能
{
// TODO: Add your control notification handler code here
if( m_formType == 1 )
{
SetWindowPos( &wndTopMost, 0, 0, 300, 420, SWP_NOMOVE | SWP_SHOWWINDOW );
m_formType = 0;
((CButton*)GetDlgItem( IDC_BUTTON4 ))->SetWindowText("隐藏任务");
}
else
{
SetWindowPos( &wndTopMost, 0, 0, 300, 270, SWP_NOMOVE | SWP_SHOWWINDOW );
m_formType = 1;
((CButton*)GetDlgItem( IDC_BUTTON4 ))->SetWindowText("显示任务");
}
}
void CMFC_TimerDlg::OnDblclkList1() //查看定时功能
{
// TODO: Add your control notification handler code here
int pos =-1;
pos = m_list.GetCurSel();
if( pos >= 0)
{
vector<Time>::iterator t = &m_time[pos];
CString msg;
char str[4];
memset( str, 0, sizeof( str ) );
if( t->isCloseCptr != 0)
{
strcpy( str, "是");
}
else
{
strcpy( str, "否");
}
msg.Format( "时间 :%d:%d\n消息提示:%s\n音乐路径:%s\n是否关机:%s",
t->hour, t->minute, t->message, t->musicPath, str );
MessageBox( msg );
}
}
void CMFC_TimerDlg::OnShowWindow(BOOL bShow, UINT nStatus)
{
CDialog::OnShowWindow(bShow, nStatus);
}
int CMFC_TimerDlg::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CDialog::OnCreate(lpCreateStruct) == -1)
return -1;
SetWindowText("CIW - Timer");
cMenu.LoadMenu(IDR_MENU1);
SetMenu(&cMenu);
cMenu.Detach();
return 0;
}
热心网友 时间:2023-10-02 09:11
日程管理要目录要 记录在文本中 或者 配置文件中 要么存入数据库中也可以
程序 重载 OnTimer(UINT nIDEvent) 函数 调用 方式SETTIME(1,60000,NULL);
每隔一分钟 获取一次系统时间。然后根据系统时间 和 日程记录的 时间做 比较。 比较到对应的 记录 然后既可以 弹出 日程提示了。
热心网友 时间:2023-10-02 09:11
我也是新手,希望能帮点忙,下面这资料不知道对你有没有用。
VC++实现非窗口类中使用定时器的方法
/////////////////////////////////////////// MyTimer.h: interface for the CMyTimer class.
#if !defined(AFX_MYTIMER_H__D97674D1_B221_49CD_9637_4CBA8C3180CE__INCLUDED_)
#define AFX_MYTIMER_H__D97674D1_B221_49CD_9637_4CBA8C3180CE__INCLUDED_
#i nclude <afxtempl.h>
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
class CMyTimer;
typedef CMap<UINT,UINT,CMyTimer*,CMyTimer*> CTimerMap;
class CMyTimer
{
public:
//设置定时器,nElapse表示时间间隔,sz表示要提示的内容
void SetMyTimer(UINT nElapse,CString sz);
//销毁该实例的定时器
void KillMyTimer();
//保存该实例的定时器标志值
UINT m_nTimerID;
//静态数据成员要提示的内容
CString szContent;
//静态数据成员,映射表类,用于保存所有的定时器信息
static CTimerMap m_sTimeMap;
//静态成员函数,用于处理定时器的消息
static void CALLBACK MyTimerProc(HWND hwnd,UINT uMsg,UINT idEvent,DWORD dwTime);
CMyTimer();
virtual ~CMyTimer();
};
#endif
///////////////////////////////////// MyTimer.cpp: implementation of the CMyTimer class.
#i nclude "stdafx.h"
#i nclude "TimerDemo.h"
#i nclude "MyTimer.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
CTimerMap CMyTimer::m_sTimeMap;
CMyTimer::CMyTimer()
{
m_nTimerID = 0;
}
CMyTimer::~CMyTimer()
{}
void CALLBACK CMyTimer::MyTimerProc(HWND hwnd,UINT uMsg,UINT idEvent,DWORD dwTime)
{
CString sz;
sz.Format("%d号定时器:%s",idEvent,m_sTimeMap[idEvent]->szContent);
AfxMessageBox(sz);
}
void CMyTimer::SetMyTimer(UINT nElapse,CString sz)
{
szContent = sz;
m_nTimerID = SetTimer(NULL,NULL,nElapse,MyTimerProc);
m_sTimeMap[m_nTimerID] = this;
}
void CMyTimer::KillMyTimer()
{
KillTimer(NULL,m_nTimerID);
m_sTimeMap.RemoveKey(m_nTimerID);
}
参考资料:http://hi.baidu.com/ypxmaomao/blog/item/50b2a3095d981285d0581b5d.html