Solved

CWinApp and CDocManager problem

Posted on 1998-07-14
14
1,102 Views
Last Modified: 2013-11-20
I derived a CDocManager class named CMyDocManager. and I derived a CWinApp class named CMyWinApp. And I declared a member var in CMyWinApp as CMyDocManager m_pDocManager. But in my overloading virtual function OpenDocumentFile(), the m_pDocManager doesn't work properly. Here's the problem:
m_pDocManager is CMyDocManager var. I haven't initialize it. But the CWinApp::m_pDocManager is initialied. How can I initialize my own m_pDocManager? Or how can I change the functions of CDocManager m_pDocManager?
here's codes:
class CMyWinApp:public CWinApp
{
....
CMyDocManager m_pDocManager;
virtual void function();//overloaded
};
class CMyDocManager :public CDocManager
{
virtual void func(); //overloaded
};
void CMyWinApp::function()
{
m_pDocManager->func();
// a error occurs here( m_pDocManager == NULL)
}
Thanks in advanced.
0
Comment
Question by:speedup
[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
14 Comments
 
LVL 3

Expert Comment

by:danny_pav
ID: 1319173
CDocManager is not a documented class.  Any answer given could be broken in later versions of MFC.  What exactly are you trying to do? Maybe documented features can be overrided which will be a more stable answer.

0
 
LVL 1

Author Comment

by:speedup
ID: 1319174
hi,danny_pav
In fact, I want to change the behavier of OpenDocumentFile() when
the file I want to open is already opened instead of doing nothing.
0
 
LVL 3

Expert Comment

by:danny_pav
ID: 1319175
Why not derive a class from CDocTemplate?  This has the OpenDocumentFile() as a virtual.  Plus it's easy to plug this in to your app.  You can edit your code in InitInstance to create your document template instead of the one the AppWizard suggested.

Also what exactly are you trying to do?

0
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!

 
LVL 1

Author Comment

by:speedup
ID: 1319176
here's for danny_pav:
I open a file with extension .tpc, and when I open the same file again, there's nothing happened. because the file has been opened. I want to change the behavior when I open the same file that is already opened, it close the file and open it again. so I can get the new content that has been updated when it's opened.
0
 
LVL 3

Expert Comment

by:danny_pav
ID: 1319177
So you want to refresh the file.  Why not add a refresh toolbar button/menu item/accelerator and do the appropriate code in response. Do you need help coding that?
0
 
LVL 1

Author Comment

by:speedup
ID: 1319178
But it is so horrible that I write a program for some dummies:-(, so i can't use toolbar. If I do that, my boss will told me that it's too difficult to use for our customers. So I want some automatic feathers.

0
 
LVL 3

Expert Comment

by:danny_pav
ID: 1319179
Assuming you can handle the refresh code, you can override
CWinApp::OpenDocumentFile().

class CMyWinApp : public CWinApp {

private:
BOOL m_bRefreshNow;

public:
BOOL MustRefreshDoc() const { return m_bRefershNow; }

public:
virtual CDocument* OpenDocumentFile(LPCTSTR lpsz) {
m_bRefreshNow = TRUE;
CDocument* pDoc = CWinApp::OpenDocumentFile(lpsz);
m_bRefreshNow = FALSE;
return pDoc;
}

};

Then in your mainframe's OnActivateFrame -- check the app's refersh flag and if TRUE-> refresh the document.  Of course, you should set the flag to False in the constructor.  This will work for SDI and MDI.





0
 
LVL 1

Author Comment

by:speedup
ID: 1319180
I've found a way to solve the problem.
codes go here:
CDocument* CRefreshApp::OpenDocumentFile(LPCTSTR lpszFileName)
{
      CDocument* pOpenDocument = NULL;
      CDocTemplate::Confidence bestMatch = CDocTemplate::noAttempt;
      CDocTemplate *pDocTemplate;
      POSITION pos = GetFirstDocTemplatePosition();
      while( pos != NULL )
      {
            pDocTemplate = GetNextDocTemplate( pos );
            ASSERT_KINDOF(CDocTemplate, pDocTemplate);
            CDocTemplate::Confidence match;
            match = pDocTemplate->MatchDocType(lpszFileName, pOpenDocument);
            if (match > bestMatch)
            {
                  bestMatch = match;
            }
            if (match == CDocTemplate::yesAlreadyOpen)
            {
                  pOpenDocument->OnCloseDocument();
                  return OpenDocumentFile( lpszFileName );
            }
      }
      return CWinApp::OpenDocumentFile( lpszFileName );
}
0
 
LVL 3

Expert Comment

by:danny_pav
ID: 1319181
why was my answer rejected?
0
 
LVL 1

Author Comment

by:speedup
ID: 1319182
I've tried your answer, but seems to fail.
0
 
LVL 3

Expert Comment

by:danny_pav
ID: 1319183
where/when did it fail?  I use this technique and have no problems
0
 

Expert Comment

by:TangCheng
ID: 1319184
  You must create a CMyDocManager's class example, Then the App can automatism initial this class, then you can use this class.
   I often use this idea to write my DocManager class.

eg.
    class CMyWinApp:public CWinApp
    {
    ....
    CMyDocManager m_DocManager; //Declare a example,don't declare
                                //a point      
    virtual void function();//overloaded
    };

    class CMyDocManager :public CDocManager
    {
    virtual void func(); //overloaded
    };

    void CMyWinApp::function()
    {
    m_DocManager.func(); //then you can call this funciton ok.
    }
0
 
LVL 1

Author Comment

by:speedup
ID: 1319185
TangCheng,
Your proposed answer can't work, coz m_pMyDocManager and the original m_pDocManager are different data member. m_pMyDocManager can't get initialized data automatically from m_pDocManager. Just try it and you will get the result.

Reguards,

0
 
LVL 1

Accepted Solution

by:
elf_k earned 100 total points
ID: 1319186
Try to do something like that.
class CMyDocManager : public CDocManager
{
// override what you want here
};

class CMyApp : public CWinApp
{
 virtual BOOL InitApplication()
 {  // best time to init here
    CDocManager::pStaticDocManager=new CMyDocManager;
    // rest of init is doing by MFC
    CWinApp::InitApplication();
 }
};

That's all. You don't need to create a m_pDocManager variable in CMyApp, because it is already exist in CWinApp, and used by many of the functions and command handlers of CWinApp.
I have no time to test it, but it must work, according to MFC source code.

0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

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…
In this post we will learn different types of Android Layout and some basics of an Android App.
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.
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

695 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