Solved

Getting ahold of a Window

Posted on 1998-09-25
4
155 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

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

Suggested Solutions

Title # Comments Views Activity
Perl Awk Need Help 3 136
fizzArray2 challenge 1 100
Problem to event 3 112
moto 4g phone enable finger print 4 42
Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
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.
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

680 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