Solved

Getting ahold of a Window

Posted on 1998-09-25
4
153 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 Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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 …
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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.

839 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