如何取得FormView指针,送28分,救急,help me!!! thanks!

ljan 2001-08-24 02:49:52
我在程序中用了一个CSizingControlBar,是一个人编写的控件,从CControlBar派生的,
他把一个FormView放入这个控件,我用了后遇到一个问题,如下:

我在我的主程序CLEditView中需要取得CFormView *pView;
pView可是老取不到,为空,出现异常,
程序控件如下:
#define TViewBarBase CSizingControlBar

class CViewBar : public TViewBarBase
{
DECLARE_DYNAMIC(CViewBar);
public:
CViewBar();
virtual ~CViewBar();
virtual BOOL Create(
CWnd* pParentWnd, // mandatory
CRuntimeClass *pViewClass, // mandatory
CCreateContext *pContext = NULL,
LPCTSTR lpszWindowName = NULL,
DWORD dwStyle = WS_CHILD | WS_VISIBLE | CBRS_TOP,
UINT nID = AFX_IDW_PANE_FIRST);
CCreateContext m_Context;

protected:
CFrameWnd *m_pFrameWnd;

// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CViewBar)
public:
//}}AFX_VIRTUAL

//{{AFX_MSG(CViewBar)
afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
afx_msg void OnSize(UINT nType, int cx, int cy);
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};

#endif

// ViewBar.cpp: implementation of the CViewBar class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "ViewBar.h"

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

IMPLEMENT_DYNAMIC(CViewBar, TViewBarBase);

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CViewBar::CViewBar()
{
ZeroMemory(&m_Context, sizeof(m_Context));
// Create a frame object.
CRuntimeClass *pRuntimeClass = RUNTIME_CLASS(CFrameWnd);
CObject* pObject = pRuntimeClass->CreateObject();
ASSERT( pObject->IsKindOf( RUNTIME_CLASS( CFrameWnd ) ) );
m_pFrameWnd = (CFrameWnd *)pObject;
}


CViewBar::~CViewBar()
{
}


BEGIN_MESSAGE_MAP(CViewBar, TViewBarBase)
//{{AFX_MSG_MAP(CViewBar)
ON_WM_CREATE()
ON_WM_SIZE()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CViewBar message handlers

/* ---------------------------------------------------------------
Overridden to set the view class before calling the
base create function. Note the other version of the create
function is "obsolete" so I don't support it.
For an SDI application the main frame window is created
when the document template is created and a valid
CreateContext is passed through (hurray!). Meaning there is
no problem creating the ViewBar in the frame window create.
However, for an MDI application the main frame window is
constructed outside the document creation, so the
CreateContext isn't present. In this case you can either
create and setup a CreateContext object with the desired
characteristics (doc template, frame window, etc.) and pass
it, or let the CViewBar::Create() create a CreateContext
with only the runtime class of the view, and the main frame
window set.
--------------------------------------------------------------- */
BOOL CViewBar::Create(
CWnd* pParentWnd,
CRuntimeClass *pViewClass,
CCreateContext *pContext,
LPCTSTR lpszWindowName,
DWORD dwStyle,
UINT nID)
{
ASSERT(pViewClass != NULL);
ASSERT(pViewClass->IsDerivedFrom(RUNTIME_CLASS(CWnd)));
if (pContext)
memcpy(&m_Context, pContext, sizeof(m_Context));
else {
CFrameWnd *fw = (CFrameWnd *)AfxGetMainWnd();
if (fw) {
// cc.m_pCurrentDoc = fw->GetActiveDocument();
m_Context.m_pCurrentFrame = fw;
}
}
m_Context.m_pNewViewClass = pViewClass;

return TViewBarBase::Create(
lpszWindowName,
pParentWnd,
nID,
dwStyle);
}


/* ---------------------------------------------------------------
Create the frame window associated with the view bar.
--------------------------------------------------------------- */
int CViewBar::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (TViewBarBase::OnCreate(lpCreateStruct) == -1)
return -1;

