Solved

Getting ahold of a Window

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

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Named range not carried over 10 70
fizzArray2 challenge 1 110
Execute multiple curl cmds with sleep and send output to file 10 121
Increment column based of a FK 8 67
Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
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.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

732 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