Thursday, February 17, 2011

MSHTML: How can you clear the undo / redo buffer of MSHTML

I'm using a C# wrapper control similar to the WebBrowser control that contains the COM / unmanaged MSHTML control. I'm using this control in the edit mode which allows the user of the application to edit a a HTML document in a WYSIWYG manner.

This control manages it's own undo / redo stack.

How can I reset / clear it so that user will not be able to redo / undo changes to the content of the document, but only be able to edit it ?

From stackoverflow
  • To clear the undo stack of MSHTML control you can use undo manager service.
    When enabling and disabling the undo service, the undo stack is cleared. To extract the undo manager out of the Document object of MSHTML you need to use the IServiceProvider.

    The solution to this is some thing like:

        //Extract undo manager
        if (m_undoManager == null) 
        {
          IServiceProvider serviceProvider = Document as IServiceProvider;
    
          Guid undoManagerGuid = typeof(IOleUndoManager).GUID;
          Guid undoManagerGuid2 = typeof(IOleUndoManager).GUID;
          IntPtr undoManagerPtr = ComSupport.NullIntPtr;
    
          int hr = serviceProvider.QueryService(ref undoManagerGuid2, ref undoManagerGuid, out undoManagerPtr);
          if ((hr == HRESULT.S_OK) && (undoManagerPtr != ComSupport.NullIntPtr))
          {
            m_undoManager = (IOleUndoManager)Marshal.GetObjectForIUnknown(undoManagerPtr);
            Marshal.Release(undoManagerPtr);
          }
        }
    
        //And to clear the stack 
        m_undoManager.Enable(true);
        Application.DoEvents();
    

    More detailed implementation and more information can be seen at:

    http://postxing.net:8080/PostXING/tags/v1.1/PostXING.HtmlComponent/Html/

    http://msdn.microsoft.com/en-us/library/ms678623(VS.85).aspx

0 comments:

Post a Comment