if (!m_pFrameWnd->Create(NULL, NULL,
WS_CHILD | WS_CLIPCHILDREN | WS_VISIBLE,
CRect(0,0,0,0), this, NULL, 0,
&m_Context))
return -1;
return 0;
}


/* ---------------------------------------------------------------
Must remember to move the frame window as well...
--------------------------------------------------------------- */
void CViewBar::OnSize(UINT nType, int cx, int cy)
{
CRect rc;

TViewBarBase::OnSize(nType, cx, cy);
GetClientRect(rc);
m_pFrameWnd->MoveWindow(rc);
}

我在CMainFrame创建控件,FormView也在控件里面,我试过不少办法,可就是取不到,

help me?thank you!!!
...全文
234 21 打赏 收藏 转发到动态 举报
写回复
用AI写文章
21 条回复
切换为时间正序
请发表友善的回复…
发表回复
ljan 2001-09-03
  • 打赏
  • 举报
回复

m_Context.m_pCurrentDoc = fw->GetActiveDocument();
你加的这一段代码,原先我也试过了,我现在也懂它的意思,不过不知道为什么原先我取不到那个指针,hehe

控件作者给我一段代码,你看看:
Get a pointer to the view.
--------------------------------------------------------------- */
CWnd *CViewBar::GetView() const
{
ASSERT_VALID(this);

CWnd* pView = m_pFrameWnd->GetDlgItem(AFX_IDW_PANE_FIRST);
ASSERT(pView != NULL);
return pView;
}


蒋晟 2001-08-30
  • 打赏
  • 举报
回复
这是我修改过的viewbar
// ViewBar.cpp: implementation of the CViewBar class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "ViewBar.h"

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

IMPLEMENT_DYNAMIC(CViewBar, TViewBarBase);

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CViewBar::CViewBar()
{
ZeroMemory(&m_Context, sizeof(m_Context));
// Create a frame object.
CRuntimeClass *pRuntimeClass = RUNTIME_CLASS(CFrameWnd);
CObject* pObject = pRuntimeClass->CreateObject();
ASSERT( pObject->IsKindOf( RUNTIME_CLASS( CFrameWnd ) ) );
m_pFrameWnd = (CFrameWnd *)pObject;
}


CViewBar::~CViewBar()
{
}


BEGIN_MESSAGE_MAP(CViewBar, TViewBarBase)
//{{AFX_MSG_MAP(CViewBar)
ON_WM_CREATE()
ON_WM_SIZE()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CViewBar message handlers

/* ---------------------------------------------------------------
Overridden to set the view class before calling the
base create function. Note the other version of the create
function is "obsolete" so I don't support it.
For an SDI application the main frame window is created
when the document template is created and a valid
CreateContext is passed through (hurray!). Meaning there is
no problem creating the ViewBar in the frame window create.
However, for an MDI application the main frame window is
constructed outside the document creation, so the
CreateContext isn't present. In this case you can either
create and setup a CreateContext object with the desired
characteristics (doc template, frame window, etc.) and pass
it, or let the CViewBar::Create() create a CreateContext
with only the runtime class of the view, and the main frame
window set.
--------------------------------------------------------------- */
BOOL CViewBar::Create(
CWnd* pParentWnd,
CRuntimeClass *pViewClass,
CCreateContext *pContext,
LPCTSTR lpszWindowName,
DWORD dwStyle,
UINT nID)
{
ASSERT(pViewClass != NULL);
ASSERT(pViewClass->IsDerivedFrom(RUNTIME_CLASS(CWnd)));
if (pContext){
memcpy(&m_Context, pContext, sizeof(m_Context));
}
else {
CFrameWnd *fw = (CFrameWnd *)AfxGetMainWnd();
if (fw) {
m_Context.m_pCurrentDoc = fw->GetActiveDocument();
m_Context.m_pCurrentFrame = fw;
}
}
m_Context.m_pNewViewClass = pViewClass;
return TViewBarBase::Create(
lpszWindowName,
pParentWnd,
nID,
dwStyle);
}


