在视图中取CmainFrame变量和对象

jyxy_ljw 2003-04-22 04:59:17
我有一个DOC类多个VIEW类

有下定义:pubic: CAdoConnection m_adoConnection;

如果放在 DOC中,在其他VIEW中,不能用 GetDocument->adoConnection 得到,
为什么?我想是多个VIEW类的原因。。。

如果我把定义:pubic: CAdoConnection m_adoConnection;
放在 CmainFrame类中,在其他VIEW中如何取呢?
我是: CmainFrame MyFrame;
MyFrame.adoConnection;
这样老觉得不方面,请问有没有其他方法?
...全文
40 19 打赏 收藏 转发到动态 举报
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhdleo 2003-04-23
  • 打赏
  • 举报
回复
RUNTIME_CLASS(class_name)
只要你写入你的class_name,
jyxy_ljw 2003-04-23
  • 打赏
  • 举报
回复
如何指定的RUNTIME_CLASS(class_name)的指针
coldbloodfox 2003-04-23
  • 打赏
  • 举报
回复
举一个最简单的例子以前的C++可以这么创建实力吗
CString clsStr="CMyClass";
class1=new clasStr;
但是如果用上边的机制就可以这么写了
jyxy_ljw 2003-04-23
  • 打赏
  • 举报
回复
有没有指针啊
coldbloodfox 2003-04-23
  • 打赏
  • 举报
回复
RUNTIME_CLASS(class_name)是动态创建宏用这个声明的类可以在程序运行中实例化
jyxy_ljw 2003-04-23
  • 打赏
  • 举报
回复
RUNTIME_CLASS(class_name)
是啥意思?
jyxy_ljw 2003-04-23
  • 打赏
  • 举报
回复
我发现我是一个傻B,明明人家写了,都没看见。。。。
ChinaShrimp 2003-04-23
  • 打赏
  • 举报
回复
GetActiveView()
jyxy_ljw 2003-04-23
  • 打赏
  • 举报
回复
context.m_pCurrentDoc = pOldView->GetDocument();

如何得到视图指针?
ChinaShrimp 2003-04-22
  • 打赏
  • 举报
回复
其他的view也很简单,当然是new出来呀!然后调用Create()方法,如果需要将它于某个documenmt关联起来,就用CCreateContext就搞定了。
例如:
p = new CTestView;
pOldView = GetActiveView();

CCreateContext context;
context.m_pCurrentDoc = pOldView->GetDocument();
p->Create(....., &context);
p->OnInitialUpdate();
webber84 2003-04-22
  • 打赏
  • 举报
回复
那其它的view你是怎么创建的呢?CCreateContext中的m_pCurrentDoc设置过了吗?
ChinaShrimp 2003-04-22
  • 打赏
  • 举报
回复
GetDocument()返回的是什么?m_pDocument!而m_pDocument在那里被初始化呢?他们之间的关系是怎样建立起来的呢?让我们来分析一下:
当WM_CREATE消息传来,将会调用CView::OnCreate(),这个是没有问题的?然后呢,
CCreateContext* pContext = (CCreateContext*)lpcs->lpCreateParams;
// A view should be created in a given context!
if (pContext != NULL && pContext->m_pCurrentDoc != NULL)
{
pContext->m_pCurrentDoc->AddView(this);
ASSERT(m_pDocument != NULL);
}
else
{
TRACE0("Warning: Creating a pane with no CDocument.\n");
}
从上面的代码可以看到,他会利用当前的pContext中保存的m_pCurrentDoc来添加一个view,对否?那我们来看看在AddView里头作了些什么手脚!
void CDocument::AddView(CView* pView)
{
......
ASSERT(pView->m_pDocument == NULL); // must be un-attached
pView->m_pDocument = this;

......
}
上面的代码很清楚的表明m_pDocument是在这里被初始化了,对否?
现在我们搞清楚了m_pDocument初始化的问题了,自然很明显如果你的view没有跟一个doc关联的话,那么呵呵!当然不能获得正确东西了。
如果在深入一点,系统如何得知doc和那个view关联,或者view没有跟doc关联呢?
关键在你的应用程序类的InitInstance()里头,请看:

BOOL CAsdafApp::InitInstance()
{
.....
CSingleDocTemplate* pDocTemplate;
pDocTemplate = new CSingleDocTemplate(
IDR_MAINFRAME,
RUNTIME_CLASS(CAsdafDoc),
RUNTIME_CLASS(CMainFrame), // main SDI frame window
RUNTIME_CLASS(CAsdafView));
AddDocTemplate(pDocTemplate);
.....
}
现在终于明白了,它是靠文档模版来管理的,对不?好了,这个问题应该解决啦!
jyxy_ljw 2003-04-22
  • 打赏
  • 举报
回复
GetDocument()->adoConnection; 也只能在主视图即向导生成的 VIEW中得到,为什么?
cadinfo 2003-04-22
  • 打赏
  • 举报
回复
呵呵,蹭分了
------------------
CMainFrame* pMF=(CMainFrame*)AfxGetApp()->GetMainWnd(); 试试看GetParent()得到什么?
GetDocument()->adoConnection;
webber84 2003-04-22
  • 打赏
  • 举报
回复
GetDocument()->adoConnection; //你没加括号?
jyxy_ljw 2003-04-22
  • 打赏
  • 举报
回复
如果放在 DOC中,在其他VIEW中,不能用 GetDocument->adoConnection 得到,
为什么?
zhdleo 2003-04-22
  • 打赏
  • 举报
回复
pMF->m_adoConnection
zhdleo 2003-04-22
  • 打赏
  • 举报
回复
1.在要使用的文件包含MainFrm.h文件

#include "MainFrm.h"

2.
CMainFrame* pMF=(CMainFrame*)AfxGetApp()->GetMainWnd();
pMF->m_wndToolBar ..... //类似
small_wei 2003-04-22
  • 打赏
  • 举报
回复
MyFrame *pWnd = (MyFrame * ) AfxGetMainWnd();

15,979

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 界面
社区管理员
  • 界面
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