[MFC] Cum fac update la view-uri intr-o aplicatie SDI/MDI?

Despre MFC, ATL si alte biblioteci C++ de la Microsoft (forum moderat)
Post Reply
User avatar
Ovidiu Cucu
Fondator
Fondator
Posts: 3778
Joined: 11 Jul 2007, 16:10
Judet: Iaşi
Location: Iasi
Contact:

[MFC] Cum fac update la view-uri intr-o aplicatie SDI/MDI?

Post by Ovidiu Cucu » 01 May 2011, 12:36

Intrebare
Cum fac update la view-uri intr-o aplicatie SDI/MDI?

Raspuns
Apelez pur si simplu functia CDocument::UpdateAllViews care este suficient in urmatoarele cazuri:
  • aplicatia este de tipul SDI (Single Document Interface).
  • aplicatia este de tipul MDI (Multiple Document Interface) si ma intereseaza sa fac update doar in view-urile din template-ul de document curent.
Exemplu

Code: Select all

void CWhateverView::OnSomethingChanged() 
{
   CView* pSender = this; // exclude this view from updating (or NULL if not exclude).
   LPARAM lHint = 0L;     // change this if need a hint about what has been changed 
   CObject* pHint = NULL; // change this if need info about what has been changed

   CDocument* pDoc = GetDocument();
   pDoc->UpdateAllViews(pSender, lHint, pHint);
}
In celelalte view-uri suprascriu functia virtuala CView::OnUpdate si fac modificarile necesare:

Code: Select all

void CAnotherView::OnUpdate(CView* pSender, LPARAM lHint, CObject* pHint) 
{
   // update this view.
   // can use pSender, lHint, and pHint to see what has to be done.
}
Nota
Un exemplu practic poate fi gasit in proiectul LogExpert care este MDI, are un singur document template dar cu mai multe view-uri intr-o fereastra splitter.

Cazurile de mai sus acopera majoritatea aplicatiilor SDI/MDI.
Totusi s-ar putea ca, intr-o aplicatie MDI sa se doreasca update la toate view-urile indiferent in ce template se afla.
In cazul acesta, enumar mai intai template-urile iar pentru fiecare template enumar documentele. In sfarsit pentru fiecare document, apelez CDocument::UpdateAllViews.
Cel mai bun loc pentru o astfel de functie ar fi in clasa aplicatiei (derivata din CWinAPP).

Exemplu

Code: Select all

class CMyMDIApp : public CWinApp
{
//...
public:
   void UpdateAllViews(CView* pSender, LPARAM lHint = 0L, CObject* pHint = NULL);
//...
};

Code: Select all

void CMyMDIApp::UpdateAllViews(CView* pSender, LPARAM lHint, CObject* pHint)
{
   POSITION posDocTemplate = GetFirstDocTemplatePosition();
   while(NULL != posDocTemplate)
   {
      CDocTemplate* pDocTemplate = GetNextDocTemplate(posDocTemplate);
      POSITION posDoc = pDocTemplate->GetFirstDocPosition();
      while(NULL != posDoc)
      {
         CDocument* pDoc = pDocTemplate->GetNextDoc(posDoc);
         pDoc->UpdateAllViews(pSender, lHint, pHint);
      }
   }
}

Code: Select all

void CWhateverView::OnSomethingChanged() 
{
   CMyMDIApp* pApp = (CMyMDIApp*)AfxGetApp();
   CView* pSender = this; // exclude this view from updating (or NULL if not exclude).
   LPARAM lHint = 0L;     // change this if need a hint about what has been changed 
   CObject* pHint = NULL; // change this if need info about what has been changed

   pApp->UpdateAllViews(pSender, lHint, pHint);
}

Vezi si << Back to MFC index



Post Reply