コード例 #1
0
        /// <summary>
        /// This function is the callback used to execute the command when the menu item is clicked.
        /// See the constructor to see how the menu item is associated with this function using
        /// OleMenuCommandService service and MenuCommand class.
        /// </summary>
        /// <param name="sender">Event sender.</param>
        /// <param name="e">Event args.</param>
        private void Execute(object sender, EventArgs e)
        {
            ThreadHelper.ThrowIfNotOnUIThread();

            object item   = ProjectUtils.GetSelectedItem();
            string folder = ProjectUtils.FindFolder(item);

            if (string.IsNullOrEmpty(folder) || !Directory.Exists(folder))
            {
                return;
            }

            ProjectItem selectedItem    = item as ProjectItem;
            Project     selectedProject = item as Project;
            Project     project         = selectedItem?.ContainingProject ?? selectedProject ?? ProjectUtils.GetActiveProject();

            if (project == null)
            {
                throw new Exception("Could not find project!"); // TODO remove exception after testing
            }

            // Get files
            if (PromptForFileName(folder))
            {
                var cppInfo = new FileInfo(m_CppFilePath);
                var hppInfo = new FileInfo(m_HppFilePath);

                ProjectItem projectItemCpp = project.AddFileToProject(cppInfo);
                ProjectItem projectItemHpp = project.AddFileToProject(hppInfo);

                if (projectItemCpp == null || projectItemHpp == null)
                {
                    // We have a problem here!
                    MessageBox.Show("Could not create project files!");
                    return;
                }

                project.Save();

                VsShellUtilities.OpenDocument(this.package, cppInfo.FullName);
                VsShellUtilities.OpenDocument(this.package, hppInfo.FullName);

                CppSourceManagerPackage.ms_DTE.ExecuteCommand("SolutionExplorer.SyncWithActiveDocument");
                CppSourceManagerPackage.ms_DTE.ActiveDocument.Activate();
            }
        }