Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 389
  • Last Modified:

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
0
shmuelal
Asked:
shmuelal
  • 9
  • 3
  • 3
  • +1
1 Solution
 
FreekekoCommented:
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
 
FreekekoCommented:
if u meant something else plz tell me.
hope it worx
0
 
shmuelalAuthor Commented:
Hi,

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

0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
FreekekoCommented:
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
 
gurlyCommented:
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
 
shmuelalAuthor Commented:
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
 
CriusCommented:
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
 
shmuelalAuthor Commented:
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
 
gurlyCommented:
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
 
shmuelalAuthor Commented:
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
 
shmuelalAuthor Commented:
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
 
gurlyCommented:
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
 
CriusCommented:
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
 
shmuelalAuthor Commented:
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
 
shmuelalAuthor Commented:
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
 
shmuelalAuthor Commented:
Thanks,

This is what I looked for.

Alon
0
 
shmuelalAuthor Commented:
Thanks,
This is what I looked for.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 9
  • 3
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now