Solved

CWinApp and CDocManager problem

Posted on 1998-07-14
14
1,034 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
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
 
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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
cat dog challenge 18 122
Expand macro to ask for filename column 8 35
Change to event 1 73
Problem to open Excel file 15 40
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…
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.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

705 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now