简体   繁体   中英

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 aa 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 ?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM