如何在SDI应用程序的对话框类中得到文档类或者视图类的指针?
如何在SDI应用程序的对话框类中得到文档类或者视图类的指针?(每个问题20分)谢谢! 问题点数:40、回复次数:8Top
1 楼rushing(勇敢的心)回复于 2002-10-23 11:17:23 得分 8
需要自定义,在析构函数中赋值或者另外建立函数赋值。
例如,GetDocument,GetViewTop
2 楼nanjianhui(nan)回复于 2002-10-23 11:17:54 得分 8
GetActiveView(); //get view pointer
Top
3 楼guangde(光光)回复于 2002-10-23 11:24:26 得分 0
能否详细点?我用了下面的代码:((CMainFrame*)(AfxGetApp()->m_pMainWnd))->GetActiveView,但得到的不是和文档类相连的视图类的指针。Top
4 楼sunshinefl(NeverMind)回复于 2002-10-23 11:42:20 得分 8
看看这个 相信你会豁然开朗的 呵呵
How to Get Current CDocument or CView from Anywhere
From Microsoft Knowledge Base (Article ID: Q108587)
One of the cases where you might need a pointer to the currently active view or document is in a modal or modeless dialog box. Generally, a dialog box should be created by the view class, because the view is what deals with the application's user interface.
Because the view class is creating the dialog box, it can pass a pointer to itself, or the active document [obtained with the GetActiveDocument() function] to the dialog box. This could be done through the dialog box's constructor or some other member function. For modal dialog boxes, the view could also put data from the dialog box into the document when DoModal() returns.
These methods are generally preferable to relying on generic functions to return pointers to the currently active view or document.
To allow you to get a pointer to the currently active document from anywhere in the program, add a static member function to your CDocument derived class as follows:
Edit the document's header file as follows to add a static member function, GetDoc():
// Document header file
class CMyDoc : public CDocument
{
...
public:
static CMyDoc * GetDoc();
...
};
For a single document interface (SDI) application, add the following code to your SDI document's implementation file for CMyDoc::GetDoc():
// SDI document implementation file
CMyDoc * CMyDoc::GetDoc()
{
CFrameWnd * pFrame = (CFrameWnd *)(AfxGetApp()->m_pMainWnd);
return (CMyDoc *) pFrame->GetActiveDocument();
}
For an SDI application, add the following code to your SDI view's implementation file for CMyView::GetView():
// View implementation file
CMyView * CMyView::GetView()
{
CFrameWnd * pFrame = (CFrameWnd *)(AfxGetApp()->m_pMainWnd);
CView * pView = pFrame->GetActiveView();
if ( !pView )
return NULL;
// Fail if view is of wrong kind
// (this could occur with splitter windows, or additional
// views on a single document
if ( ! pView->IsKindOf( RUNTIME_CLASS(CMyView) ) )
return NULL;
return (CMyView *) pView;
}
Top
5 楼guangde(光光)回复于 2002-10-23 16:59:34 得分 0
试了,好像不行!没人能说详细点吗?再加100分!!!!Top
6 楼sunshinefl(NeverMind)回复于 2002-10-23 18:19:54 得分 8
应该可以 我用过,先在Document header file中添加静态成员函数
static CMyDoc * GetDoc();
在实现文件中添加
CMyDoc * CMyDoc::GetDoc()
{
CFrameWnd * pFrame = (CFrameWnd *)(AfxGetApp()->m_pMainWnd);
return (CMyDoc *) pFrame->GetActiveDocument();
}
然后需要Document指针时,调用CMyDoc *pDoc= CMyDoc::GetDoc();就可以了!
视图指针同理!也是先定义函数,在写实现Top
7 楼andy_lau(天行键,君子当自强不息!)回复于 2002-10-23 18:35:15 得分 8
GetActiveView();Top
8 楼guangde(光光)回复于 2002-10-24 09:17:51 得分 0
已经解决了,不过没有用各位的办法,不过还是要谢谢家!给分!Top




