CCmdTarget
类CCmdTarget是MFC类库中消息映射体系的一个基类。消息映射把命令或消息引导给用户为之编写的响应函数(命令是由菜单项、命令按钮或者加速键产生的消息)。从CCmdTarget继承来的按键框架类包括:CView、CWinApp、CDocument、CWnd和CFrameWnd。如果想生成一个处理按键消息的类,可以选择其中的一个派生一个子类。很少需要直接从CCmdTarget派生类。相关的命令目标和OnCmdMsg例程的其它信息,请参阅联机文档“VisualC++程序员指南斨械摹懊钅勘辍薄ⅰ懊盥酚伞焙汀坝成湎ⅰ辈糠帧@郈CmdTarget包括了处理沙漏形光标显示的成员函数。当某个命令的执行时间比较长时,可以显示沙漏标提示用户命令正在执行。和消息映射类似,分派映射用于列出OLE自动的IDispatch功能。列出这个接口后,其它的应用(如VB)就能调用这个应用了。有关IDispatch接口的更详细的信息,请参阅“Win32 SDK OLE程序员参考”中的“创建IDPatch接口”和“分派接口与API函数”。 #include <afxwin.h> 请参阅 CCmdUI, CDocument, CDocTemplate, CWinApp,CWnd, CView,CFrameWnd, COleDispatchDriver
CCmdTarget类的成员 属性 FromDispatch返回一个指向CCmdTarget对象的指针,该对象与IDispatch相联系 GetDispatch返回一个指向IDispatch对象的指针,该对象与CCmdTarget对象相关 IsResultExpected如果自动函数要返回一个值,则返回非零值 操作 BeginWaitCursor显示沙漏标 EnableAutomation允许CCmdTarget 对象的OLE自动函数 EndWaitCursor返回到前一个光标 RestoreWaitCursor重置沙漏标 可覆盖的函数 OnCmdMsg分派命令消息 OnFinalRelease在最后一个OLE对象参考被释放时清除环境
成员函数 CCmdTarget::BeginWaitCursor void BeginWaitCursor( ); 说明 本函数用于显示沙漏标(通常在命令执行时间较长时采用)。框架调用本函数显示沙漏标,告诉用户系统忙,例如在加载一个CDocument对象或把它保存到文件时。在不是处理单个消息时,BeginWaitCursor可能不象其它函数那样有效,例如OnSetCursor的处理也能改变光标形状。调用函数EndWaitCursor可以恢复此前的光标。 示例
//
The following example illustrates the most common case
//
of displaying the hourglass cursor during some lengthy
//
processing of a command handler implemented in some
//
CCmdTaget-derived class,such as a document or view.
void
CMyView::OnSomeCommand( )

...
{
BeginWaitCursor( ); //display the hourglass cursor
//do some lengthy processing
EndWaitCursor( ); //remove the hourglass cursor
}
//
The next example illustrates RestoreWaitCursor
void
CMyView::OnSomeCommand( )

...
{
BeginWaitCursor( ); //display the hourglass cursor
//do some lengthy processing
// The dialog box will normally change the cursor to
// the standard arrow cursor,and leave the cursor in
// as the standard arrow cursor when the dialog box is
//closed.
CMyDialog dlg;dlg.DoModal( );
// It is necessary to call RestoreWaitCursor here in order
// to change the cursor back to the hourglass cursor.
RestoreWaitCursor( );
// do some more lengthy processing
EndWaitCursor( ); //remove the hourglass cursor
}
//
In the above example,the dialog was clearly invoked between
//
the pair of calls to BeginWaitCursor and EndWaitCursor.
//
Sometimes it may not be clear whether the dialog is invoked
//
in between a pair of calls to BeginWaitCursor and EndWaitCursor
.
//
It is permissable to call RestoreWaitCursor,even if
//
BeginWaitCursor was not previously called.This case is
//
illustrated below,where CMyView::AnotherFunction does not
//
need to know whether it was called in the context of an
//
hourglass cursor.
void
CMyView::AnotherFunction( )

...
{
// some processing
CMyDialog dlg;
dlg.DoModal( );
RestoreWaitCursor( );
//some more processing
}
//
If the dialog is invoked from a member function of
//
some non-CCmdTarget,the you call CWinApp::DoWaitCursor
//
with a 0 parameter value to restore the hourglass cursor.
void
CMyObject::AnotherFunction( )

...
{
CMyDialog dlg;
dlg.DoModal( );
AfxGetApp( )->DoWaitCursor(0); //same as CCmdTarget::RestoreWaitCursor
}
请参阅 CWaitCursor, CCmdTarget::EndWaitCursor, CCmdTarget::RestoreWaitCursor, CWinApp::DoWaitCursor
CCmdTarget::EnableAutomation void EnableAutomation( ); 说明 本函数设置对象的OLE自动功能。一般在对象的构造函数里调用。调用时要保证已经为类声明了分派映射。有关自动功能的更详细信息,请参阅联机文档“Visual C++程序员指南”中的” 自动客户”和“自动服务器” 请参阅 DECLARE_DISPATCH_MAP, DECLARE_OLECREATE
CCmdTarget::EndWaitCursor void EndWaitCursor( ); 说明 本函数在BeginWaitCursor之后调用。它用于撤消沙漏标,并恢复以前的光标。框架在调用沙漏标之后也调用本函数。 示例
//
The following example illustrates the most common case
//
of displaying the hourglass cursor during some lengthy
//
processing of a command handler implemented in some
//
CCmdTarget-derived class,such as a document or view.
void
CMyView::OnSomeCommand( )

...
{
BeginWaitCursor( ); // display the hourglass cursor
// do some lengthy processing
EndWaitCursor( ); // remove the hourglass cursor
}
//
The next example illustrates RestoreWaitCursor
void
CMyView::OnSomeCommand( )

...
{
BeginWaitCursor( ); // display the hourglass cursor
// do some lengthy processing
// The dialog box will normally change the cursor to
// the standard arrow cursor,and leave the cursor in
// as the standard arrow cursor when the dialog box is
// closed.
CMyDialog dlg;
dlg.DoModal( );
// It is necessary to call RestoreWaitCursor here in order
// to change the cursor back to the hourglass cursor.
RestoreWaitCursor( );
// do some more lengthy processing
EndWaitCursor( ); // remove the hourglass cursor
}
//
In the above example,the dialog was clearly invoked between
//
the pair of calls to BeginWaitCursor and EndWaitCursor.
//
Sometimes it may not be clear whether the dialog is invoked
//
in between a pair of calls to BeginWaitCursor and EndWaitCursor.
//
It is permissable to call RestoreWaitCursor,even if
//
BeginWaitCursor was not previously called.This case is
//
illustrated below,where CMyView::AnotherFunction does not
//
need to know whether it was called in the context of an
//
hourglass cursor.
void
CMyView::AnotherFunction( )

...
{
// some processing….
CMyDialog dlg;
dlg.DoModal( );
RestoreWaitCursor( );
// some more processing…
}
//
If the dialog is invoked from a member function of
//
some non-CCmdTarget,the you call CWinApp::DoWaitCursor
//
with a 0 parameter value to restore the hourglass cursor.
void
CMyObject::AnotherFunction( )

