Solved

Getting ahold of a Window

Posted on 1998-09-25
4
157 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
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.
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…

691 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