Skip to content

Instantly share code, notes, and snippets.

@hexagit
Last active June 1, 2018 01:09
Show Gist options
  • Save hexagit/c5f0cc1ec0c52719bc86a237fb96c6b9 to your computer and use it in GitHub Desktop.
Save hexagit/c5f0cc1ec0c52719bc86a237fb96c6b9 to your computer and use it in GitHub Desktop.
using System;
using System.ComponentModel.Design;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Runtime.InteropServices;
using Microsoft.VisualStudio;
using Microsoft.VisualStudio.OLE.Interop;
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.Shell.Interop;
using Microsoft.Win32;
namespace VSIXTest2
{
/// <summary>
/// This is the class that implements the package exposed by this assembly.
/// </summary>
/// <remarks>
/// <para>
/// The minimum requirement for a class to be considered a valid package for Visual Studio
/// is to implement the IVsPackage interface and register itself with the shell.
/// This package uses the helper classes defined inside the Managed Package Framework (MPF)
/// to do it: it derives from the Package class that provides the implementation of the
/// IVsPackage interface and uses the registration attributes defined in the framework to
/// register itself and its components with the shell. These attributes tell the pkgdef creation
/// utility what data to put into .pkgdef file.
/// </para>
/// <para>
/// To get loaded into VS, the package must be referred by &lt;Asset Type="Microsoft.VisualStudio.VsPackage" ...&gt; in .vsixmanifest file.
/// </para>
/// </remarks>
[PackageRegistration(UseManagedResourcesOnly = true)]
[ProvideAutoLoad(UIContextGuids80.SolutionExists)]
[InstalledProductRegistration("#110", "#112", "1.0", IconResourceID = 400)] // Info on this package for Help/About
[Guid(VSPackageTest.PackageGuidString)]
[SuppressMessage("StyleCop.CSharp.DocumentationRules", "SA1650:ElementDocumentationMustBeSpelledCorrectly", Justification = "pkgdef, VS and vsixmanifest are valid VS terms")]
public sealed class VSPackageTest : Package
{
/// <summary>
/// VSPackageTest GUID string.
/// </summary>
public const string PackageGuidString = "326ade09-034d-48fa-96d4-9150a3cbcdf1";
/// <summary>
/// Initializes a new instance of the <see cref="VSPackageTest"/> class.
/// </summary>
public VSPackageTest()
{
// Inside this method you can place any initialization code that does not require
// any Visual Studio service because at this point the package object is created but
// not sited yet inside Visual Studio environment. The place to do all the other
// initialization is the Initialize method.
}
#region Package Members
/// <summary>
/// Initialization of the package; this method is called right after the package is sited, so this is the place
/// where you can put all the initialization code that rely on services provided by VisualStudio.
/// </summary>
protected override void Initialize()
{
base.Initialize();
var sp = GetGlobalService(typeof(Microsoft.VisualStudio.OLE.Interop.IServiceProvider)) as Microsoft.VisualStudio.OLE.Interop.IServiceProvider;
if( sp == null ) return;
var uiShell = (IVsUIShell)GetService(typeof(SVsUIShell));
var rdt = new RunningDocumentTable(new ServiceProvider(sp));
_docEvents = new DocEvents(rdt, uiShell);
}
protected override void Dispose(bool disposing)
{
if( disposing ) {
if( _docEvents != null ) {
_docEvents.Dispose();
_docEvents = null;
}
}
base.Dispose(disposing);
}
private DocEvents _docEvents = null;
#endregion
}
class DocEvents : IDisposable, IVsRunningDocTableEvents3
{
public DocEvents(RunningDocumentTable rdt, IVsUIShell uiShell)
{
_rdt = rdt;
_ui = uiShell;
_myCookie = VSConstants.VSCOOKIE_NIL;
if( _rdt == null ) return;
_myCookie = _rdt.Advise(this);
}
public void Dispose()
{
if( _rdt != null && _myCookie != VSConstants.VSCOOKIE_NIL ) {
_rdt.Unadvise(_myCookie);
}
}
public int OnAfterFirstDocumentLock(uint docCookie, uint dwRDTLockType, uint dwReadLocksRemaining, uint dwEditLocksRemaining)
{
return VSConstants.S_OK;
}
public int OnBeforeLastDocumentUnlock(uint docCookie, uint dwRDTLockType, uint dwReadLocksRemaining, uint dwEditLocksRemaining)
{
return VSConstants.S_OK;
}
public int OnBeforeSave(uint docCookie)
{
return VSConstants.S_OK;
}
public int OnAfterSave(uint docCookie)
{
if( _rdt == null || _ui == null ) return VSConstants.S_OK;
// 保存したファイル情報を表示
var docInfo = _rdt.GetDocumentInfo(docCookie);
var id = Guid.Empty;
int result;
ErrorHandler.ThrowOnFailure(
_ui.ShowMessageBox(
0,
ref id,
"File Saved",
docInfo.Moniker,
string.Empty,
0,
OLEMSGBUTTON.OLEMSGBUTTON_OK,
OLEMSGDEFBUTTON.OLEMSGDEFBUTTON_FIRST,
OLEMSGICON.OLEMSGICON_INFO,
0,
out result
)
);
return VSConstants.S_OK;
}
public int OnAfterAttributeChange(uint docCookie, uint grfAttribs)
{
return VSConstants.S_OK;
}
public int OnAfterAttributeChangeEx(uint docCookie, uint grfAttribs, IVsHierarchy pHierOld, uint itemidOld, string pszMkDocumentOld, IVsHierarchy pHierNew, uint itemidNew, string pszMkDocumentNew)
{
return VSConstants.S_OK;
}
public int OnBeforeDocumentWindowShow(uint docCookie, int fFirstShow, IVsWindowFrame pFrame)
{
return VSConstants.S_OK;
}
public int OnAfterDocumentWindowHide(uint docCookie, IVsWindowFrame pFrame)
{
return VSConstants.S_OK;
}
private readonly RunningDocumentTable _rdt;
private readonly IVsUIShell _ui;
private readonly uint _myCookie;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment