// [!output APP_IMPL] : Defines the class behaviors for the application. // #include "stdafx.h" #include "[!output APP_HEADER]" #include "[!output MAIN_FRAME_HEADER]" [!if APP_TYPE_MDI] #include "[!output CHILD_FRAME_HEADER]" [!endif] [!if MINI_SERVER || FULL_SERVER || CONTAINER_SERVER] #include "[!output INPLACE_FRAME_HEADER]" [!endif] [!if OLEDB_RECORD_VIEW || ODBC_RECORD_VIEW] #include "[!output ROWSET_HEADER]" [!endif] [!if DOCVIEW] #include "[!output DOC_HEADER]" [!if PROJECT_STYLE_EXPLORER] #include "[!output TREE_VIEW_HEADER]" [!else] #include "[!output VIEW_HEADER]" [!endif] [!endif] #ifdef _DEBUG #define new DEBUG_NEW #endif // [!output APP_CLASS] BEGIN_MESSAGE_MAP([!output APP_CLASS], [!output APP_BASE_CLASS]) ON_COMMAND(ID_APP_ABOUT, OnAppAbout) [!if APP_TYPE_MTLD] ON_COMMAND(ID_FILE_NEW_FRAME, OnFileNewFrame) [!if DOCVIEW] ON_COMMAND(ID_FILE_NEW, OnFileNew) [!endif] [!endif] [!if !DOCVIEW] [!if APP_TYPE_MDI] ON_COMMAND(ID_FILE_NEW, OnFileNew) [!endif] [!endif] [!if DOCVIEW] [!if !DB_VIEW_NO_FILE] // Standard file based document commands [!if !APP_TYPE_MTLD] ON_COMMAND(ID_FILE_NEW, &[!output APP_BASE_CLASS]::OnFileNew) [!endif] ON_COMMAND(ID_FILE_OPEN, &[!output APP_BASE_CLASS]::OnFileOpen) [!endif] [!endif] [!if PRINTING && DOCVIEW] // Standard print setup command ON_COMMAND(ID_FILE_PRINT_SETUP, &[!output APP_BASE_CLASS]::OnFilePrintSetup) [!endif] END_MESSAGE_MAP() // [!output APP_CLASS] construction [!output APP_CLASS]::[!output APP_CLASS]() { [!if CONTEXT_HELP && HELPSTYLE_HTML] EnableHtmlHelp(); [!endif] // TODO: add construction code here, // Place all significant initialization in InitInstance } [!if ATL_SUPPORT] // ATL Module object CComModule _Module; [!endif] // The one and only [!output APP_CLASS] object [!output APP_CLASS] theApp; [!if MINI_SERVER || FULL_SERVER || CONTAINER_SERVER || AUTOMATION] // This identifier was generated to be statistically unique for your app // You may change it if you prefer to choose a specific identifier // {[!output APP_CLSID_REGISTRY_FORMAT]} static const CLSID clsid = [!output APP_CLSID_STATIC_CONST_GUID_FORMAT]; [!endif] [!if AUTOMATION] const GUID CDECL BASED_CODE _tlid = [!output LIBID_STATIC_CONST_GUID_FORMAT]; const WORD _wVerMajor = 1; const WORD _wVerMinor = 0; [!endif] // [!output APP_CLASS] initialization BOOL [!output APP_CLASS]::InitInstance() { [!if MANIFEST] // InitCommonControls() is required on Windows XP if an application // manifest specifies use of ComCtl32.dll version 6 or later to enable // visual styles. Otherwise, any window creation will fail. InitCommonControls(); [!endif] [!output APP_BASE_CLASS]::InitInstance(); [!if OLEDB] CoInitialize(NULL); [!endif] [!if SOCKETS] if (!AfxSocketInit()) { AfxMessageBox(IDP_SOCKETS_INIT_FAILED); return FALSE; } [!endif] [!if CONTAINER || MINI_SERVER || FULL_SERVER || CONTAINER_SERVER || AUTOMATION || OLEDB || ACTIVEX_CONTROLS || ACCESSIBILITY] // Initialize OLE libraries if (!AfxOleInit()) { AfxMessageBox(IDP_OLE_INIT_FAILED); return FALSE; } [!endif] [!if ACTIVEX_CONTROLS] AfxEnableControlContainer(); [!endif] // Standard initialization // If you are not using these features and wish to reduce the size // of your final executable, you should remove from the following // the specific initialization routines you do not need // Change the registry key under which our settings are stored // TODO: You should modify this string to be something appropriate // such as the name of your company or organization SetRegistryKey(_T("Local AppWizard-Generated Applications")); [!if DOCVIEW] LoadStdProfileSettings([!output MRU_SIZE]); // Load standard INI file options (including MRU) [!endif] [!if !DOCVIEW] // To create the main window, this code creates a new frame window // object and then sets it as the application's main window object [!if APP_TYPE_MDI] CMDIFrameWnd* pFrame = new [!output MAIN_FRAME_CLASS]; [!else] [!if !DOCVIEW] CMainFrame* pFrame = new [!output MAIN_FRAME_CLASS]; [!else] CRuntimeClass* pClass = RUNTIME_CLASS(CMainFrame); CFrameWnd* pFrame = (CFrameWnd*) pClass->CreateObject(); ASSERT_KINDOF(CFrameWnd, pFrame); [!endif] [!endif] if (!pFrame) return FALSE; m_pMainWnd = pFrame; [!if APP_TYPE_SDI] [!if DOCVIEW] // load the frame against an empty context CCreateContext context; [!else] // create and load the frame with its resources [!endif] pFrame->LoadFrame(IDR_MAINFRAME, WS_OVERLAPPEDWINDOW | FWS_ADDTOTITLE, NULL, [!if DOCVIEW] &context); [!else] NULL); [!endif] [!else] // create main MDI frame window if (!pFrame->LoadFrame(IDR_MAINFRAME)) return FALSE; // try to load shared MDI menus and accelerator table //TODO: add additional member variables and load calls for // additional menu types your application may need HINSTANCE hInst = AfxGetResourceHandle(); m_hMDIMenu = ::LoadMenu(hInst, MAKEINTRESOURCE(IDR_[!output SAFE_DOC_TYPE_NAME]TYPE)); m_hMDIAccel = ::LoadAccelerators(hInst, MAKEINTRESOURCE(IDR_[!output SAFE_DOC_TYPE_NAME]TYPE)); [!endif] [!else] // Register the application's document templates. Document templates // serve as the connection between documents, frame windows and views [!if APP_TYPE_MDI] CMultiDocTemplate* pDocTemplate; pDocTemplate = new CMultiDocTemplate(IDR_[!output SAFE_DOC_TYPE_NAME]TYPE, [!else] [!if APP_TYPE_MTLD] CMultiDocTemplate* pDocTemplate; pDocTemplate = new CMultiDocTemplate( IDR_MAINFRAME, [!else] CSingleDocTemplate* pDocTemplate; pDocTemplate = new CSingleDocTemplate( IDR_MAINFRAME, [!endif] [!endif] RUNTIME_CLASS([!output DOC_CLASS]), [!if APP_TYPE_MDI] RUNTIME_CLASS([!output CHILD_FRAME_CLASS]), // custom MDI child frame [!else] RUNTIME_CLASS([!output MAIN_FRAME_CLASS]), // main SDI frame window [!endif] [!if PROJECT_STYLE_EXPLORER] RUNTIME_CLASS([!output TREE_VIEW_CLASS])); [!else] RUNTIME_CLASS([!output VIEW_CLASS])); [!endif] if (!pDocTemplate) return FALSE; [!if CONTAINER || CONTAINER_SERVER] [!if APP_TYPE_MDI] pDocTemplate->SetContainerInfo(IDR_[!output SAFE_DOC_TYPE_NAME]TYPE_CNTR_IP); [!else] pDocTemplate->SetContainerInfo(IDR_CNTR_INPLACE); [!endif] [!endif] [!if MINI_SERVER || FULL_SERVER || CONTAINER_SERVER] pDocTemplate->SetServerInfo( [!if APP_TYPE_MDI] IDR_[!output SAFE_DOC_TYPE_NAME]TYPE_SRVR_EMB, IDR_[!output SAFE_DOC_TYPE_NAME]TYPE_SRVR_IP, [!else] IDR_SRVR_EMBEDDED, IDR_SRVR_INPLACE, [!endif] RUNTIME_CLASS([!output INPLACE_FRAME_CLASS])); [!endif] [!if APP_TYPE_MTLD] m_pDocTemplate = pDocTemplate; [!endif] AddDocTemplate(pDocTemplate); [!if MINI_SERVER || FULL_SERVER || CONTAINER_SERVER || AUTOMATION] // Connect the COleTemplateServer to the document template // The COleTemplateServer creates new documents on behalf // of requesting OLE containers by using information // specified in the document template [!if APP_TYPE_MDI || APP_TYPE_MTLD] m_server.ConnectTemplate(clsid, pDocTemplate, FALSE); // Register all OLE server factories as running. This enables the // OLE libraries to create objects from other applications COleTemplateServer::RegisterAll(); // Note: MDI applications register all server objects without regard // to the /Embedding or /Automation on the command line [!else] m_server.ConnectTemplate(clsid, pDocTemplate, TRUE); // Note: SDI applications register server objects only if /Embedding // or /Automation is present on the command line [!endif] [!endif] [!endif] [!if APP_TYPE_MDI && DOCVIEW] // create main MDI Frame window [!output MAIN_FRAME_CLASS]* pMainFrame = new [!output MAIN_FRAME_CLASS]; if (!pMainFrame || !pMainFrame->LoadFrame(IDR_MAINFRAME)) { delete pMainFrame; return FALSE; } m_pMainWnd = pMainFrame; [!if !MINI_SERVER] // call DragAcceptFiles only if there's a suffix // In an MDI app, this should occur immediately after setting m_pMainWnd [!if HAS_SUFFIX && !HTML_EDITVIEW] // Enable drag/drop open m_pMainWnd->DragAcceptFiles(); [!endif] [!endif] [!endif] [!if !MINI_SERVER] [!if HAS_SUFFIX && !HTML_EDITVIEW] // Enable DDE Execute open EnableShellOpen(); RegisterShellFileTypes(TRUE); [!endif] [!endif] [!if DOCVIEW] // Parse command line for standard shell commands, DDE, file open CCommandLineInfo cmdInfo; ParseCommandLine(cmdInfo); [!endif] [!if MINI_SERVER || FULL_SERVER || CONTAINER_SERVER || AUTOMATION] // App was launched with /Embedding or /Automation switch. // Run app as automation server. if (cmdInfo.m_bRunEmbedded || cmdInfo.m_bRunAutomated) { [!if !APP_TYPE_MDI && !APP_TYPE_MTLD] // Register all OLE server factories as running. This enables the // OLE libraries to create objects from other applications COleTemplateServer::RegisterAll(); [!endif] // Don't show the main window return TRUE; } // App was launched with /Unregserver or /Unregister switch. Unregister // typelibrary. Other unregistration occurs in ProcessShellCommand(). else if (cmdInfo.m_nShellCommand == CCommandLineInfo::AppUnregister) { [!if !MINI_SERVER] [!if HAS_SUFFIX && !HTML_EDITVIEW] UnregisterShellFileTypes(); [!endif] [!endif] [!if MINI_SERVER || FULL_SERVER || CONTAINER_SERVER] [!if ACTIVE_DOC_SERVER] m_server.UpdateRegistry(OAT_DOC_OBJECT_SERVER, NULL, NULL, FALSE); [!else] m_server.UpdateRegistry(OAT_INPLACE_SERVER, NULL, NULL, FALSE); [!endif] [!else] [!if AUTOMATION] m_server.UpdateRegistry(OAT_DISPATCH_OBJECT, NULL, NULL, FALSE); [!endif] [!endif] [!if AUTOMATION] AfxOleUnregisterTypeLib(_tlid, _wVerMajor, _wVerMinor); [!endif] [!if MINI_SERVER] return FALSE; [!endif] } // App was launched standalone or with other switches (e.g. /Register // or /Regserver). Update registry entries, including typelibrary. else { [!if MINI_SERVER || FULL_SERVER || CONTAINER_SERVER] [!if ACTIVE_DOC_SERVER] m_server.UpdateRegistry(OAT_DOC_OBJECT_SERVER); [!else] m_server.UpdateRegistry(OAT_INPLACE_SERVER); [!endif] [!else] [!if AUTOMATION] m_server.UpdateRegistry(OAT_DISPATCH_OBJECT); [!endif] [!endif] [!if AUTOMATION] COleObjectFactory::UpdateRegistryAll(); AfxOleRegisterTypeLib(AfxGetInstanceHandle(), _tlid); [!endif] [!if MINI_SERVER] if (cmdInfo.m_nShellCommand == CCommandLineInfo::AppRegister) return FALSE; [!endif] } [!endif] [!if MINI_SERVER] // When a mini-server is run stand-alone the registry is updated and the // user is instructed to use the Insert Object dialog in a container // to use the server. Mini-servers do not have stand-alone user interfaces AfxMessageBox(IDP_USE_INSERT_OBJECT); return FALSE; [!else] [!if DOCVIEW] // Dispatch commands specified on the command line. Will return FALSE if // app was launched with /RegServer, /Register, /Unregserver or /Unregister. if (!ProcessShellCommand(cmdInfo)) return FALSE; [!endif] [!if APP_TYPE_MDI] // The main window has been initialized, so show and update it [!if DOCVIEW] [!if MAIN_FRAME_MAXIMIZED] pMainFrame->ShowWindow(SW_SHOWMAXIMIZED); [!else] [!if MAIN_FRAME_MINIMIZED] pMainFrame->ShowWindow(SW_SHOWMINIMIZED); [!else] [!if APP_TYPE_SDI || APP_TYPE_MTLD] pMainFrame->ShowWindow(SW_SHOW); [!else] pMainFrame->ShowWindow(m_nCmdShow); [!endif] [!endif] [!endif] pMainFrame->UpdateWindow(); [!else] [!if MAIN_FRAME_MAXIMIZED] pFrame->ShowWindow(SW_SHOWMAXIMIZED); [!else] [!if MAIN_FRAME_MINIMIZED] pFrame->ShowWindow(SW_SHOWMINIMIZED); [!else] [!if APP_TYPE_SDI || APP_TYPE_MTLD] pFrame->ShowWindow(SW_SHOW); [!else] pFrame->ShowWindow(m_nCmdShow); [!endif] [!endif] [!endif] pFrame->UpdateWindow(); [!endif] [!endif] [!if APP_TYPE_SDI || APP_TYPE_MTLD] // The one and only window has been initialized, so show and update it [!if DOCVIEW] [!if MAIN_FRAME_MAXIMIZED] m_pMainWnd->ShowWindow(SW_SHOWMAXIMIZED); [!else] [!if MAIN_FRAME_MINIMIZED] m_pMainWnd->ShowWindow(SW_SHOWMINIMIZED); [!else] [!if APP_TYPE_SDI || APP_TYPE_MTLD] m_pMainWnd->ShowWindow(SW_SHOW); [!else] m_pMainWnd->ShowWindow(m_nCmdShow); [!endif] [!endif] [!endif] m_pMainWnd->UpdateWindow(); [!else] [!if MAIN_FRAME_MAXIMIZED] pFrame->ShowWindow(SW_SHOWMAXIMIZED); [!else] [!if MAIN_FRAME_MINIMIZED] pFrame->ShowWindow(SW_SHOWMINIMIZED); [!else] [!if APP_TYPE_SDI || APP_TYPE_MTLD] pFrame->ShowWindow(SW_SHOW); [!else] pFrame->ShowWindow(m_nCmdShow); [!endif] [!endif] [!endif] pFrame->UpdateWindow(); [!endif] // call DragAcceptFiles only if there's a suffix // In an SDI app, this should occur after ProcessShellCommand [!if HAS_SUFFIX && !HTML_EDITVIEW] // Enable drag/drop open m_pMainWnd->DragAcceptFiles(); [!endif] [!endif] return TRUE; [!endif] } [!if !DOCVIEW] // [!output APP_CLASS] message handlers [!if APP_TYPE_MDI] [!if !DOCVIEW] int [!output APP_CLASS]::ExitInstance() { //TODO: handle additional resources you may have added if (m_hMDIMenu != NULL) FreeResource(m_hMDIMenu); if (m_hMDIAccel != NULL) FreeResource(m_hMDIAccel); return CWinApp::ExitInstance(); } [!endif] [!endif] [!if APP_TYPE_MDI || DOCVIEW] void [!output APP_CLASS]::OnFileNew() { [!if APP_TYPE_SDI] CString strUntitled; CFrameWnd* pFrameWnd = DYNAMIC_DOWNCAST(CFrameWnd, m_pMainWnd); if (pFrameWnd != NULL) { //TODO: close and destroy any file-specific data you've got // in memory. Once done, execute the following if statement // to reset the title of the frame window if (strUntitled.LoadString(AFX_IDS_UNTITLED)) pFrameWnd->UpdateFrameTitleForDocument(strUntitled); else pFrameWnd->UpdateFrameTitleForDocument(NULL); } [!else] [!output MAIN_FRAME_CLASS]* pFrame = STATIC_DOWNCAST([!output MAIN_FRAME_CLASS], m_pMainWnd); // create a new MDI child window pFrame->CreateNewChild( RUNTIME_CLASS([!output CHILD_FRAME_CLASS]), IDR_[!output SAFE_DOC_TYPE_NAME]TYPE, m_hMDIMenu, m_hMDIAccel); [!endif] } [!endif] [!endif] // CAboutDlg dialog used for App About class CAboutDlg : public CDialog { public: CAboutDlg(); // Dialog Data enum { IDD = IDD_ABOUTBOX }; protected: virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support // Implementation protected: DECLARE_MESSAGE_MAP() }; CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD) { [!if ACCESSIBILITY] EnableActiveAccessibility(); [!endif] } void CAboutDlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); } BEGIN_MESSAGE_MAP(CAboutDlg, CDialog) END_MESSAGE_MAP() // App command to run the dialog void [!output APP_CLASS]::OnAppAbout() { CAboutDlg aboutDlg; aboutDlg.DoModal(); } // [!output APP_CLASS] message handlers [!if APP_TYPE_MTLD] [!if DOCVIEW] void [!output APP_CLASS]::OnFileNewFrame() { _ASSERTE(m_pDocTemplate != NULL); CDocument* pDoc = NULL; CFrameWnd* pFrame = NULL; // Create a new instance of the document referenced // by the m_pDocTemplate member. pDoc = m_pDocTemplate->CreateNewDocument(); if (pDoc != NULL) { // If creation worked, use create a new frame for // that document. pFrame = m_pDocTemplate->CreateNewFrame(pDoc, NULL); if (pFrame != NULL) { // Set the title, and initialize the document. // If document initialization fails, clean-up // the frame window and document. m_pDocTemplate->SetDefaultTitle(pDoc); if (!pDoc->OnNewDocument()) { pFrame->DestroyWindow(); pFrame = NULL; } else { // Otherwise, update the frame m_pDocTemplate->InitialUpdateFrame(pFrame, pDoc, TRUE); } } } // If we failed, clean up the document and show a // message to the user. if (pFrame == NULL || pDoc == NULL) { delete pDoc; AfxMessageBox(AFX_IDP_FAILED_TO_CREATE_DOC); } } void [!output APP_CLASS]::OnFileNew() { CDocument* pDoc = NULL; CFrameWnd* pFrame; pFrame = DYNAMIC_DOWNCAST(CFrameWnd, CWnd::GetActiveWindow()); if (pFrame != NULL) pDoc = pFrame->GetActiveDocument(); if (pFrame == NULL || pDoc == NULL) { // if it's the first document, create as normal CWinApp::OnFileNew(); } else { // Otherwise, see if we have to save modified, then // ask the document to reinitialize itself. if (!pDoc->SaveModified()) return; CDocTemplate* pTemplate = pDoc->GetDocTemplate(); _ASSERTE(pTemplate != NULL); pTemplate->SetDefaultTitle(pDoc); pDoc->OnNewDocument(); } } [!else] void [!output APP_CLASS]::OnFileNewFrame() { CMainFrame* pFrame = new CMainFrame; pFrame->LoadFrame(IDR_MAINFRAME, WS_OVERLAPPEDWINDOW | FWS_ADDTOTITLE, NULL, NULL); pFrame->ShowWindow(SW_SHOW); pFrame->UpdateWindow(); m_aryFrames.Add(pFrame->GetSafeHwnd()); } [!endif] [!endif]