Avatar of StraySod
StraySod
Flag 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

Editors IDEsSystem ProgrammingVisual C++.NET

Avatar of undefined
Last Comment
StraySod

8/22/2022 - Mon
pgnatyuk

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
pgnatyuk

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

alb66

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
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
SOLUTION
AndyAinscow

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
ASKER CERTIFIED SOLUTION
itsmeandnobodyelse

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
StraySod

ASKER
Thank you