Solved

Getting ahold of a Window

Posted on 1998-09-25
4
148 Views
Last Modified: 2013-11-20
I need to call a member function of a view class.  I am
calling it from a function that is totally unrelated and in
a separate file of (non-classed) functions.  How do I get
ahold of the view's instance so I can call the function?  
It isn't static.
     
There is always one instance of this view open.
0
Comment
Question by:appleby
  • 2
4 Comments
 
LVL 8

Accepted Solution

by:
Answers2000 earned 70 total points
ID: 1322616
You need to be able to get a pointer to the view as you realize

Why not add a public static member to the view class :

In view header

class CSomeView : public CView
{
 //etc
public:
  static CSomeView * pTheView ;
// etc
} ;

In the view's cpp

CSomeView * CSomeView::pTheView = NULL ;

In the constructor of the view, assign pTheView to the this ptr
In the destructor of the view, assign it back to NULL

CSomeView::CSomeView( ..etc.. )
{
 //etc
 pTheView = this ;
}

CSomeView::~CSomeView()
{
 pTheView = NULL ;
}


Now in the function that needs to call the view's function you can do this

void SomeFunction( ..etc.. )
{
  ASSERT_VALID( CSomeView::pTheView ) ;
  CSomeView::pTheView->FunctionToCall( ..etc.. ) ;
}

0
 

Expert Comment

by:umarama
ID: 1322617
Using Afx calls you can get a pointer to the current active view.
m_pView      = ((CFrameWnd *)AfxGetMainWnd())->GetActiveView();

Also, you can use the following calls to get other imp ptrs.
m_pMainWnd = (CFrameWnd*)AfxGetMainWnd();
m_pApp         = AfxGetApp();


0
 

Author Comment

by:appleby
ID: 1322618
Answers2000, what is going on with that first line you suggest adding to the view's .cpp?  
 
CSomeView * CSomeView::pTheView=NULL;

I've never seen this type of syntax before.  Why is it necessary, instead of just going right to the constructor?
0
 

Author Comment

by:appleby
ID: 1322619
I get it - just an initializer.  Ok thanks!
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now