views and docs

i have 2 problems:
1. i need to prevent from documents to be created in some conditions, how to do it?
2. i need to draw on the a 2nd view in a coordinates of the corsor which is in the first view, and only there, how to loop for the view? (which is not this pointer)

Who is Participating?
Meir RivkinConnect With a Mentor Full stack Software EngineerCommented:
for Q1:

in OnNewDocument of your Doc class add a condition before the creation:
m_bAllowCreateDocs is a data member in the app or main frame which is false in the cinstructor and true when u allow creating of docs

BOOL CMyDoc::OnNewDocument()
     if (CDocument::OnNewDocument())
               return TRUE;

     return TRUE;

for Q2 is a bit longer:
1. add this member to your CApp class ->      CMultiDocTemplate* m_ptrDocTemplate;
and initialize it in the InitInstance():
     CMultiDocTemplate* pDocTemplate;
     pDocTemplate = new CMultiDocTemplate(
          RUNTIME_CLASS(CChildFrame), // custom MDI child frame
     m_ptrDocTemplate = pDocTemplate; //saving pointer to the doc template

2.add a function to the app called GetNumOfOpenWindows():
UINT CDblCursorApp::GetNumOfOpenWindows()
     UINT uNumOfOpenWindows = 0;
     POSITION pos = m_ptrDocTemplate->GetFirstDocPosition();
     CDblCursorDoc* pDoc;
     CDblCursorView* pView;

     while ( pos != NULL ) {
          pDoc = (CMyDoc*)(m_ptrDocTemplate->GetNextDoc(pos));
          POSITION posView = pDoc->GetFirstViewPosition();

          while( posView != NULL ) {
               pView = (CMyView*)pDoc->GetNextView(posView);


     return uNumOfOpenWindows;

3.override OnMouseMove of your CVIew:
void CDblCursorView::OnMouseMove(UINT nFlags, CPoint point)
     if(((CDblCursorApp*)AfxGetApp())->GetNumOfOpenWindows())     {
          CMultiDocTemplate* pDT = ((CMyApp*)AfxGetApp())->GetDocTemplate();
          POSITION pos = pDT->GetFirstDocPosition();
          CMyDoc* pDoc;
          CMyView* pView;

          while ( pos != NULL ) {
               pDoc = (CMyDoc*)(pDT->GetNextDoc(pos));
               POSITION posView = pDoc->GetFirstViewPosition();

               while(posView != NULL) {
                    pView = (CMyView*)pDoc->GetNextView(posView);
                    if(pView == this)
                         continue;//skip special drawing on your view !!!

     CView::OnMouseMove(nFlags, point);

4.add a special drawing function to your view:
void CMyView::MoveCursorPos(CPoint point)
//here add your special drawing code with the point from the 1st view

good luck

UpdateAllViews with proper hint will help you to part2.
for part1
IMHO you have to derive class DocTemplate and override virtual CDocument* CreateNewDocument(); method
Meir RivkinFull stack Software EngineerCommented:
instead of void CMyView::MoveCursorPos(CPoint point)
it should be void CMyView::DoSpecialDraw(CPoint point)

cheers mate
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.