 |
|
 |
I think we can use the global variable theApp instead of the following line:
CWinApp* pApp = (CWinApp*) AfxGetApp ();
|
|
|
|
 |
|
 |
if i Add another view class to my MDI App how can i Access the new view From App Class please help IBRAHIM
|
|
|
|
 |
|
 |
I just wrote a tut on that. It is in the doc/view section. Vote if you like it.
thx.
R.J.
|
|
|
|
 |
|
 |
Maybe that's obvious to everyone, but it took me half an hour to find out that you should include Doc and View H files in this order:
#include "MyView.h"
#include "MyDoc.h"
and not other way around.
Hope it helps,
Slawa.
|
|
|
|
 |
|
 |
Sorry!!! I mixed it up myself.
IT REALLY SHOULD BE LIKE THIS:
1. Doc
2. View
Slawa.
|
|
|
|
 |
|
 |
If its for the purpose of posting messages I'd do it this way.
In CMainFrame::OnCmdMsg(....)
{
add..
CSOFIDoc* pDoc = (CSOFIDoc*) GetActiveDocument();
if (pDoc != NULL) {
return pDoc->RouteCmdToAllViews (GetActiveView(), nID, nCode, pExtra, pHandlerInfo);
}
}
Then in your Document class add the function:
BOOL CSOFIDoc::RouteCmdToAllViews(CView *pView, UINT nID, int nCode, void *pExtra, AFX_CMDHANDLERINFO *pHandlerInfo)
{
POSITION pos = GetFirstViewPosition();
while (pos != NULL) {
CView * pNextView = GetNextView(pos);
if (pNextView != pView) {
if (pNextView->OnCmdMsg(nID,nCode,pExtra,pHandlerInfo))
return TRUE;
}
}
return FALSE;
}
Now you can create a custom message (eg. WM_MYMESSAGE) and post from anywhere in your application with SendMessage or PostMessage and the correct view will receive it. In other words you don't have to do stuff like:
CDocument * pDoc = GetDocument();
POSITION pos = pDoc->GetFirstViewPosition()
while(pos)
{
CView * pView = pos->GetNextView()
if (pView->IsKindOf(RUNTIME_CLASS(CMyView))
{
( (CMyView*) pView)->DoSomething()
}
}
Just my $0.002
|
|
|
|
 |
|
 |
Hey Brendan,
Can I ask you a question? I like your approach you have suggested and I think it will apply nicely to my particualr application. But, I am still uncertain.
My problem is this:
I have An SDI application with tabs, multiple views and the whole nine yards. My application needs the serial port for a communicaiton to a device. Several different views need to access the serial port.
Now I have found a great serial port library by Ramon de Klein. I am really new to the whole SDI/MDI way of how things work. I was consfused as to where I can place once instance of that port class and have the multiple views be able to access it and the Doc seemed like a good candidate since it is the once thing common to all views. I am not usre if this is the right approach. I mean I can only have the port opened once and have it actively waiting on data. Yet, I need to direct certain messages to certain views and allow them to use the port.
Any comment on how I would go about doing this? More specifically, where whould I open the port, collect messages and how could I send certain messages to certain views?
I appreciate any help you can give me
"DWORD my man!"
|
|
|
|
 |
|
 |
Why do you want to have access to your CView-class from anywhere in your application?
I don't know that much about Object Oriented Programming, but I think this is certainly not object oriented? (Although I can see that it is usefull sometimes). What exactly are the criteria when you decide between OOP/Usefullness?
--
Alex Marbus
|
|
|
|
 |
|
 |
I have used a similair technique before. It is useful when you do splitters and generally have more than one view/window/dialog on the screen and they all need to communicate..An explorer type app is a good example. Left view is a tree which communicates to the right view (list)...OO doesnt really dictate to me that I absolutely can have no knowledge of what other objects are in existence. My tree is an object (Self contained) that when an item is selected it looks for the pointer to the list, if its there my tree posts a message to it. The list handles the rest.
The reason for sticking the pointers in the app object itself is because every class (or almost every class) in an SDI or MDI app can all AfxGetApp() and get a pointer to the app.
This (to me) is not a tradeoff in OOP/usefullness. It is one of the available solutions that work.
You could literally spend an entire day trying to figure out some elegant way to do something (I have), that hardly anyone will probably ever appreciate, or get it done and working (which you and most people do appreciate).
-David S.
|
|
|
|
 |
|
 |
The proper solution is to have the creator of those two views (the Main Frame) give both view a pointer to each other by calling a SetXxxView() function in those views. Then they would store a pointer to that view as a member, and thus be able to access it whenever.
Otherwise you are not being very OO. p
[b]yte your digital photos with [ae]phid [p]hotokeeper - www.aephid.com.
|
|
|
|
 |
|
 |
Oh well, thats one way to break the doc-view framework, but there are more elegant solutions.
|
|
|
|
 |
|
 |
That's true. Why don't you login and give us some.
|
|
|
|
 |
|
 |
I access the cview classes from the document manager. You can access the document manager from the global variable theApp. From the document manager you can get the doc template which stores a pointer to the view. I have more details on my tutorials on multiple cview mdi apps (under doc/view section). There is also other good tut on cdocmanager class on code project. I forget from who.
Rj45
|
|
|
|
 |
|
 |
CView *GetActiveView(void)
{
CFrameWnd *pFrameWnd;
pFrameWnd = (CFrameWnd *)AfxGetMainWnd();
if (pFrameWnd != NULL && pFrameWnd->IsKindOf(RUNTIME_CLASS(CMDIFrameWnd)))
pFrameWnd = ((CMDIFrameWnd *)pFrameWnd)->GetActiveFrame();
return (pFrameWnd == NULL)? NULL : pFrameWnd->GetActiveView();
}
This function will always return the active view in an SDI or MDI application without the need to store/restore pointers
Craig
|
|
|
|
 |
|
 |
In an MDI, incase any of childs is NOT maximized, then you will have a problem!
One suggestion is to fix it this way:
BOOL bMaximized;
CxxxxxxView* m_pActiveView;
CMDIChildWnd* pMDIChild = MDIGetActive(&bMaximized);
if(pMDIChild)
{
m_pActiveView = (CxxxxxxView*)pMDIChild->GetActiveView();
ASSERT_VALID(m_pActiveView);
}
|
|
|
|
 |
|
 |
This is an example to retrieve the CTreeCtrl in an explorer like view.
CTreeCtrl* CHTMLTreeDoc::GetTree()
{
POSITION pos=GetFirstViewPosition();
CView *m_V=NULL;
while (pos)
{
m_V=GetNextView(pos);
if (m_V->IsKindOf(RUNTIME_CLASS(CLeftView)))
return &((CLeftView*)m_V)->GetTreeCtrl();
}
return NULL;
}
|
|
|
|
 |
|
 |
Brilliant idea Being a beginner I think it was quite helpful
|
|
|
|
 |