Link to home
Start Free TrialLog in
Avatar of tantormedia
tantormediaFlag for United States of America

asked on

MFC: OnFileOpen problem

Dear experts,

I have an MFC application. My application class is derived from CWinApp. It implements OnFileOpen(). When the user selects File -> Open, this method is called. But my users usually open documents in another way: they just drag a document file to the .exe file, and the document opens with my program. But this way, OnFileOpen() is not called. Where should I place my code then to make sure it is called in any scenario?
Thanks.
Avatar of jkr
jkr
Flag of Germany image

'OnDropFiles()' is the place for that, see http://msdn.microsoft.com/en-us/library/62zys01d%28v=VS.100%29.aspx and http://www.codeproject.com/KB/dialog/JibDragDrop.aspx ("Drag and Drop in a Dialog") for an example.
Avatar of tantormedia

ASKER

Thank you for your answer. But I am not sure this is what I need. The user doesn't drop anything over a window, he drops the document file over the program's executable in Windows Explorer.
ASKER CERTIFIED SOLUTION
Avatar of jkr
jkr
Flag of Germany image

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
The problem is that I need my code to be executed after opening document, not on opening the program istself.
Well, in this case here, 'opening the document' and 'opening the program' are the same thing.
But I can e.g. open the program without opening a document.  Then do File->Open.
Yes, that's clear. But when a file is dropped on your executable in explorer, this place is what opens the file after your app has been started.
You see, what the code should do, depends on version of the document that is being opened. So if I put my code in InitInstance(), and then open document with File->Open, the code will not be executed again. And placing it in document's OnOpenDocument() doesn't work for me, either...
This is the code I want to execute after opening document. You will see why it probably will not work from InitInstance:
if(FILE_VERSION > ((CBookDoc*)pDoc)->GetFileWriteVersion())
		{
			if(AfxMessageBox("This .bok file was created using an older version of the Book Program.\r\n"
				"Would you like to save it now with a different name to have all new features available?",
				MB_YESNO|MB_ICONQUESTION) == IDYES)
			{
				((CBookDoc*)pDoc)->DoFileSaveAs(FALSE, false);
			}
		}

Open in new window

Well, if you are doing that via serialization, you can get the version via 'CArchive::GetObjectSchema()', e.g.
IMPLEMENT_SERIAL(CMyObject, CObject, VERSIONABLE_SCHEMA|1)

void CMyObject::Serialize(CArchive& ar) 
{
   if (ar.IsLoading())
   {
      int nVersion = ar.GetObjectSchema();

      switch(nVersion)
      {
      case 0:
         // read in previous version of 
         // this object
         break;
      case 1:
         // read in current version of
         // this object
         break;
      default:
         // report unknown version of 
         // this object
         break;
      }
   }
   else
   {
      // Normal storing code goes here
   }

Open in new window

No, I am afraid, I am not doing this via serialization.
Well, then how are you doing it?
Doing what exactly?
Thank you for your help. I placed my code in a separate method, and call it from InitInstance() as you said with a condition if a document is already open (in case it was dragged over the exe), and also I call it from OnFileOpen()  after opening the file.