/* ---------------------------------------------------------------
Create the frame window associated with the view bar.
--------------------------------------------------------------- */
int CViewBar::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (TViewBarBase::OnCreate(lpCreateStruct) == -1)
return -1;

if (!m_pFrameWnd->Create(NULL, NULL,
WS_CHILD | WS_CLIPCHILDREN | WS_VISIBLE,
CRect(0,0,0,0), this, NULL, 0,
&m_Context))
return -1;
return 0;
}


/* ---------------------------------------------------------------
Must remember to move the frame window as well...
--------------------------------------------------------------- */
void CViewBar::OnSize(UINT nType, int cx, int cy)
{
CRect rc;

TViewBarBase::OnSize(nType, cx, cy);
GetClientRect(rc);
if(::IsWindow(m_pFrameWnd->GetSafeHwnd()))
m_pFrameWnd->MoveWindow(rc);
}
蒋晟 2001-08-27
  • 打赏
  • 举报
回复
118762
ljan 2001-08-27
  • 打赏
  • 举报
回复
我在CViewBar的OnCreate()加了几行代码:如下
BOOL CViewBar::Create(
CWnd* pParentWnd,
CRuntimeClass *pViewClass,
CCreateContext *pContext,
LPCTSTR lpszWindowName,
DWORD dwStyle,
UINT nID)
{
ASSERT(pViewClass != NULL);
ASSERT(pViewClass->IsDerivedFrom(RUNTIME_CLASS(CWnd)));
if (pContext)
memcpy(&m_Context, pContext, sizeof(m_Context));
else {
CFrameWnd *fw = (CFrameWnd *)AfxGetMainWnd();
if (fw) {
// cc.m_pCurrentDoc = fw->GetActiveDocument();
m_Context.m_pCurrentFrame = fw;
}
}
m_Context.m_pNewViewClass = pViewClass;
// m_Context.m_pCurrentDoc=((CFrameWnd *)AfxGetMainWnd())->GetActiveDocument();
m_Context.m_pCurrentDoc=m_pFrameWnd->GetActiveDocument();

return TViewBarBase::Create(
lpszWindowName,
pParentWnd,
nID,
dwStyle);
}

但无法取到我需要的应用程序文档指针,最上一行注释是原控件作者加的
ljan 2001-08-27
  • 打赏
  • 举报
回复
我看了一下,你的意思是不是这样?
CCreateContext m_Context;
m_Context.m_pCurrentDoc=当前文档;
然后就会文档和视自动建立连接了。。。

非常感谢你这些天对我的帮助,谢谢,能在网上碰见你,我非常的高兴

我的qq 是66442318,可告诉我,你的吗
ljan 2001-08-26
  • 打赏
  • 举报
回复
m_pDocument是当前文档指针的意思吧,是应该m_pDocument!=NULL,才能AddView吧
ljan 2001-08-26
  • 打赏
  • 举报
回复
m_pDocument是当前文档指针的意思吧,是应该m_pDocument!=
ljan 2001-08-26
  • 打赏
  • 举报
回复
为建立文档和视图的连接,在FormView中调用pDoc->AddView(this),来建立连接,

但如何取到pDoc,因为FormView还没有和文档建立连接,用GetDocument()取不到,

若通过另一个View来GetDocument,又需要取得另一个View的指针pAnotherView,是不是麻烦了一些?
蒋晟 2001-08-26
  • 打赏
  • 举报
回复
是啊
只要你指定了CreateContext的m_pDocument,它自己就会AddView。
蒋晟 2001-08-26
  • 打赏
  • 举报
回复
随便。
注意只有pView->m_pDocument=NULL时才能AddView(pView)
ljan 2001-08-25
  • 打赏
  • 举报
