Solved

How to open programmatically a new file in an MDI application?

Posted on 2002-05-22
17
328 Views
Last Modified: 2013-11-20
Hi all,

I'm writing an MDI application which is a visual studio like.
I want to open a new file through the tree view, and therefore I should handle this through the code.
The problem I have is how to open a new file in which I will put the content of the file I'm openning.

Thanks in advance,
Alon Shmuel
0
Comment
Question by:shmuelal
  • 9
  • 3
  • 3
  • +1
17 Comments
 
LVL 1

Expert Comment

by:Freekeko
ID: 7026367
well, i didn't understand exactly what u wanna do, but if u want a code 2 open a new file, n write in it, here it's:


CFile cFile;
CString cText = "Hi, This is a test";

cFile.Open "C:\\Test.txt",CFile::modeCreate|CFile::modeReadWrite);

cFile.Write (cText,cText.GetLength());

cFile.Close;
0
 
LVL 1

Expert Comment

by:Freekeko
ID: 7026368
if u meant something else plz tell me.
hope it worx
0
 

Author Comment

by:shmuelal
ID: 7026408
Hi,

What I ment is to open a new child window, like it happens when you select through the menu File->New.

0
 
LVL 1

Expert Comment

by:Freekeko
ID: 7026414
um, okay one more thing, is your program is for text only? i mean, you want 2 make it like notepad or such programs?

if so u can  change the base class from the beginning to CEditView, so? u wanna make it a text program?
0
 
LVL 1

Expert Comment

by:gurly
ID: 7026753
If you want to open a new child window (with a new view on it) you should use the doc-template mechanism.
Meaning: create a new doc template and add it to the application. Than call OpenDocumentFile on the doc template.
If you need more information or source code - let me know.
0
 

Author Comment

by:shmuelal
ID: 7026790
First,
I'm using a Rich Edit View and therefore my view class is from that type.
regarding the doc-template thing.
I saw something about it at the MSDN but I didn't succeed in implementing it.
I would be very happy for an example.

Thanks
0
 
LVL 3

Expert Comment

by:Crius
ID: 7028050
You can try something like this:

(In your CWinApp child InitInstance() you have someththing like:
    pDocTemplate = new CMultiDocTemplate(
         IDR_MAINFRAME,
         RUNTIME_CLASS(CMAMReportsDoc),
         RUNTIME_CLASS(CChildFrame), // custom MDI child frame
         RUNTIME_CLASS(CMAMReportsView));
    AddDocTemplate(pDocTemplate);


You can add a function to your tree control, like:
CMyDocument *CMyTreeControl::MakeDocument()
{
    CMyDocument *WorkWinApp = (CMyWinApp *)AfxGetApp();

    return (CMyDocument *)WorkWinApp->pDocTemplate->OpenDocumentFile(NULL);
}

Assuming pDocTemplate is your template name.

You can then use the returned Document pointer to add information, load files, give commands, etc.

This is basically some sample code for gurly's solution.
0
 

Author Comment

by:shmuelal
ID: 7028809
Hi  Crius,

I have some questions about your answer:

What is CMyDocument? Is it teh document that attached to the tree view?

How do I use this code?

0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 1

Expert Comment

by:gurly
ID: 7029422
CMyDocument is supposed to be the document that is attached to your Rich Edit View.
If you don't have any you may want to add such a class.
If not - you can use the document attached to your tree view.
If neither of these options suites you, you can create the view without using a doc template. just call CreateView on your frame window (child frame / main frame) with an appropriate CCreateContext.
0
 

Author Comment

by:shmuelal
ID: 7029493
I'm sorry,
But I do not understand what you're saying.
First of all I tried to write the code you attached and it has problem in the compilation.
It does not allow the  CMyDocument *WorkWinApp = (CMyWinApp *)AfxGetApp().

Second, what did you mean with the CCreateContext?
I need to see an example because I don't have any clue.


Thanks
0
 

Author Comment

by:shmuelal
ID: 7029509
I'm sorry,
But I do not understand what you're saying.
First of all I tried to write the code you attached and it has problem in the compilation.
It does not allow the  CMyDocument *WorkWinApp = (CMyWinApp *)AfxGetApp().

Second, what did you mean with the CCreateContext?
I need to see an example because I don't have any clue.


Thanks
0
 
LVL 1

Expert Comment

by:gurly
ID: 7029670
In order to help you, I need to know if you will need a specific document attached to your rich edit view.
If you do - should it be a different class, or a different instance of your current document (the one attached to the tree view).
If you don't need a specific document, that means you can use the same instance the rich edit view is attached to.
Is that correct ?
0
 
LVL 3

Accepted Solution

by:
Crius earned 100 total points
ID: 7029889
Oops, my mistake. I have made a correction to the code I posted (I blew it when I was renaming the class types from my own code):

Assuming you CWinApp child class is called CMyWinApp:

(In CMyWinApp::InitInstance() you have someththing like:
   pDocTemplate = new CMultiDocTemplate(
        IDR_MAINFRAME,
        RUNTIME_CLASS(CRichEditDoc),
        RUNTIME_CLASS(CChildFrame), // custom MDI child frame
        RUNTIME_CLASS(CRichEditView));
   AddDocTemplate(pDocTemplate);


You can add a function to your tree control, like:
CRichEditDoc *CMyTreeView::MakeDocument()
{
   CMyWinApp *WorkWinApp = (CMyWinApp *)AfxGetApp();

   return (CRichEditDoc *)WorkWinApp->pDocTemplate->OpenDocumentFile(NULL);
}

The problem was I was trying to cast and stuff a CMyWinApp object into a CMyDocument class previously. Sorry.
0
 

Author Comment

by:shmuelal
ID: 7034945
Hi,

Crius: The example you gave still doesn't work. I get this error:
'pDocTemplate' : is not a member of 'CVideoPlusDebuggerApp' -
where 'CVideoPlusDebuggerApp' is the name my application.

Qurly: The project that I'm writing is has a tree view which is a dockable window. The treeview doesn't have any document because it is based on a code I took from codeguru for creating dockable windows in an MDI application.
The Rich Edit View is has the document, and maybe this causes me all the problems. Isn't it?
In the edit view I would like to open different files, so I think I should have a different document.


0
 

Author Comment

by:shmuelal
ID: 7034949
Hi Crius,

I'm returning back my words from he last comment, this cod eis fine and I just made some little modifications and now it looks like this:

CRichEditDoc *CProjectView::MakeDocument()
{
CMyApp *WorkWinApp = (CMyApp *)AfxGetApp();

POSITION pos = WorkWinApp->GetFirstDocTemplatePosition();
CDocTemplate *pDocTemplate =
                  WorkWinApp->GetNextDocTemplate(pos);
     return (CRichEditDoc *)pDocTemplate->OpenDocumentFile(NULL);
}

Now I just have to check if everything is fine and if it does I will take this comment.
0
 

Author Comment

by:shmuelal
ID: 7034970
Thanks,

This is what I looked for.

Alon
0
 

Author Comment

by:shmuelal
ID: 7034971
Thanks,
This is what I looked for.
0

Featured Post

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.

Join & Write a Comment

Suggested Solutions

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…
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.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

743 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

11 Experts available now in Live!

Get 1:1 Help Now