VS2019 Add Project Property page

576 views Asked by At

I am trying to extend the VS2019 project properties page and add a new one but I can't get to work if I use the Microsoft guide:

Adding and removing property pages

Does anyone maybe have a sample code?

EDIT:

My Project

I currently have and normal VSIX Project that I used to create an Editor package and added menu items successfully. I basically just want to add a project property page that will be used on all "Class Library" projects.

1

There are 1 answers

4
Mr Qian On

Does anyone maybe have a sample code?

Actually, follow the guidance and you can get what you want.

Follow this guidance:

Remove a property page

1) create a vsix c# project in VS2019 IDE and then add a new class like RemovePage

2) then add these namespaces in this file:

using EnvDTE;
using Microsoft.VisualStudio;
using Microsoft.VisualStudio.Shell.Interop;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

and implement the IVsHierarchy interface and add its implementation methods.

3) After that, on the bottom of the page, add the main function to realize it:

protected int GetProperty(uint itemId, int propId, out object property)
        {
            //Use propId to filter configuration-independent property pages.
            switch (propId)
            {

                case (int)__VSHPROPID2.VSHPROPID_PropertyPagesCLSIDList:
                    {
                        //Get a semicolon-delimited list of clsids of the configuration-independent property pages


                        ErrorHandler.ThrowOnFailure(GetProperty(itemId, propId, out property));
                        string propertyPagesList = ((string)property).ToUpper(CultureInfo.InvariantCulture);
                        //Remove the property page here

                        string buildEventsPageGuid = "{1E78F8DB-6C07-4D61-A18F-7514010ABD56}";
                        int index = propertyPagesList.IndexOf(buildEventsPageGuid);
                        if (index != -1)
                        {
                            // GUIDs are separated by ';' so if you remove the last GUID, also remove the last ';'
                            int index2 = index + buildEventsPageGuid.Length + 1;
                            if (index2 >= propertyPagesList.Length)
                                propertyPagesList = propertyPagesList.Substring(0, index).TrimEnd(';');
                            else
                                propertyPagesList = propertyPagesList.Substring(0, index) + propertyPagesList.Substring(index2);


                        }
                        //New property value
                        property = propertyPagesList;

                        break; 
                    }

            }

Add a property page

1) add a new class called DeployPropertyPage and implement Form and Microsoft.VisualStudio.OLE.Interop.IPropertyPage. After it, add the implementation method as prompted.

2) add these namespaces:

using Microsoft.VisualStudio.OLE.Interop;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Microsoft.VisualStudio.Shell;
using MSVSIP = Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio;
using Microsoft.VisualStudio.Shell.Interop;

3) change the default function GetPageInfo into:

 public void GetPageInfo(Microsoft.VisualStudio.OLE.Interop.PROPPAGEINFO[] pPageInfo)
        {
            PROPPAGEINFO info = new PROPPAGEINFO();
            info.cb = (uint)Marshal.SizeOf(typeof(PROPPAGEINFO));
            info.dwHelpContext = 0;
            info.pszDocString = null;
            info.pszHelpFile = null;
            info.pszTitle = "Deployment";  //Assign tab name
            info.SIZE.cx = this.Size.Width;
            info.SIZE.cy = this.Size.Height;
            if (pPageInfo != null && pPageInfo.Length > 0)
                pPageInfo[0] = info;
        }

4) add the main function as the document said on the bottom of the file:

protected int GetProperty(uint itemId, int propId, out object property)
        {
            //Use propId to filter configuration-dependent property pages.
            switch (propId)
            {

           case (int)__VSHPROPID2.VSHPROPID_CfgPropertyPagesCLSIDList:
            {
                //Get a semicolon-delimited list of clsids of the configuration-dependent property pages.
                ErrorHandler.ThrowOnFailure(GetProperty(itemId, propId, out property));
                //Add the Deployment property page.
                property += ';' + typeof(DeployPropertyPage).GUID.ToString("B");

                 break;
            }
        }

       return GetProperty(itemId, propId, out property);
    }

5) then register your new property page before the class name DeployPropertyPage.

[MSVSIP.ProvideObject(typeof(DeployPropertyPage), RegisterUsing = RegistrationMethod.CodeBase)]

Update 1

DeployPropertyPagelike these:

class DeployPropertyPage:IPropertyPage
    {
        public void SetPageSite(IPropertyPageSite pPageSite)
        {
            throw new NotImplementedException();
        }

        public void Activate(IntPtr hWndParent, RECT[] pRect, int bModal)
        {
            throw new NotImplementedException();
        }

        public void Deactivate()
        {
            throw new NotImplementedException();
        }

        public void GetPageInfo(PROPPAGEINFO[] pPageInfo)
        {
            throw new NotImplementedException();
        }

        public void SetObjects(uint cObjects, object[] ppunk)
        {
            throw new NotImplementedException();
        }

        public void Show(uint nCmdShow)
        {
            throw new NotImplementedException();
        }

        public void Move(RECT[] pRect)
        {
            throw new NotImplementedException();
        }

        public int IsPageDirty()
        {
            throw new NotImplementedException();
        }

        public int Apply()
        {
            throw new NotImplementedException();
        }

        public void Help(string pszHelpDir)
        {
            throw new NotImplementedException();
        }

        public int TranslateAccelerator(MSG[] pMsg)
        {
            throw new NotImplementedException();
        }
    }

RemovePage like this:

 class RemovePage: IVsHierarchy
    {
        public int SetSite(Microsoft.VisualStudio.OLE.Interop.IServiceProvider psp)
        {
            throw new NotImplementedException();
        }

        public int GetSite(out Microsoft.VisualStudio.OLE.Interop.IServiceProvider ppSP)
        {
            throw new NotImplementedException();
        }

        public int QueryClose(out int pfCanClose)
        {
            throw new NotImplementedException();
        }

        public int Close()
        {
            throw new NotImplementedException();
        }

        public int GetGuidProperty(uint itemid, int propid, out Guid pguid)
        {
            throw new NotImplementedException();
        }

        public int SetGuidProperty(uint itemid, int propid, ref Guid rguid)
        {
            throw new NotImplementedException();
        }

        public int GetProperty(uint itemid, int propid, out object pvar)
        {
            throw new NotImplementedException();
        }

        public int SetProperty(uint itemid, int propid, object var)
        {
            throw new NotImplementedException();
        }

        public int GetNestedHierarchy(uint itemid, ref Guid iidHierarchyNested, out IntPtr ppHierarchyNested, out uint pitemidNested)
        {
            throw new NotImplementedException();
        }

        public int GetCanonicalName(uint itemid, out string pbstrName)
        {
            throw new NotImplementedException();
        }

        public int ParseCanonicalName(string pszName, out uint pitemid)
        {
            throw new NotImplementedException();
        }

        public int Unused0()
        {
            throw new NotImplementedException();
        }

        public int AdviseHierarchyEvents(IVsHierarchyEvents pEventSink, out uint pdwCookie)
        {
            throw new NotImplementedException();
        }

        public int UnadviseHierarchyEvents(uint dwCookie)
        {
            throw new NotImplementedException();
        }

        public int Unused1()
        {
            throw new NotImplementedException();
        }

        public int Unused2()
        {
            throw new NotImplementedException();
        }

        public int Unused3()
        {
            throw new NotImplementedException();
        }

        public int Unused4()
        {
            throw new NotImplementedException();
        }
    }

And then you can modify your code in these methods.

Hope it could help you.