VSXtra-like implementation of VS SDK's OptionsPage Sample

Package source code:

using System.Runtime.InteropServices;
using Microsoft.VisualStudio.Shell;
using VSXtra;

namespace DeepDiver.OptionsPage
{
  [PackageRegistration(UseManagedResourcesOnly = true)]
  [DefaultRegistryRoot("Software\\Microsoft\\VisualStudio\\9.0")]
  [InstalledProductRegistration(false, "#110", "#112", "1.0", IconResourceID = 400)]
  [ProvideLoadKey("Standard", "1.0", "OptionsPage", "DeepDiver", 1)]
  [XtraProvideOptionPageAttribute(typeof(OptionsPageGeneral), "My Options Page (C#)", "General", 101, 106, true)]
  [ProvideProfileAttribute(typeof(OptionsPageGeneral), "My Options Page (C#)", "General Options", 101, 106, true, DescriptionResourceID = 101)]
  [XtraProvideOptionPageAttribute(typeof(OptionsPageCustom), "My Options Page (C#)", "Custom", 101, 107, true)]
  [ProvideProfileAttribute(typeof(OptionsPageCustom), "My Options Page (C#)", "Custom", 101, 106, true, DescriptionResourceID = 101)]
  [Guid(GuidList.guidOptionsPagePkgString)]
  public sealed class OptionsPagePackage : PackageBase
  {
  }
}
General Options Page:

using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.Globalization;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using VSXtra;

namespace DeepDiver.OptionsPage
{
  [Guid(GuidList.guidPageGeneral)]
  public class OptionsPageGeneral : DialogPage<OptionsPagePackage>
  {
    public OptionsPageGeneral()
    {
      OptionString = "Hello World";
      OptionInteger = 567;
      CustomSize = new Size(50, 50);
    }

    [Category("String Options"), Description("My string option")]
    public string OptionString { get; set; }

    [Category("Integer Options")] 
    [Description("My integer option")]
    public int OptionInteger { get; set; }

    [Category("Expandable Options")]
    [Description("My Expandable option")]
    public Size CustomSize { get; set; }

    protected override void OnActivate(CancelEventArgs e)
    {
      var result = VsMessageBox.Show(Resources.MessageOnActivateEntered, "OnActivate",
        MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
      if (result == DialogResult.Cancel)
      {
        Trace.WriteLine(string.Format(CultureInfo.CurrentCulture, 
          "Cancelled the OnActivate event"));
        e.Cancel = true;
      }
      base.OnActivate(e);
    }

    protected override void OnClosed(EventArgs e)
    {
      VsMessageBox.Show(Resources.MessageOnClosed);
    }

    protected override void OnDeactivate(CancelEventArgs e)
    {
      var result = VsMessageBox.Show(Resources.MessageOnDeactivateEntered, 
        "OnDeactivate", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
      if (result == DialogResult.Cancel)
      {
        Trace.WriteLine(string.Format(CultureInfo.CurrentCulture, 
          "Cancelled the OnDeactivate event"));
        e.Cancel = true;
      }
    }

    protected override void OnApply(PageApplyEventArgs e)
    {
      var result = VsMessageBox.Show(Resources.MessageOnApplyEntered);
      if (result == DialogResult.Cancel)
      {
        Trace.WriteLine(string.Format(CultureInfo.CurrentCulture, 
          "Cancelled the OnApply event"));
        e.ApplyBehavior = PageApplyKind.Cancel;
      }
      else
      {
        base.OnApply(e);
      }
      VsMessageBox.Show(Resources.MessageOnApply);
    }
  }
}
Custom Options Page:

using System;
using System.ComponentModel;
using System.Runtime.InteropServices;
using VSXtra;

namespace DeepDiver.OptionsPage
{
  [Guid(GuidList.guidPageCustom)]
  public class OptionsPageCustom : DialogPage<OptionsPagePackage, OptionsCompositeControl>
  {
    public OptionsPageCustom()
    {
      CustomBitmap = String.Empty;
    }

    protected override void OnPageCreated()
    {
      UIControl.OptionsPage = this;
    }

    protected override void OnPageDataLoaded()
    {
      UIControl.RefreshImage();
    }

    [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
    public string CustomBitmap { get; set; }
  }
}
UI control for the Custom Options Page:

using System;
using System.IO;
using System.Drawing;
using System.Windows.Forms;

namespace DeepDiver.OptionsPage
{
  public class OptionsCompositeControl : UserControl
  {
    /// <summary>
    /// Explicitly defined default constructor.
    /// Initializes new instance of OptionsCompositeControl class.
    /// </summary>
    public OptionsCompositeControl()
    {
      InitializeComponent();
    }

    private void OnChooseImage(object sender, EventArgs e)
    {
      openImageFileDialog = new OpenFileDialog();

      if ((openImageFileDialog == null) || (DialogResult.OK != openImageFileDialog.ShowDialog()))
        return;
      if (customOptionsPage != null)
        customOptionsPage.CustomBitmap = openImageFileDialog.FileName;
      RefreshImage();
    }
    
    private void OnClearImage(object sender, EventArgs e)
    {
      if (customOptionsPage != null)
        customOptionsPage.CustomBitmap = null;
      RefreshImage();
    }

    public void RefreshImage()
    {
      if (customOptionsPage == null) return;
      var fileName = customOptionsPage.CustomBitmap;
      if (!string.IsNullOrEmpty(fileName))
      {
        // --- Avoid to use Image.FromFile() method for loading image to exclude file locks
        using (var lStream = new FileStream(fileName, FileMode.Open, FileAccess.Read))
        {
          pictureBox.Image = Image.FromStream(lStream);
        }
      }
      else pictureBox.Image = null;
    }

    public OptionsPageCustom OptionsPage
    {
      get { return customOptionsPage; }
      set
      {
        customOptionsPage = value;
        RefreshImage();
      }
    }
  }
}

Screenshots:

OptionsPage1.png

OptionsPage2.png

Last edited Aug 5, 2008 at 8:02 AM by INovak, version 4

Comments

No comments yet.