Link to home
Start Free TrialLog in
Avatar of shmuelal
shmuelal

asked on

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

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
Avatar of Freekeko
Freekeko

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;
if u meant something else plz tell me.
hope it worx
Avatar of shmuelal

ASKER

Hi,

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

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?
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.
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
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.
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?

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.
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
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
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 ?
ASKER CERTIFIED SOLUTION
Avatar of Crius
Crius

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.


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.
Thanks,

This is what I looked for.

Alon
Thanks,
This is what I looked for.