How to cancel IVsWindowFrame close via VSPackage

毅磊 朱 1 Reputation point
2022-08-29T01:28:05.543+00:00

I saw a workaround about how to cancel a ToolWindowPane close operation via a VSPackage, but ToolWindowPane is still closed when canceling the close.

Create a windowpane:

IVsWindowFrame windowframe;  
  
ThreadHelper.ThrowIfNotOnUIThread();  
var uiShell = (IVsUIShell) Package.GetGlobalService(typeof(SVsUIShell));  
  
var result = uiShell.CreateToolWindow((uint) __VSCREATETOOLWIN.CTW_fMultiInstance,  
        (uint) id, editorPane.Control, ref guidNull,   
        ref toolWindowPersistenceGuid,   
        ref guidNull, null, editorPane.Caption, position, out windowFrame);  
  
ErrorHandler.ThrowOnFailure(result);  
   
windowFrame.SetProperty((int) __VSFPROPID.VSFPROPID_FrameMode, VSFRAMEMODE.VSFM_MdiChild);  

ToolWindowListener implement IVsWindowFrameNotify and IVsWindowFrameNotify3:

Advise Event:

private void SubscribeForEvents()  
{  
    if (WindowPane == null)  
        throw new InvalidOperationException();  
  
    ThreadHelper.ThrowIfNotOnUIThread();  
    // ReSharper disable once SuspiciousTypeConversion.Global  
    var frame = WindowPane.Frame as IVsWindowFrame2;  
      
    if (frame == null)  
        throw new InvalidOperationException();  
    if (_cookie != 0U)  
        throw new InvalidOperationException();  
          
    ErrorHandler.ThrowOnFailure(frame.Advise(this, out _cookie));  
}  

On Close:

public int OnClose(ref uint pgrfSaveOptions)  
{  
    if (MessageBox.Show("Closed?", "", MessageBoxButton.YesNo) == MessageBoxResult.Yes)  
    {  
        return VSConstants.S_OK;  
    }  
    else  
    {  
        return VSConstants.E_ABORT;  
    }  
}  

What do i miss in code?

Visual Studio
Visual Studio
A family of Microsoft suites of integrated development tools for building applications for Windows, the web and mobile devices.
4,643 questions
{count} votes