Click here to Skip to main content
15,880,469 members
Articles / Desktop Programming / MFC
Article

CView Access From Anywhere

Rate me:
Please Sign up or sign in to vote.
3.45/5 (19 votes)
15 Jan 2001 118.3K   868   25   17
Access your View class from anywhere in the application.

Introduction

MFC (Access to CView from Anywhere).

This is the cheats way of getting access to CView from anywhere in your application.

  1. In you CWinApp, create two functions: a void function StoreMyView (CView* pView) and a CView* function GetMyView ().
  2. Declare a pointer of type CView in the CWinApp class.
  3. In CView constructor, get the application using the following lines:
    CWinApp* pApp = (CWinApp*) AfxGetApp ();
    pApp->StoreMyView (this);
  4. In CWinApp GetMyView (), return your declared pointer.
  5. In CWinApp StoreMyView, point your data member to the parameter value, i.e. say, data member is m_pView and parameter is pView; so m_pView = pView;
  6. Now you can access your view using the application object, i.e.,
    CWinApp * pApp = (CWinApp*) AfxGetApp ();
    CView * pView = pApp. GetMyView ();
  7. Don't forget to include View.h and Doc.h in CWinApp.h and the view.h is included in .cpp files automatically when you create your data member in VC++.

That's all folks.

Any comments, mail to: kings_oz@yahoo.com.

History

2001-01-15 - First release

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
kin
Europe Europe
no need for this information at this moment

Comments and Discussions

 
GeneraltheApp Instead of AfxGetApp() Pin
Joseph Marzbani26-Feb-09 9:01
Joseph Marzbani26-Feb-09 9:01 
GeneralFor another View in MDI Pin
Ibrahim Hamoud22-Jul-03 13:14
Ibrahim Hamoud22-Jul-03 13:14 
GeneralRe: For another View in MDI Pin
rj4527-Oct-03 21:41
rj4527-Oct-03 21:41 
GeneralOrder of includes is important! Pin
Slawa19-May-03 4:26
Slawa19-May-03 4:26 
GeneralRe: Order of includes is important! Pin
Slawa19-May-03 5:29
Slawa19-May-03 5:29 
GeneralAnother possibility... Pin
Brendan Tregear30-Jan-01 15:49
Brendan Tregear30-Jan-01 15:49 
GeneralRe: Another possibility... Pin
eric kaminski11-Nov-02 10:50
eric kaminski11-Nov-02 10:50 
QuestionWhy? Pin
AlexMarbus22-Jan-01 12:10
AlexMarbus22-Jan-01 12:10 
AnswerRe: Why? Pin
dswigger28-Jul-01 9:19
dswigger28-Jul-01 9:19 
GeneralRe: Why? Pin
Swinefeaster7-Nov-03 12:48
Swinefeaster7-Nov-03 12:48 
The proper solution is to have the creator of those two views (the Main Frame) give both view a pointer to each other by calling a SetXxxView() function in those views. Then they would store a pointer to that view as a member, and thus be able to access it whenever.

Otherwise you are not being very OO. Smile | :) p

[b]yte your digital photos with [ae]phid [p]hotokeeper - www.aephid.com.
GeneralBreaking the framework. Pin
21-Jan-01 2:38
suss21-Jan-01 2:38 
GeneralRe: Breaking the framework. Pin
Paul Belikian26-Sep-02 13:33
Paul Belikian26-Sep-02 13:33 
GeneralRe: Breaking the framework. (I agree) Pin
rj4527-Oct-03 21:39
rj4527-Oct-03 21:39 
GeneralAlternative: easier and more robust Pin
17-Jan-01 1:29
suss17-Jan-01 1:29 
GeneralRe: Alternative: easier and more robust Pin
18-Jan-01 9:28
suss18-Jan-01 9:28 
GeneralRe: Alternative: easier and more robust Pin
Gilad Novik12-Jun-01 8:15
Gilad Novik12-Jun-01 8:15 
Generalgreat idea Pin
16-Jan-01 23:27
suss16-Jan-01 23:27 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.