private ArrayList CopyUndoUnitsToArray(IEnumOleUndoUnits undoUnits) { // get a list of all of the undo units ArrayList undoUnitArray = new ArrayList(); IOleUndoUnit current; int result; while ((result = undoUnits.Next(1, out current, IntPtr.Zero)) == HRESULT.S_OK) undoUnitArray.Add(current); // return the list in an array return undoUnitArray; }
private static List <IOleUndoUnit> RemoveTop(IOleUndoManager undoManager, IEnumOleUndoUnits enumerator, int count) { List <IOleUndoUnit> backupList = new List <IOleUndoUnit>(); List <IOleUndoUnit> returnList = new List <IOleUndoUnit>(); int hr; if (count > 0) { uint returned = 1; // backup all existing undo units while (returned > 0) { IOleUndoUnit[] units = new IOleUndoUnit[10]; hr = enumerator.Next((uint)units.Length, units, out returned); Marshal.ThrowExceptionForHR(hr); if (returned == 10) { backupList.AddRange(units); } else if (returned > 0) { for (int i = 0; i < returned; i++) { backupList.Add(units[i]); } } } // put units back except those which should be removed if (backupList.Count > 0) { PoisonPillUndoUnit pill = new PoisonPillUndoUnit(); undoManager.Add(pill); // clear stack undoManager.DiscardFrom(pill); // add units back for (int i = 0; i < backupList.Count - count; i++) { undoManager.Add(backupList[i]); } // return top "count" units or less, if there's not enough if (count <= backupList.Count) { returnList = backupList.GetRange(backupList.Count - count, count); } else { returnList = backupList; } } } returnList.Reverse(); return(returnList); }