mfc如何在文档cxxx类或框架cmainframe类中获得视类cmyviewHow d.docx
- 文档编号:15168151
- 上传时间:2023-07-02
- 格式:DOCX
- 页数:7
- 大小:16.61KB
mfc如何在文档cxxx类或框架cmainframe类中获得视类cmyviewHow d.docx
《mfc如何在文档cxxx类或框架cmainframe类中获得视类cmyviewHow d.docx》由会员分享,可在线阅读,更多相关《mfc如何在文档cxxx类或框架cmainframe类中获得视类cmyviewHow d.docx(7页珍藏版)》请在冰点文库上搜索。
mfc如何在文档cxxx类或框架cmainframe类中获得视类cmyviewHowd
mfc如何在文档(cxxxdoc)类或框架(cmainframe)类中获得视类(cmyview(HowdoesMFCgetavisualclassinthedocument(cxxxdoc)classorCMainFrameclass(cmyview))
HowdoesMFCgetavisualclassinthedocument(CXXXDoc)classorCMainFrameclass(CMyView.txt)
HowdoesMFCgettheviewclass(CMyView)pointerinthedocument(CXXXDoc)classortheframework(CMainFrame)class?
Inthedocumentclass,justusethe(multidocument,singledocument,alittlesimpler)
POSITION(pos=this->GetFirstViewPosition);//towriteinthedocumentclass,toreplacethisdocumentpointercanbeinotherclasses
While(POS)
{
CView*pView=this->GetNextView(POS);
}
Intheframeworkclass,justselectthedocumentobject,andthenusethecodeabove
Here'sanaspectofgettinganotherobjectfromanobject(excerptfromMFC)
Themethodofobtaininganotherobjectfromanobject
Thememberfunctionsusedbytheobjecttobeobtainedbythisobject
CDocumentobjectviewlistGetFirstViewPosition,GetNextView
DocumenttemplateGetDocTemplate
CViewobjectdocumentobjectGetDocument
BorderwindowGetParentFrame
CMDIChildWndor
CFrameWndobjectactivityviewGetActiveView
ActivevisualdocumentGetActiveDocument
CMDIFrameWndobject,activedocument,borderwindow,MDIGetActive
UseGetActiveView()intheframeworkclass;
Inthedocumentclass(CMainFrame*)(AfxGetApp()->m_pMainWnd))->GetActiveView();
HowdoesMFCgetthepointertotheViewclassunderCMainFrame?
UsingtheGetActiveView()function
HowdoordinaryclassesinMFCgetpointerstotheviewclass?
HowdoIgetpointerstotheviewclassintheclass(Generic)?
I'vetriedthat:
CMainFrame*p=(CMainFrame*)AfxGetMainWnd();
CView,*pv=p->GetActiveView();
Accordingtoyourusage,that'sallright!
IsthistheViewthathasn'tbeencreatedyet,soPVisempty?
.
CMainFrame*m_pfm=(CMainFrame*)AfxGetApp()->m_pMainWnd;
...*m_pView=(*)m_pfm->GetActiveView();
Onelessword:
AfxGetApp()
CMainFrame*p=(CMainFrame*)AfxGetApp()->m_pMainWnd;
CView*pv=(CView*)p->GetActiveView();
Upstairsinchargeofbuildinganalysis,asequenceoffunctioncalls,notwhatisPreCreateWindoworCXXXApp,theconstructoroftheCXXXFrameInitInstance,themainwindowtemplatedidnotfinishforthemanipulationoftheCView.
Asdfasdf
HowdoIreturnapointertoaclassinMFC,suchasCWIN,view,andsoon!
IntheCViewclass,callthis->GetDocument()togetdoc.
IntheCDocumentclass,callGetFirstViewPosition()andGetNextView()togetalltheview.
Inotherclasses,onemethodistopassthepointerinforthecalltouse.
Also,yougetCWinAppwithAfxGetApp(),thenGetFirstDocTemplatePosition()andGetNextDocTemplate()togetthedoctemplate,andthencallGetFirstDocPosition()andGetNextDoc()togetthedoc.
HowdoIgetthepointeroftheViewclass(this)orthepointerofthewholedialogboxintheOnDrawfunctionorthe***Dlgfunction?
Inasingledocument,youcanusethefollowingmethods:
Thefilewhereyouwanttogetthepointernowcontainstwofilesatthestart:
Viewofthemainframeclassheaderfile,thatis,#include"MainFrm.h""
Viewclassheaderfile,thatis,#include"XXView.h""
Thenaddthefollowingstatementatthepointwhereyouwanttoaccessthem_pSet
CMainFrame*frm=(CMainFrame*)AfxGetApp()->m_pMainWnd;
CXXView*pView=(CXXView*)frm->GetActiveView();
PView,thatis,theViewclasspointer
Butisn'titeasierforyoutousethis?
InthesingledocumentofMFC,theviewwindowhandleproducedbyCtestviewisobtainedfromthetreeviewclassofaview
Getsthewindowhandleoftheviewinthesingledocumentframework.Askforapieceofcode.Checkonline,didnotgetitout.Whichbrotherhastimetowriteaparagraph?
.
You'renotclear.Therearemanywaystocarryitout.
YoucangettheviewpointerthroughtheglobalobjecttheApp,andthen
Viewpointer->GetSafeHwnd();getsthehandle.
Youcanalsosendcustommessagestosendthehandletothespecifiedwindowclass.
YoucanalsodefineaCviewpointerinyourstdafx.h,whichpointstotheViewclassduringtheinitializationprocess,andisusedinyourtreeview.
Youcanalsoreceiveahandleforthetreeviewclassdefinitionmethodbyitself,whencalled.
Notethatyoucanhandlethecreationsequence,orelseyouwillmakeanassertionerrorunderdebug.
Themethodisreallyalot.
Ifyoudonotusethedocument/viewstructure,thesingledocumentiscreatedinthebuildframewindow.Thereisavariablecalledm_wndView.Thisisthehandle.He'saCWndderivative.
Ifyouusethedocument/viewstructure,directGetActiveViewisfine.
Thereisonemorecase,ifyouusethesplitwindow.......
Whichfunctionhasbeenforgotten?
.Get***Paneseems..Checkit.
HowistheinitialtitleoftheviewwindowproducedbytheMFCsingledocumentCtestview?
Youcanmodifythecharacterfileintheresourcefile,specificallyhowtomodifytheInternetsearch,
AnothermethodistojoinreturninthePreCreateWindowfunctionoftheviewclass
M_strTitle=singledocumentheader";
Youstartedanotherthread,CMyApp.HowdoyouaccessmyViewclassinCMyApp?
IfIweretoregenerateaWinAppclassandcallAfxBeginThread((RUNTIME_CLASS)CMyApp)tostartanotherthread,thenhowdoIaccessmyViewclassinCMyApp?
Iaskedthequestioninthemorning,butnoonegavemetheanswer.Amastercanenlightenah
1.YoucanpassintheclassofthisViewasaparameter,whichistheeasiestsolution
2.
Youcangetitinthefollowingway
CWinApp*pA=AfxGetApp();
POSITION,pos=pA->GetFirstDocTemplatePosition();
CDocTemplate*pDT=pA->GetNextDocTemplate(POS);
Pos=pDT->GetFirstDocPosition();
CDocument*pD=pDT->GetNextDoc(POS);
Pos=pD->GetFirstViewPosition();
CView*pV=pD->GetNextView(POS);
Ofcourse,theprincipleisthatifyouhavemultipleView,orhaveDoc,orevenmultipleDocTemplate,youcanadjustitappropriately
Thesourcecode,Itried,andshouldbenoproblem
HowdoestheMFCsingledocumentgetahandletothewindowoftheViewview(thewindowproducedbytheCmyViewclassinstance)?
Inanotherviewclass
GetthroughtheAppclass
Headerfiledefinition:
CmyView,*GetmyView();
Realization:
CmyView*CmyApp:
:
GetmyView()
{
If(m_pDocManager)
{
POSITIONpos1=m_pDocManager->GetFirstDocTemplatePosition();
While(pos1)
{
CDocTemplate*pDocTemplate=m_pDocManager->GetNextDocTemplate(pos1);
If(pDocTemplate)
{
POSITIONpos2=pDocTemplate->GetFirstDocPosition();
While(pos2)
{
CDocument*pDoc=pDocTemplate->GetNextDoc(pos2);
If(pDoc)
{
POSITIONpos3=pDoc->GetFirstViewPosition();
While(pos3)
{
CView*pView=pDoc->GetNextView(pos3);
If(pView)
{
If(pView->IsKindOf(RUNTIME_CLASS(CmyView))
Return(CmyView*)pView;
}
}
}
}
}
}
}
ReturnNULL;
}
Addapoint,ifyouareasplitviewgeneratedbystaticsegmentation,canmakefulluseoftheGetPanefunction,suchasCSplitterWnd,CmyViewtogetthewindowhandleinCmylView(assumingCmyViewinsegmentationof0rowsand0columns):
CSplitterWnd*ps=(CSplitterWnd*)GetParent();
CmyView*pView=(CmyView*)(ps->GetPane(0,0));
PView->m_hWndgetsthehandletotheviewform
Simple:
becauseyouhavemorethanoneclassofview,onlythehandleofthewindowismeaningfulonlywhenyougettheViewview(thewindowproducedbytheCmyViewclassinstance)whenyourviewclassisactive!
Aslongasafunctioncanbesolved.
CView*GetActiveView()const;
GetActiveView()getsthecurrentactiveviewclasswindowhandle
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- mfc如何在文档cxxx类或框架cmainframe类中获得视类cmyviewHow mfc 如何 文档 cxxx 框架 cmainframe 获得 cmyviewHow