窗口中的回调函数和虚函数
1: 下面列举一下重要的窗口“回调函数”
//当在窗口中“单击鼠标右键” 时,调用指定的回调函数
void
CWindow::SetRightMouseFc(void far (*pFc)(CObject*));
//当在窗口中“按下Ctrl + H”组合键时,调用指定的回调函数
void CWindow::SetHelpFc(void far
(*pFc)(CObject*)=NULL);
//当窗口空闲时,调用指定的回调函数,如同VC中的CWinApp::OnIdle()的概念
void CWindow::SetIdleFc(void far
(*pFc)(CObject*)=NULL);
//窗口定时器,系统“空闲”时每隔55mS调用一次,类似VC中的CWnd::OnTime()函数
void CWindow::SetTimeFc(void far
(*pFc)(CObject*)=NULL);
//附加键盘的回调函数,当窗口“取消息”时会优先考虑该“回调函数”的调用
void CWindow::SetKeyboardFc(int
far (*pFc)(CObject*)=NULL);
//窗口画图回调函数,如同VC中的CWnd::OnDraw()的概念,
void CWindow::SetDrawFc(void far
(*pFc)(CObject*)=NULL);
2: 下面列举重要的窗口虚函数
//窗口画图函数,如同VC中的CWnd::OnDraw(CDC*
pDc)的概念。该函数跟回调函数比较起来,缺点是须重载父窗口,麻烦。
void far CWindow::OnDraw();
3: 举例
int far MyKeyboard_01(CObject* pObj)//附加键盘回调函数
{
CDialog*
pDialog = (CDialog *)pObj; //得到窗口指针
//假设“左移”键配置成“LEFT_KEY”名字, “右移”键配置成“RIGHT_KEY”名字。
if (
ActiveInput(“LeftKey”)) return(VK_LEFT);
if (
ActiveInput(“RightKey”)) return(VK_RIGHT);
return(0);
}
void far OnDraw_MyWindow(CObject* pObj) //附加显示回调函数
{
CDialog*
pDialog = (CDialog *)pObj; //得到窗口指针
//显示一行文字
::ChPrintf(30,
40, RED, BLACK, 2, 0, “我爱祖国天安门”);
}
void far MyWindow()
{
CDialog*
pDialog = new CDialog; //分配实例
pDialog->CreateWindow(0,0,
300 ,200, “这是一个窗口”); //建立窗口
pDialog->Center();//窗口居中
new
CcloseButton(pDilaog);
pDialog->SetKeyboardFc((int far
(*pFc)(CObject*))MyKeyboard_01); //向窗口注册附加键盘回调函数
pDialog->SetDrawFc((int far
(*pFc)(CObject*))OnDraw_MyWindow); //向窗口注册显示回调函数
pDialog->ShowWindow();//显示窗口
pDialog->DoModal();//进入窗口消息循环
DELETE(pDialog);//删除实例
return;//函数返回
}