读书人

atl控件里创建单文档,该怎么解决

发布时间: 2012-03-19 22:03:05 作者: rapoo

atl控件里创建单文档
我想通过createview创建出来一个单文档,但是得到文档对象为空,大家给点意见啊
[code=C/C++][/code]
class CFramePrint : public CFrameWnd
class CDjcxView : public CScrollView
class CDjcxDoc : public COleDocument

STDMETHODIMP CPrintClient::CreateView(LONG* nOK)
{
AFX_MANAGE_STATE(AfxGetStaticModuleState());

// TODO: 在此添加实现代码
*nOK = 0;

m_pF=new CFramePrint;

CWnd* pWnd=CWnd::FromHandle(m_wndParent);
m_sCaption = "打印预览";

if (!m_pF->CreatePrintView(NULL,m_sCaption))
{
::MessageBox(NULL,"创建打印视图失败!","严重错误",MB_ICONHAND);
delete m_pF;
m_pF=NULL;
return S_FALSE;
}

*nOK = (LONG)(m_pF->m_hWnd);

return S_OK;
}

CWnd* CFramePrint::CreatePrintView(CWnd* pParentWnd,CString sTitle)
{
ASSERT(this);

HICON hIcon=g_WinApp->LoadIcon(IDI_FRAMEPRINT1);

LPCTSTR lpWndCls= AfxRegisterWndClass(CS_DBLCLKS,g_WinApp->LoadCursor(IDC_ARROW),HBRUSH(::GetStockObject(WHITE_BRUSH)),hIcon);


if (!LoadFrame(IDR_FRAMEPRINT,WS_OVERLAPPEDWINDOW|FWS_ADDTOTITLE,pParentWnd))
return NULL;

ShowWindow(SW_SHOW);

CView* pOldActiveView = GetActiveView();
if (pOldActiveView)
::SetWindowLong(pOldActiveView->m_hWnd, GWL_ID,0);


CRuntimeClass* pNewViewClass;
pNewViewClass= RUNTIME_CLASS(CDjcxView);

// create the new view
CCreateContext context;
context.m_pNewViewClass = pNewViewClass;
context.m_pCurrentDoc = GetActiveDocument();
CView* pNewView = STATIC_DOWNCAST(CView, CreateView(&context));
if (pNewView != NULL)
{
// the new view is there, but invisible and not active...
pNewView->ShowWindow(SW_SHOW);
pNewView->OnInitialUpdate();
SetActiveView(pNewView);
RecalcLayout();

// finally destroy the old view...
if (pOldActiveView)
pOldActiveView->DestroyWindow();
}


m_pDjcxView=(CDjcxView*)pNewView;

m_pDjcxView->m_pFrame=this;


SetToolBar();
SetWindowText(sTitle);

return this;
}

CView* pOldActiveView = GetActiveView();
context.m_pCurrentDoc = GetActiveDocument();
这两句得到的都是NULL,不知道为什么


[解决办法]
GetActiveView、GetActiveDocument窗口在激活状态才行,调试的话肯定不行的
[解决办法]
OCX中嵌入文档视图可以参看下这篇文章.
http://air-head.blogbus.com/logs/43811528.html

读书人网 >VC/MFC

热点推荐