Solved

CWinApp and CDocManager problem

Posted on 1998-07-14
14
1,062 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
The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

 
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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Turn a spreadsheet into a vba executable. 2 86
Adapt this command to show who installed 29 108
Separate into files by filename 12 82
Window placement 17 82
Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
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.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

773 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