How do you cancel a ToolWindowPane or Visual Studio IDE close operation via a VSPackage?

安稳与你 提交于 2019-12-03 17:07:56

For completeness, there IS a useful way to cancel a close with a tool window (provided by user Chicobo on the msdn forum and repeated here).

  1. In your ToolWindowPane class, implement the IVsWindowFrameNotify2 interface, which provides a method OnClose.

  2. To cancel the window closing, consider using :

    public int OnClose(ref uint pgrfSaveOptions)
    {
        // Check if your content is dirty here, then
    
        // Prompt a dialog
        MessageBoxResult res = MessageBox.Show("This Document has been modified. Do you want to save the changes ?",
                      "Unsaved changes", MessageBoxButton.YesNoCancel, MessageBoxImage.Warning);
        // If the users wants to save
        if (res == MessageBoxResult.Yes)
        {
            // Handle with your "save method here"
        }
    
        if (res == MessageBoxResult.Cancel)
        {
            // If "cancel" is clicked, abort the close
            return VSConstants.E_ABORT;
        }
    
        // Else, exit
        return VSConstants.S_OK;
    }
    

For the purpose of completing the previous answer, the following code snippet targets an unanswered part of the question which is on how to prevent the close operation of the Visual Studio IDE inside a VsPackage:

    protected override int QueryClose(out bool pfCanClose)
    {
        pfCanClose = false;
        return VSConstants.S_OK;
    }

Use the above code inside your package class.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!