Tuesday, May 28, 2013

modaless dialog refresh the view in SDI without document/view structure support

I create one SDI without document/view structure support, i name the project as ImagePro, then I can get the following files:

ChildView.cpp; (class CChildView : public CWnd, OnPaint() is here)
MainFrm.cpp; (class CMainFrame : public CFrameWnd)
ImagePro.cpp;(class CImageProApp : public CWinApp)

I also create CModalLessDlg using the dialog template, which is defined in ModalLessDlg.cpp and ModalLessDlg.h

In the  ModalLessDlg.cpp, I want to press the "Begin" button to refresh the "view"

you can realize this like this:

1. in MainFrm.h, there is one protected variable:

protected: CChildView   m_wndView;

change it as public variable:

public: CChildView   m_wndView;

2. add one public variable for the ChildView.h:

public: int dlg_fresh;

initialize it in ChildView.cpp:

CChildView::CChildView()
{
 dlg_fresh=0;
}

3. in the response function of modalless dialog button, write like this:

//ModalLessDlg.cpp 

void CModalLessDlg::OnBegin() // stard the thread
{
 CMainFrame   *pFrame=(CMainFrame*)(AfxGetApp()->m_pMainWnd); // get the mainframe
 (pFrame->m_wndView).dlg_fresh=1; //set the flag
 (pFrame->m_wndView).Invalidate(); // refresh the view in the ChildView

}

4. in the ChildView.cpp, implement the OnPaint() like this:

//ChildView.cpp

void CChildView::OnPaint()
{
 CPaintDC dc(this); // device context for painting
 CRect rect;
 GetClientRect (&rect);
 if(dlg_fresh==0)
 {
  dc.DrawText (_T ("it is not the modaless dlg refreshing!"), -1, &rect,DT_SINGLELINE | DT_CENTER | DT_VCENTER); 
 }
 else
 {
    dc.DrawText (_T ("it is the modaless dlg refreshing!"), -1, &rect,DT_SINGLELINE | DT_CENTER | DT_VCENTER); 
       dlg_fresh=0;
 }
}


No comments:

Post a Comment