Solved

CWinApp and CDocManager problem

Posted on 1998-07-14
14
1,088 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
Independent Software Vendors: 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

Independent Software Vendors: 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!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
zeroFront challenge 7 124
How to convert MFC::CString to UTF8 wchar_t* 10 386
Capture logon name 13 105
Problem to App source 6 93
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…
Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
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.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

733 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