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
Solved

CWinApp and CDocManager problem

Posted on 1998-07-14
14
1,080 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
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.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

840 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