回调函数例子

时间:2021-11-05 17:36:56

回调函数例子

具体过程就不再详细描述了。可与看  我转载的“C++回调函数(callback)的使用”一文

直接上例子。


//头文件

#if !defined(AFX_CALLBACKMFCTESTDLG_H__05A42778_CA7F_4D81_9350_3592155687BB__INCLUDED_)
#define AFX_CALLBACKMFCTESTDLG_H__05A42778_CA7F_4D81_9350_3592155687BB__INCLUDED_


#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000


/////////////////////////////////////////////////////////////////////////////
struct TIMEMSG{
int Y;
int M;
int D;
int H;
int Mi;
int Sec;
};
// CCallBackMfcTestDlg dialog
//可以定义回调函数为一个类型,函数指针类型
typedef void (*UserCallBackType)(TIMEMSG a,int b,LPVOID  userParam);//申明回调函数类型


class CCallBackMfcTestDlg : public CDialog
{
// Construction
public:


UserCallBackType m_UserFunPter;
//注册回调函数可以定在类里面,也可以定在外面
BOOL RegiserUserCallBack(int a,UserCallBackType/*回调函数类型*/ UserFun,LPVOID  userParam);
CCallBackMfcTestDlg(CWnd* pParent = NULL);// standard constructor


// Dialog Data
//{{AFX_DATA(CCallBackMfcTestDlg)
enum { IDD = IDD_CALLBACKMFCTEST_DIALOG };
CStatic m_DateTimeDis;
//}}AFX_DATA


// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CCallBackMfcTestDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX);// DDX/DDV support
//}}AFX_VIRTUAL


// Implementation
protected:
HICON m_hIcon;


// Generated message map functions
//{{AFX_MSG(CCallBackMfcTestDlg)
virtual BOOL OnInitDialog();
afx_msg void OnSysCommand(UINT nID, LPARAM lParam);
afx_msg void OnPaint();
afx_msg HCURSOR OnQueryDragIcon();
afx_msg void OnButCallback();
afx_msg void OnTimer(UINT nIDEvent);
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};


//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.


#endif // !defined(AFX_CALLBACKMFCTESTDLG_H__05A42778_CA7F_4D81_9350_3592155687BB__INCLUDED_)





// CallBackMfcTestDlg.cpp : implementation file
//




//源文件
#include "stdafx.h"
#include "CallBackMfcTest.h"
#include "CallBackMfcTestDlg.h"


#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif


/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About


#if 1
//用回调函数的功能定义,由用户完成
void MyCallBackf(/*自定义时间结构类型*/TIMEMSG a,int b,LPVOID  userParam/*用户的参数,此处为类指针*/)
{
//获取类结构指针
CCallBackMfcTestDlg *pThis=(CCallBackMfcTestDlg*)userParam;
CString str;
str.Format("%4d年 %02d月 %02d日 :%02d时 %02d分 %02d秒\n",a.Y,a.M,a.D,a.H,a.Mi,a.Sec);


//在回调函数中使用,类中的成员变量
pThis->m_DateTimeDis.SetWindowText(str);

//AfxMessageBox(str);
TRACE(str);
}
#endif 


class CAboutDlg : public CDialog
{
public:
CAboutDlg();


// Dialog Data
//{{AFX_DATA(CAboutDlg)
enum { IDD = IDD_ABOUTBOX };
//}}AFX_DATA


// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CAboutDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
//}}AFX_VIRTUAL


// Implementation
protected:
//{{AFX_MSG(CAboutDlg)
afx_msg void OnTimer(UINT nIDEvent);
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};


CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
//{{AFX_DATA_INIT(CAboutDlg)
//}}AFX_DATA_INIT
}


void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAboutDlg)
//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
ON_WM_TIMER()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()


/////////////////////////////////////////////////////////////////////////////
// CCallBackMfcTestDlg dialog


CCallBackMfcTestDlg::CCallBackMfcTestDlg(CWnd* pParent /*=NULL*/)
: CDialog(CCallBackMfcTestDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CCallBackMfcTestDlg)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}


void CCallBackMfcTestDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CCallBackMfcTestDlg)
DDX_Control(pDX, IDC_STATIC_TEST, m_DateTimeDis);
//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CCallBackMfcTestDlg, CDialog)
//{{AFX_MSG_MAP(CCallBackMfcTestDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUT_CALLBACK, OnButCallback)
ON_WM_TIMER()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()


/////////////////////////////////////////////////////////////////////////////
// CCallBackMfcTestDlg message handlers


BOOL CCallBackMfcTestDlg::OnInitDialog()
{
CDialog::OnInitDialog();


// Add "About..." menu item to system menu.


// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);


CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}


// Set the icon for this dialog.  The framework does this automatically
//  when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE);// Set big icon
SetIcon(m_hIcon, FALSE);// Set small icon

// TODO: Add extra initialization here

return TRUE;  // return TRUE  unless you set the focus to a control
}


void CCallBackMfcTestDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}


// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.


void CCallBackMfcTestDlg::OnPaint() 
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting


SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);


// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;


// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}


// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CCallBackMfcTestDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}


void CCallBackMfcTestDlg::OnButCallback() 
{
// TODO: Add your control notification handler code here

// SetTimer(0,1000,NULL);
//事件触发调用回调函数@@@@@@@@@@@@@@用户调用注册回调函数,同时,可以传递自己需要的变量,此例中传递的是类指针


//@@@@@@@@@@@@用户程序调用注册函数,然后,注册函数再调用用户回调函数
RegiserUserCallBack(0, MyCallBackf/*在类外面定义用户回调*/, this/*用户参数,此处传递类指针*/);
}




//注册回调函数定义,也是接口定义地方
BOOL CCallBackMfcTestDlg::RegiserUserCallBack(int a, UserCallBackType UserFun, LPVOID userParam)
{
/*
time_t osBinaryTime;  
time( &osBinaryTime );
*/


TIMEMSG BeginTime;
CTime time = CTime::GetCurrentTime();

m_UserFunPter=UserFun;   //@@@@@@@@@把回调函数的指针,传递给类的回调函数的指针变量

BeginTime.Y=time.GetYear();
BeginTime.M=time.GetMonth();
BeginTime.D=time.GetDay();
BeginTime.H=time.GetHour();
BeginTime.Mi=time.GetMinute();
BeginTime.Sec=time.GetSecond();


//TRACE("%2d\%d%d%d%d%d\n",BeginTime.Y,BeginTime.M,BeginTime.D,BeginTime.H,BeginTime.Mi,BeginTime.Sec);
//通知用户回调函数
m_UserFunPter(BeginTime,200,userParam/*用户参数直接传递下来*/); //@@@@@@@@@@调用回调函数

return 0;
}


void CAboutDlg::OnTimer(UINT nIDEvent) 
{
// TODO: Add your message handler code here and/or call default




CDialog::OnTimer(nIDEvent);


}


void CCallBackMfcTestDlg::OnTimer(UINT nIDEvent) 
{


switch(nIDEvent)//用定时器调用回调函数
{
case 0://@@@@@@@@@@@@用户程序调用注册函数,然后,注册函数再调用用户回调函数
RegiserUserCallBack(0, MyCallBackf/*define outside of the class*/, this);
break;
case 1:
break;
}


CDialog::OnTimer(nIDEvent);
}