MFC 定时器的使用.docx
《MFC 定时器的使用.docx》由会员分享,可在线阅读,更多相关《MFC 定时器的使用.docx(17页珍藏版)》请在冰豆网上搜索。
MFC定时器的使用
MFC定时器的使用
点击连接(按住Ctrl点击)
最近遇到关于定时器的使用问题,在网上也搜索了很多相关资料。
其中都是讲了使用SetTimer和KillTimer
具体的介绍大家可以看看这个 连接:
里面讲的比较详细的。
但是由于自己不是很了解怎样使用,就是说在那里添加SetTimer()和KillTimer()所以尝试也是失败了好几次。
经过摸索终于成功了,现在和大家分享一下,相互学习。
。
我的是基于对话框的工程,
于是在OnInitDialog()里面添加定时器
BOOLCMonitorProcessDlg:
:
OnInitDialog()
{
SetTimer(1,3000,NULL); //第一个参数可以自己设置是定时器的ID,第二个参数是间隔时间(毫秒为单位的)
}
当然别忘记添加WM_TIMER消息OnTimer()
然后我定义了2个全局变量
intnPress=0;//用于联合判断“启动”按钮的按下状态
BOOLbStatic=FALSE;//用于判断程序是否在执行,以便KillTimer()
例如按下按钮之后,显示“”
voidCMonitorProcessDlg:
:
OnBnClickedButtonStart()
{
MessageBox(" ");
nPress++;
bStatic=TRUE;
}
在OnTimer()函数里面添加代码。
voidCMonitorProcessDlg:
:
OnTimer(UINT_PTRnIDEvent)
{
//TODO:
在此添加消息处理程序代码和/或调用默认值
if(nIDEvent==1&&nPress==1)
{
OnBnClickedButtonStart();
nPress--;
}
if(bStatic==TRUE)
{
KillTimer
(1);
}
CDialog:
:
OnTimer(nIDEvent);
}
上面只是一个简单的演示,希望对大家有帮助吧。
。
。
下面的是我工程里面的代码:
(是用来监测其他程序的,如果其他程序没有执行则重启该程序。
。
)
//MonitorProcessDlg.cpp:
实现文件
//
#include"stdafx.h"
#include"MonitorProcess.h"
#include"MonitorProcessDlg.h"
#include"Psapi.h"
#pragmacomment(lib,"psapi.lib")
#ifdef_DEBUG
#definenewDEBUG_NEW
#endif
//注册消息WM_SHOWTASK
#defineWM_SHOWTASK(WM_USER+1)
//用于应用程序“关于”菜单项的CAboutDlg对话框
classCAboutDlg:
publicCDialog
{
public:
CAboutDlg();
//对话框数据
enum{IDD=IDD_ABOUTBOX};
protected:
virtualvoidDoDataExchange(CDataExchange*pDX);//DDX/DDV支持
//实现
protected:
DECLARE_MESSAGE_MAP()
};
CAboutDlg:
:
CAboutDlg():
CDialog(CAboutDlg:
:
IDD)
{
}
voidCAboutDlg:
:
DoDataExchange(CDataExchange*pDX)
{
CDialog:
:
DoDataExchange(pDX);
}
BEGIN_MESSAGE_MAP(CAboutDlg,CDialog)
END_MESSAGE_MAP()
//CMonitorProcessDlg对话框
CMonitorProcessDlg:
:
CMonitorProcessDlg(CWnd*pParent/*=NULL*/)
:
CDialog(CMonitorProcessDlg:
:
IDD,pParent)
m_strPathName(_T(""))
m_strProcessName(_T(""))
{
m_hIcon=AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
voidCMonitorProcessDlg:
:
DoDataExchange(CDataExchange*pDX)
{
CDialog:
:
DoDataExchange(pDX);
DDX_Control(pDX,IDC_LIST_PATHNAME,m_ctrlPathName);
DDX_CBString(pDX,IDC_LIST_PATHNAME,m_strPathName);
DDX_Control(pDX,IDC_LIST_PROCESSNAME,m_ctrlProcessName);
DDX_CBString(pDX,IDC_LIST_PROCESSNAME,m_strProcessName);
}
BEGIN_MESSAGE_MAP(CMonitorProcessDlg,CDialog)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
//}}AFX_MSG_MAP
ON_BN_CLICKED(ID_BUTTON_START,&CMonitorProcessDlg:
:
OnBnClickedButtonStart)
ON_WM_HOTKEY()
ON_MESSAGE(WM_SHOWTASK,OnShowTask)
ON_CBN_SELCHANGE(IDC_LIST_PATHNAME,&CMonitorProcessDlg:
:
OnCbnSelchangeListPathname)
ON_CBN_SELCHANGE(IDC_LIST_PROCESSNAME,&CMonitorProcessDlg:
:
OnCbnSelchangeListProcessname)
ON_WM_NCPAINT()
ON_WM_TIMER()
END_MESSAGE_MAP()
//CMonitorProcessDlg消息处理程序
BOOLCMonitorProcessDlg:
:
OnInitDialog()
{
CDialog:
:
OnInitDialog();
//将“关于...”菜单项添加到系统菜单中。
//IDM_ABOUTBOX必须在系统命令范围内。
ASSERT((IDM_ABOUTBOX&0xFFF0)==IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX<0xF000);
CMenu*pSysMenu=GetSystemMenu(FALSE);
if(pSysMenu!
=NULL)
{
CStringstrAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if(!
strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING,IDM_ABOUTBOX,strAboutMenu);
}
}
//设置此对话框的图标。
当应用程序主窗口不是对话框时,框架将自动
//执行此操作
SetIcon(m_hIcon,TRUE);//设置大图标
SetIcon(m_hIcon,FALSE);//设置小图标
//TODO:
在此添加额外的初始化代码
:
:
RegisterHotKey(m_hWnd,918,MOD_CONTROL,'S');
//添加定时器
SetTimer(1,3000,NULL);
CStdioFilemyFile;
CStringoneLine;
if(!
myFile.Open(("path.txt"),CFile:
:
modeRead|CFile:
:
typeText))
{
AfxMessageBox(_T("打开文件错误!
"));
returnFALSE;
}
else
{
//donothing
}
while(myFile.ReadString(oneLine))
{
m_ctrlPathName.AddString(oneLine);
}
myFile.Close();
if(!
myFile.Open(("process.txt"),CFile:
:
modeRead|CFile:
:
typeText))
{
AfxMessageBox(_T("打开文件错误!
"));
returnFALSE;
}
else
{
//donothing
}
while(myFile.ReadString(oneLine))
{
m_ctrlProcessName.AddString(oneLine);
}
myFile.Close();
returnTRUE;//除非将焦点设置到控件,否则返回TRUE
}
voidCMonitorProcessDlg:
:
OnSysCommand(UINTnID,LPARAMlParam)
{
if((nID&0xFFF0)==IDM_ABOUTBOX)
{
CAboutDlgdlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog:
:
OnSysCommand(nID,lParam);
}
}
//如果向对话框添加最小化按钮,则需要下面的代码
//来绘制该图标。
对于使用文档/视图模型的MFC应用程序,
//这将由框架自动完成。
voidCMonitorProcessDlg:
:
OnPaint()
{
if(IsIconic())
{
CPaintDCdc(this);//用于绘制的设备上下文
SendMessage(WM_ICONERASEBKGND,reinterpret_cast(dc.GetSafeHdc()),0);
//使图标在工作区矩形中居中
intcxIcon=GetSystemMetrics(SM_CXICON);
intcyIcon=GetSystemMetrics(SM_CYICON);
CRectrect;
GetClientRect(&rect);
intx=(rect.Width()-cxIcon+1)/2;
inty=(rect.Height()-cyIcon+1)/2;
//绘制图标
dc.DrawIcon(x,y,m_hIcon);
}
else
{
CDialog:
:
OnPaint();
}
}
//当用户拖动最小化窗口时系统调用此函数取得光标
//显示。
HCURSORCMonitorProcessDlg:
:
OnQueryDragIcon()
{
returnstatic_cast(m_hIcon);
}
/////////////////////////////
intKillProcess(LPCSTRpszWindowTitle)
{
HANDLEhProcessHandle;
ULONGnProcessID;
HWNDTheWindow;
TheWindow=:
:
FindWindow(NULL,pszWindowTitle);
:
:
GetWindowThreadProcessId(TheWindow,&nProcessID);
hProcessHandle=:
:
OpenProcess(PROCESS_TERMINATE,FALSE,nProcessID);
return:
:
TerminateProcess(hProcessHandle,4);
}
////////////////////////////启动
intnPress=0;//用于联合判断“启动”按钮的按下状态
BOOLbStatic=FALSE;//用于判断程序是否在执行,以便KillTimer
voidCMonitorProcessDlg:
:
OnBnClickedButtonStart()
{
//TODO:
在此添加控件通知处理程序代码
//读取命令行参数
LPTSTRlpstr=AfxGetApp()->m_lpCmdLine;
this->ShowWindow(SW_HIDE);
//读取路径以及文件名
CStringstrCmdLine;
strCmdLine=m_strPathName;
//读取程序名
CStringprocessName;
processName=m_strProcessName;
CStringWarning("正在运行中!
或者输入有误!
");
intiStartIndex=1;
intiEndIndex=0;
CStringcCurrentDir="";
CStringstrTempName="";
iEndIndex=strCmdLine.Find("-",iStartIndex);
cCurrentDir=strCmdLine.Mid(iStartIndex,iEndIndex-iStartIndex-1);
iStartIndex=iEndIndex+1;
strTempName=strCmdLine.Mid(iStartIndex,strCmdLine.GetLength()-iStartIndex);
//BOOLTemp=IsAbort((char*)(LPCTSTR)strTempName);
LPCSTRpName("MonitorProcess");
if(strlen(strCmdLine)==0&&strlen(processName)==0)
{
AfxMessageBox("未输入有效信息!
");
KillProcess(pName);
return;
}
elseif(strlen(strCmdLine)==0)
{
AfxMessageBox("未输入有效的路径名!
");
KillProcess(pName);
return;
}
elseif(strlen(processName)==0)
{
AfxMessageBox("未输入程序名!
");
KillProcess(pName);
return;
}
else
{
//donothing
}
CWnd*pWnd=CWnd:
:
FindWindow(NULL,(LPCSTR)processName);
if(pWnd==NULL)
{
//如果中断重新启动
ShellExecute(NULL,"open",strTempName,"",cCurrentDir,SW_SHOWNORMAL);
//KillProcess(pName);
}
else
{
Warning.Insert(0,processName);
AfxMessageBox(Warning);
bStatic=TRUE;
//KillProcess(pName);
}
nPress++;
return;
}
///////////////快捷键
inti=0;//用于联合判断热键的按下状态
voidCMonitorProcessDlg:
:
OnHotKey(UINTnHotKeyId,UINTnKey1,UINTnKey2)
{
//TODO:
在此添加消息处理程序代码和/或调用默认值
if(i==0&&nHotKeyId==918)
{
i++;
this->ShowWindow(SW_SHOW);//显示主窗口
DeleteTray();
}
elseif(i==1&&nHotKeyId==918)
{
i--;
ToTray();
//OnShowTask(IDR_MAINFRAME,lParam);
}
CDialog:
:
OnHotKey(nHotKeyId,nKey1,nKey2);
}
////////////////自定义消息
LRESULTCMonitorProcessDlg:
:
OnShowTask(WPARAMwParam,LPARAMlParam)
{
if(wParam!
=IDR_MAINFRAME)//图标ID
return1;
switch(lParam)
{
caseWM_RBUTTONUP:
//右键起来时弹出快捷菜单,这里只有一个“退出”
{
LPPOINTlpoint=newtagPOINT;
:
:
GetCursorPos(lpoint);//得到鼠标位置
CMenumenu;
menu.CreatePopupMenu();//声明一个弹出式菜单
//增加菜单项“退出”,点击则发送消息WM_DESTROY给主窗口将程序结束。
menu.AppendMenu(MF_STRING,WM_DESTROY,"退出");
//确定弹出式菜单的位置
menu.TrackPopupMenu(TPM_LEFTALIGN,lpoint->x,lpoint->y,this);
//资源回收
HMENUhmenu=menu.Detach();
menu.DestroyMenu();
deletelpoint;
DeleteTray();
}
break;
caseWM_LBUTTONDBLCLK:
//双击左键的处理
{
this->ShowWindow(SW_SHOW);//显示主窗口
DeleteTray();
}
break;
default:
break;
}
return0;
}
/////////////托盘绘制图标
voidCMonitorProcessDlg:
:
ToTray(void)
{
NOTIFYICONDATAnid;
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_SHOWTASK;//自定义的消息名称
nid.hIcon=LoadIcon(AfxGetInstanceHandle(),MAKEINTRESOURCE(IDR_MAINFRAME));
strcpy(nid.szTip,"MonitorProcess");//信息提示条
Shell_NotifyIcon(NIM_ADD,&nid);//在托盘区添加图标
ShowWindow(SW_HIDE);//隐藏主窗口
}
////////////删除托盘图标
voidCMonitorProcessDlg:
:
DeleteTray(void)
{
NOTIFYICONDATAnid;
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_SHOWTASK;//自定义的消息名称
nid.hIcon=LoadIcon(AfxGetInstanceHandle(),MAKEINTRESOURCE(IDR_MAINFRAME));
strcpy(nid.szTip,"MonitorProcess");//信息提示条
Shell_NotifyIcon(NIM_DELETE,&nid);//在托盘区删除图标
}
voidCMonitorProcessDlg:
:
OnCbnSelchangeListPathname()
{
//TODO:
在此添加控件通知处理程序代码
intj=0;
j=m_ctrlPathName.GetCurSel();
m_ctrlPathName.GetLBText(j,m_strPathName);
}
voidCMonitorProcessDlg:
:
OnCbnSelchangeListProcessname()
{
//TODO:
在此添加控件通知处理程序代码
intj=0;
j=m_ctrlProcessName.GetCurSel();
m_ctrlProcessName.GetLBText(j,m_strProcessName);
}
voidCMonitorProcessDlg:
:
OnNcPaint()
{
//TODO:
在此处添加消息处理程序代码
//不为绘图消息调用CDialog:
:
OnNcPaint()
staticinti=2;
if(i>0)
{
i--;
ShowWindow(SW_HIDE);
}
else
{
CDialog:
:
OnNcPaint();
}
}
voidCMonitorProcessDlg:
:
OnTimer(UINT_PTRnIDEvent)
{