|
Hello all,
I am writing an MFC app for a college project.
I have some code that opens & saves files (not using serialization).
When I open a file (.gbc) it is read in and parsed, objects are created (Notes, Chords, Bars, Tablines) based on these values, but I dont know how to create a new view or doc to display these objects.
Can someone help me please ?
My open code is in MainFrame.cpp and works wonderfully, except I can open 5 files but only have the one original document open ?
Rob
|
|
|
|
|
You should place your load/save code into the Document class, regardless of if you use serialisation or your own code. If you put it in the mainframe, you're going to be fighting doc/view all the way. If you set things up so your view displays the info in your document class, which loads and saves it, using your existing code, and let OnFileNew/OnFileOpen/OnFileSave do their default thing of creating instances of views/documents, you'll find life a lot easier.
Christian
After all, there's nothing wrong with an elite as long as I'm allowed to be part of it!! - Mike Burston Oct 23, 2001
|
|
|
|
|
Thanks Christian,
I'll try moving my code into the doc.
One thing I am baffled on is how the default view/doc is created ?
When I open one of my files, i can parse the data, but i can make a view to display the objects I have just made ?
I found some code to do this, but keep getting errors. If you were going to open a file, parsing it and then make a new view, how would you do it ?
Rob
|
|
|
|
|
Opening a document also creates a view to go with it.
To open a document from the mainframe class ( which I do to impliment a file browser ), I do this
AfxGetApp()->OpenDocumentFile(Dlg.m_Path);
where m_Path is a member variable of the browser dialog, containing the path to load
In my document class I override OnFileOpen and call it in the CWinApp derived class, which looks like this:
CDocument* CMyApp::OpenDocumentFile(LPCTSTR lpszFileName)
{
CFileStatus fs;
if (!CFile::GetStatus(lpszFileName,fs))
{
CString s;
s.Format("Error - file %s not found",lpszFileName);
AfxMessageBox(s,MB_ICONERROR);
return NULL;
}
try
{
CDocument* pReturn = CWinApp::OpenDocumentFile(lpszFileName);
CMyDoc* pDoc = (CMyDoc*)pReturn;
pDoc->m_Filename = lpszFileName;
return pReturn;
}
catch(...)
{
return NULL;
}
}
I seem to recall this was because it's a MDI app, so it called the method in CWinApp if I had nothing open, otherwise it called it in the document class.
This calls OnOpenDocument in my document class, where I simply load my data into the class and return TRUE. This gives me a document and the view associated with it.
Christian
After all, there's nothing wrong with an elite as long as I'm allowed to be part of it!! - Mike Burston Oct 23, 2001
|
|
|
|
|
Hi Christian,
Thank you very much for your help. Here is my code from my Mainfrm.cpp
// i select a file to load from the load dialog pop up
void CMainFrame::OnFileOpen()
{
CFileDialog dlg (TRUE, _T ("gbc"), _T ("*.gbc"),
OFN_FILEMUSTEXIST | OFN_HIDEREADONLY, m_szFilters);
if (dlg.DoModal () == IDOK &&
LoadFile(dlg.GetPathName())) {
m_strPathName = dlg.GetPathName ();
}
}
// i load the file, and pass each line to a parser class, thus creating my objects
BOOL CMainFrame::LoadFile(LPCTSTR pszFile)
{
BOOL bResult = FALSE;
FileParser fp;
try {
CStdioFile file (pszFile, CFile::modeRead);
CString string;
file.ReadString (string);
// if the string has a length
if (string.Compare("EOF") != 0) {
//fp.ParseString(string);
} else {
// if string is NULL then return
return TRUE;
}// end if
MessageBox("need to create a view...");
// at this point i need to create a view to display my objects, so they can be drawn in this view ?
bResult = TRUE;
}
catch (CFileException* e) {
e->ReportError ();
e->Delete ();
}
return bResult;
}
From the code you previously posted, I'm not sure which code will create a view ?
|
|
|
|
|
Creating a new document creates the view associated with it also.
Christian
After all, there's nothing wrong with an elite as long as I'm allowed to be part of it!! - Mike Burston Oct 23, 2001
|
|
|
|
|
WOOT ! I think I understand this ! Okay, one more question.
I see your OpenDoc() return a CDocument, which I cast into my view type and then use.
1. I am assuming that I will have to get this specific doc in order to put values into it. If I am going to have multipl views opnen at one time, Should I put a pointer to each doc into an array, so I can find it easily ?
How would you go about this ?
Many, many, many thanks,
Rob
|
|
|
|
|
Why HeapFree() is slower then free() about 200 times in Windows98?
In Win2000 all right.
|
|
|
|
|
The dialogs and dialog bars create the controls based on the selected font during creation. Is there any way to dynamically reposition/resize all controls and the dialogbar, when the font is changed?
I am now able to do it only before creation.
Thank you
Thomas
modified 29-Aug-18 21:01pm.
|
|
|
|
|
Hi,
I use the Video For Windows function AVIStreamGetFrame which returns a Packed DIB.
I then convert it to a DDB using the following code fragment:
HDC hDc=GetDC(NULL);
BITMAPINFOHEADER* bminfoheader;
BITMAPINFO* bminfo;
LPVOID bits;
bminfoheader=(BITMAPINFOHEADER*)lpbi;
bminfo=(BITMAPINFO*)lpbi;
bits = bminfoheader->biSize + (char *)lpbi;
HBITMAP hbitmap = CreateDIBitmap(hDc, bminfoheader, CBM_INIT, bits, bminfo, DIB_RGB_COLORS);
My concern is the colour usage parameter in createDIBitmap, could there be a situation where AVIStreamGetFrame returns a image which uses DIB_PAL_COLORS.
Would DIB_PAL_COLORS have any effect on calls to GetPixel and SetPixel, when the image is selected into a device context.
Thanks Again
Richard
|
|
|
|
|
I use createprocess() to execute "notepad" in my application.
I want to show .txt file in it.How can I do this?
|
|
|
|
|
Depending on how you use CreateProcess() there's different ways to do it. In one of the parameters you write something like notepad.exe , just write notepad.exe c:\\test.txt or whatever you call your document then notepad should open it...
- Anders
Money talks, but all mine ever says is "Goodbye!"
|
|
|
|
|
I used this code:
STARTUPINFO si;
PROCESS_INFORMATION pi;
memset (&si, 0, sizeof (STARTUPINFO));
memset (&pi, 0, sizeof (PROCESS_INFORMATION));
si.cb = sizeof (STARTUPINFO);
si.dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES;
si.wShowWindow = SW_SHOW;
CreateProcess("c:\\WINDOWS\\notepad.exe c:\\My Documents\\test.txt",NULL,NULL,NULL,NULL,
CREATE_DEFAULT_ERROR_MODE,NULL,NULL,&si,&pi);
????It doesn't work?
|
|
|
|
|
Try with CreateProcess("c:\\WINDOWS\\notepad.exe \"c:\\My Documents\\test.txt\"",NULL,NULL,NULL,NULL,
CREATE_DEFAULT_ERROR_MODE,NULL,NULL,&si,&pi);
- Anders
Money talks, but all mine ever says is "Goodbye!"
|
|
|
|
|
You have problems with My Docuements, I found the solution converting to ShortPath.
You must to execute with C:\\windows\\notepad.exe C:\\My documents\\test.txt
But C:\\My documents\\test.txt must be used with short path, because the application found a space between My and Documents.
For convert use GetShortPathName(). Or put the test.txt field in other directory without spaces...
Best Regards
Carlos Antollini.
|
|
|
|
|
Use ShellExecute() instead, which will automatically run the app associated with .TXT files.
--Mike--
http://home.inreach.com/mdunn/
#include "witty_sig.h"
your with and
|
|
|
|
|
You should be able to include the file name you want opened on the command line parameter (2nd argument in CreateProcess).
e.g. "notepad.exe c:\test.txt"
Hope this helps,
Bill
|
|
|
|
|
Help me please
I am not able to find my semantic error
I want a pointer to the document (MDI, Doc/View) in the application class.
I tried this way:
void CMyApp::OnFileNew()
{
CWinApp::OnFileNew();
CMainFrame *pFrame = (CMainFrame *)AfxGetMainWnd();
CMyDoc* pDoc = (CMyDoc*)pFrame->GetActiveDocument();
pDoc->SetModifiedFlag(); //Error: Access Violation!!!!!!!
}
Why is pDoc not a pointer to the active document??????
Any suggestion would be very appreciated
thanks in advance
regards
jens
[email]sport(at)web,de[/email]
|
|
|
|
|
I have'nt tried the solution you suggest, and I don't know why it fails. But since the document has nothing to do with the frame class, you should obtain it from the Application class. Copy and paste this code into you CMyApp class:
CCommixDoc* CCommixApp::GetActiveDocument()
{
POSITION pos = GetFirstDocTemplatePosition();
ASSERT(pos);
CDocTemplate* pTempl = GetNextDocTemplate(pos);
ASSERT(pTempl);
pos = pTempl->GetFirstDocPosition();
ASSERT(pos);
if (pos==NULL)
{
return NULL;
}
return (CCommixDoc *)pTempl->GetNextDoc(pos);
}
Christian Skovdal Andersen
"I have a plan so cunning you could put a tail
on it and call it a weasel." -Edmund, Blackadder III
|
|
|
|
|
Hi christian
thanks a lot. I have tried your solution and it works in this case very good
My aim is to open several documents with the belonging childwindows depending on a loop in a global function. And fill the data member of the document class
When i try your solution in the global function:
CMyApp * pApp = (CMyApp *)AfxGetApp();
CMyDoc * pDoc = pApp->GetActiveDocument();
and try to fill the data member of the document class:
pDoc->m_data = x;
i get an error
in your function GetActiveDocument() in the evaluation line:
ASSERT(pos);
Can you explain me this behavior?
What is my error in reasoning?
thanks in advance
regards
jens
|
|
|
|
|
E-mail me a sample, that have the error, and I'll take a look at it.
Christian Skovdal Andersen
"I have a plan so cunning you could put a tail
on it and call it a weasel." -Edmund, Blackadder III
|
|
|
|
|
Hi Christian,
thank you very much for your readiness to help me in this way!
As you can see below, I could solve my problems.
best wishes
jens
|
|
|
|
|
If you have an MDI app you need to go
AfxGetApp()->MDIGetActive()->GetActiveView->GetDocument(); ( or possibly AfxGetApp()->MDIGetActive()->GetActiveDOcument();, but I'm not sure on that one ).
Christian
After all, there's nothing wrong with an elite as long as I'm allowed to be part of it!! - Mike Burston Oct 23, 2001
|
|
|
|
|
Great! fantastic! excellent!
Thank you very mutch for your suggestion
It works fine.
Some small correction:
MDIGetActive() is not a element from derived CWinApp class (CMyApp), but CMDIFrameWnd::MDIGetActive. So I insert a pointer to the CMainFrame:
CMainFrame* pmyFrame = (CMainFrame*)AfxGetMainWnd();
ASSERT_VALID(pmyFrame);
CMyDoc * pmyDoc = (CMyDoc*) pmyFrame->MDIGetActive()->GetActiveDocument();
ASSERT_VALID(pmyDoc);
thank you
regards
jens
|
|
|
|
|
HOWTO: Get Current CDocument or CView from Anywhere
Article ID: Q108587
werks fer me, eh...
|
|
|
|
|