Link to home
Start Free TrialLog in
Avatar of StraySod
StraySodFlag for Czechia

asked on

Getting CView pointer

Hi,

is there a better way of getting CMyView pointer on object created using RUNTIME_CLASS than the one in code snippet?

I would like to get the pointer pointing on CMyView object right after RUNTIME_CLASS call locally and not to use global variable.
//global variable
CMyView* g_pView;

/////////////////////////////////////////

//dynamic creation of CView derived class somewhere else
RUNTIME_CLASS(CMyView);

/////////////////////////////////////////

//CMyView constructor
CMyView::CMyView()
{
   g_pView = this;
}

Open in new window

Avatar of pgnatyuk
pgnatyuk
Flag of Israel image

Of course, you can find the code creating this view. Probably you have a frame window and in this class (CMainFrame) you can find the CView member.

CMyView* pView = (CMyView*)GetActiveView();
if(pView->IsKindOf(RUNTIME_CLASS(CMyView)))

http://msdn.microsoft.com/en-us/library/zs0t7t34(VS.80).aspx
I took the attached code from "How To Get Current CDocument or CView from Anywhere":
http://support.microsoft.com/kb/108587

// View implementation file
   CMyView * CMyView::GetView()
   {
      CFrameWnd * pFrame = (CFrameWnd *)(AfxGetApp()->m_pMainWnd);

      CView * pView = pFrame->GetActiveView();

      if ( !pView )
         return NULL;

      // Fail if view is of wrong kind
      // (this could occur with splitter windows, or additional
      // views on a single document
      if ( ! pView->IsKindOf( RUNTIME_CLASS(CMyView) ) )
         return NULL;

      return (CMyView *) pView;
   }

Open in new window

You can get the view pointer in different ways; it depends by the type of your application (MDI, SDI...)

See "How To Get Current CDocument or CView from Anywhere" at
http://support.microsoft.com/?scid=kb%3Ben-us%3B108587&x=12&y=8
SOLUTION
Avatar of AndyAinscow
AndyAinscow
Flag of Switzerland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of StraySod

ASKER

Thank you