
本代码可以在CFormView中,根据事先画好的控件位置创建CScrollView
也可以在CDialog中创建CScrollView、CFormView等
注:
若以下代码放在CMainRightView::OnCreate(LPCREATESTRUCT lpCreateStruct)内,则GetDlgItem()函数将调用失败,因为此时控件都还未被创建!
voidCMainRightView::OnInitialUpdate()
{
CFormView::OnInitialUpdate();
//TODO:Addyourspecializedcodehereand/orcallthebaseclass
//获得目标位置控件
UINTTargetCtrlID=IDC_STATIC_SCROLLVIEW;
CWnd*pWnd=this->GetDlgItem(TargetCtrlID);
CRectRectTargetCtrl;
pWnd->GetWindowRect(RectTargetCtrl);
pWnd->DestroyWindow();
this->ScreenToClient(RectTargetCtrl);
//在目标位置动态创建CScrollView
CEMapView*pEMapView=(CEMapView*)RUNTIME_CLASS(CEMapView)->CreateObject();
pEMapView->Create(NULL,NULL,AFX_WS_DEFAULT_VIEW,RectTargetCtrl,this,TargetCtrlID);
//使用CreateView创建的视图不能自动调用OnInitialUpdate函数,需要人工调用OnInitialUpdate函数或者发送WM_INITIALUPDATE消息
pEMapView->OnInitialUpdate();
//SetScrollSizes()必须被调用,否则运行时会出ASSERT错误,当然,也可以在目标View内的OnInitialUpdate()中调用
pEMapView->SetScrollSizes(MM_TEXT,CSize(RectTargetCtrl.Width()-10,RectTargetCtrl.Height()-10));
//使用CreateView创建的视图不会自动显示并且激活,需要人工操作
pEMapView->ShowWindow(SW_SHOW);
}
注:如果需要在CDialog中创建CScrollView、CFormView,则需要在View中overload、override这些View中以下的4个方法,否则会出ASSERT错误
afx_msgintOnMouseActivate(CWnd*pDesktopWnd,UINTnHitTest,UINTmessage);
afx_msgvoidOnDestroy();
virtualvoidPostNcDestroy();
virtualvoidOnActivateFrame(UINTnState,CFrameWnd*pDeactivateFrame);
intCFormViewPrint::OnMouseActivate(CWnd*pDesktopWnd,UINTnHitTest,UINTmessage)
{
//TODO:Addyourmessagehandlercodehereand/orcalldefault
本文来自电脑杂谈,转载请注明本文网址:
http://www.pc-fly.com/a/jisuanjixue/article-32922-1.html
到天亮都招呼不完