Click here to Skip to main content
15,914,416 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I understand I need to override the OnFileOpen to customize my app after reading a few articles here and at this MSDN link[^]. But, I can't figure out how to make my SDI-Doc/View-CFormView app open a single file on app startup without having to select the file from a dialog. How do I accomplish this simple but elusive solution?

Please be kind ;)

-DrB
Posted

1 solution

If you want to open a specific file upon starting your application, you can modify the CCommandLineInfo after parsing. This method can be also used to avoid starting with a new document.

An example to optional load a file at program start or just start without any document might look like this:

C++
CMyApp::OnInitInstance()
{
    ...
    CCommandLineInfo cmdInfo;
    ParseCommandLine(cmdInfo);
    // No file name passed on command line
    if (cmdInfo.m_nShellCommand == CCommandLineInfo::FileNew)
    {
	cmdInfo.m_nShellCommand = CCommandLineInfo::FileNothing;
	if (GetProfileInt(_T("Config"), _T("OpenAtStart")))
	{
            cmdInfo.m_strFileName = GetProfileString(
                _T("Config"), _T("InitialFile"));
            if (!cmdInfo.m_strFileName.IsEmpty())
                cmdInfo.m_nShellCommand = CCommandLineInfo::FileOpen;
        }
    }
    ProcessShellCommand(cmdInfo);
    ...
}
 
Share this answer
 
Comments
DrBones69 13-Jan-12 22:23pm    
I'm not sure how to use this or if it what I'm looking for.

I created an MFC SDI DOC/VIEW w/CFormView as my base class. I would like to "attach" my file to the application when I launch it. I'm using serialize and saving data when the app detects a change. If I choose the menu command OPEN and select my file, my app opens the file and my form is populated with the first record in the file, this is what I would like to happen automatically when I launch my app. How can I achieve this?

Thanks
Jochen Arndt 14-Jan-12 5:15am    
My code opens the file specified in cmdInfo.m_strFileName upon starting the application when no file is specified on the command line. If a file name is specified on the command line (executing 'MyApp.exe filename') the file specified by the command is loaded. I think this is what you want. Give it a try (remove the GetProfile() calls and initialize cmdInfo.mstrFileName with your file name). ProcessShellCommmand() opens the file (calls OnFileOpen and all other file opening functions) when command is set to FileOpen.

To register your file name extension and start your application when double clicking a file with your extension in the Explorer, call EnableShellOpen() and RegisterShellFileTypes() from within InitInstance().
DrBones69 14-Jan-12 17:54pm    
Thanks for your help +5, I found that replacing ParseCommandLine(cmdInfo) with cmInfo.ParseParam(_T("MyDir\\MyFile"), FALSE, TRUE) works exactly like I needed.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900