...
{
CMyDialog dlg;
dlg.DoModal( );
AfxGetApp( )->DoWaitCursor(0); // same as CCmdTarget::RestorWaitCursor
}
请参阅 CWaitCursor, CCmdTarget::BeginWaitCursor, CCmdTarget::RestoreWaitCursor, CWinApp::DoWaitCursor
CCmdTarget::FromIDispatch static CCmdTarget* FromIDispatch( LPDISPATCH lpDispatch ); 返回值 返回一个和lpDistpatch相关的CCmdTarget对象的指针。如果该IDistpatch对象不是一个MFC IDistpatch对象,则返回NULL。 参数 lpDispatch指向IDistpatch对象的指针。 说明 本函数把从类的自动成员函数中得到的一个IDistpatch指针映射到一个实现了IDistpatch对象接口的CCmdTarget对象。函数的结果与调用函数GetIDispatch的结果相反。 请参阅 CCmdTarget::GetIDispatch, COleDispatchDriver
CCmdTarget::GetIDispatch LPDISPATCH GetIDispatch( BOOL bAddRef ); 返回值 返回一个和该对象相关的IDistpatch指针。 参数 bAddRef指明是否增加对该对象的参考记数。 说明 本成员函数用于检索一个自动方法的IDistpatch指针,该自动方法返回一个IDistpatch指针或参考一个IDistpatch指针。对于那些在构造函数中调用了EnableAutomation的对象,本函数使它们自动激活,并返回一个指向IDistpatch在MFC中的实现的指针。该IDistpatch被那些通过IDistpatch接口通信的客户所使用。本函数的调用自动增加一个对该指针的参考,因而不需要调用IUnknow::AddRef。 请参阅 CCmdTarget::EnableAutomation,COleDispatchDriver,IUnknow::Release
CCmdTarget::IsResultExpected BOOL IsResultExpected( ); 返回值 如果自动函数要返回一个值,则返回非零值。否则为0。 说明 本函数确定客户是否期待从它对自动函数的调用中返回一个值。OLE接口告诉MFC客户是使用了还是忽略了调用返回值。MFC则使用这些信息决定IsResultExpected的返回值。如果返回值的计算比较耗费时间或其它资源,可以在计算返回值之前调用本函数以提高效率。本函数只返回一次0,这样,如果在一个已被客户端调用的自动函数中调用了其它的自动函数,就可以获得有效的返回值。如果在没有进行自动函数调用时调用本函数,将返回非零值。 请参阅 CCmdTarget::GETIDispatch, CCmdTarget::EnableAutomation CCmdTarget::OnCmdMsgvirtual BOOL OnCmdMsg( UINT nID, int nCode, void* pExtra,AFX_CMDHANDL-ERINF O*pHandlerInfo ); 返回值 如果消息被处理了,则返回非零值;否则为0。 参数 nID命令的ID。 nCode命令的通知代码。 PExtra根据nCode的值使用。 pHandlerInfo如果非空,OnCmdMsg将填充pHandlerInfo 结构的pTarget和pmf成员,而不是分派该命令。此参数通常为NULL。 说明 本函数由框架来调用,它分派命令并处理那些提供了命令用户接口的对象的更新。这是框架的命令体系中实现的一个主要例程。在运行时,OnCmdMsg把命令分派到其它对象上或者调用CcmdTarg-et::OnCmd Msg(此函数进行真正的消息映射查找)自己处理命令。有关这个缺省命令例程的完整描述,请参阅联机文档“Visual C++程序员指南”中的“消息处理”和“映射主题”部分。偶尔需要覆盖本函数以扩展MFC框架的标准命令例程。有关命令例程体系中的更多细节,请参阅联机文档中的“技术指南21”。 请参阅 CCmdUI
CCmdTarget::OnFinalRelease virtual void OnFinalRelease( ); 说明 本函数在对对象的最后一个OLE参考或对象对别人的后一个OLE参考被释放时,由框架调用。可以覆盖它以进行所需的处理。缺省的实现是删除该对象。 请参阅 COleServerItem
CCmdTarget::RestoreWaitCursorvoid RestoreWaitCursor( ); 说明 本函数用于在系统光标改变后重置沙漏标(例如,在一个耗时操作的过程中打开一个消息窗口而后又关闭它。) 请参阅 CWaitCusor, CCmdTarget:: EndWaitCursor, CCmdTarget::BeginWaitCursor, CWinApp::DoWaitCursor
|