回复
在哪个函数中添加呢,在initialupdate()中可以吧
ljan 2001-08-25
  • 打赏
  • 举报
回复
让我试试,是在FormView.cpp中添加吗
蒋晟 2001-08-25
  • 打赏
  • 举报
回复
CDocument::AddView这个函数是建立文档/视图之间的联系,由系统自动调用。


蒋晟 2001-08-25
  • 打赏
  • 举报
回复
跟一下看有没有进到CDocument::AddView啦,我这个代码可以多个视图共享Document对象的。
ljan 2001-08-25
  • 打赏
  • 举报
回复
to jiangsheng:

CDocument::AddView应该怎么加,可以具体一点吗:
我的e-mail:ljan2001@sina.com

可以给一个具体相关的代码吗,very thank you!!!
ljan 2001-08-24
  • 打赏
  • 举报
回复
此问题我已经通过其他方法解决了,谢谢大家,要是其他人知道其他方法,告诉我,我同样会给分的

(我在应用程序app中加了一个FormView指针,然后,在FormView构造中把this赋给了app中指针)
ljan 2001-08-24
  • 打赏
  • 举报
回复
to jiangsheng:

我建的类CEagleView派生于CFormView,不知道它是不是默认和我的程序文档进行连接,
在CLEditApp中的Initinstance()中只有 pDocTemplate = new CMultiDocTemplate(
IDR_LEDITTYPE,
RUNTIME_CLASS(CLEditDoc),
RUNTIME_CLASS(CChildFrame), // custom MDI child frame
RUNTIME_CLASS(CLEditView));
AddDocTemplate(pDocTemplate);
不知道需不需要添加一个文档模板以建立CEagleView和CLEditDoc连接,这方面我理解的很浑,

你的方法是可以把视加入到控件中,这个我已经实现了,但在取得CEagleView *pView指针时
CEagleView* CLEditView::GetView()
{
CLEditDoc *pDoc=(CLEditDoc *)GetDocument();
if(pDoc){

POSITION pos=pDoc->GetFirstViewPosition();
CView* pView;
while (pos != NULL)
{
pView = pDoc->GetNextView(pos);
if (pView->IsKindOf(RUNTIME_CLASS(CEagleView)))
return (CEagleView *)pView;
}
}
return NULL;
}

取不到CEagleView指针,是什么原因,首先谢谢你,help me?
ljan 2001-08-24
  • 打赏
  • 举报
回复
to xiezhsh:
pFrame=(CMainFrame *)::AfxGetMainWnd();
pView=(CEagleView *)(pFrame->m_wndMyBar.GetDlgItem(AFX_IDW_PANE_FIRST+32));

pView=(CEagleView *)(pFrame->m_wndMyBar.GetDlgItem(IDD_EAGLE));
都不行
蒋晟 2001-08-24
  • 打赏
  • 举报
回复
int CCardFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CMDIChildWnd::OnCreate(lpCreateStruct) == -1)
return -1;

// TODO: Add your specialized creation code here
MDICREATESTRUCT* lpmcs=(MDICREATESTRUCT*)lpCreateStruct->lpCreateParams;
CCreateContext* pContext = (CCreateContext*)lpmcs->lParam;
if(!m_wndDeptViewBar.Create(this, RUNTIME_CLASS(CCWDeptTreeView),pContext,"部门栏",
WS_CHILD | WS_VISIBLE | CBRS_RIGHT|CBRS_GRIPPER,AFX_IDW_CONTROLBAR_FIRST + 33 )){
TRACE0("Failed to create dept bar\n");
return -1; // fail to create
}
m_wndDeptViewBar.SetSCBStyle(SCBS_SIZECHILD);
蒋晟 2001-08-24
  • 打赏
  • 举报
回复
GetDocument,然后遍历所有视图。
加载更多回复(1)

16,551

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • AIGC Browser
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

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