I am still trying to get the Undo function work on Internet Explorer editor
hosted as activex in my c# application.
Some time ago I've found a note at msdn that says "undo is not supported". I
gave up.
But lately I've downloaded LutzRoeder Writer that makes use of undo
function. I've looked at the code and it turns out that it uses
IOleCommandTarget interface to invoke this command.
Because I cannot get it to work, I have questions:
1. is it a good way to follow? the undo does not work when invoked by
execCommand on IHtmlDocument. is there a chance that it will work with
IOleCommandTarget then?
2. after few days of fighting, digging through msdn and the groups archive,
I've came up with following code but it still does not work. when asking for
CanUndo I always get 0.
could anyone share his/her experiences with me and tell me where the problem
could be? I can send the full source code of my webbrowser control if
needed.
thanks in advance,
Wiktor Zychla
----------------------------------------------------------------------------
---
public class MyWebBrowser
{
// the rest of the code is not here
public enum MiscCommandTarget
{
Find = 1, ViewSource = 2, Options = 3, Undo = 43
}
internal int GetCommandInfo( MiscCommandTarget commandId )
{
OLECMD[] ole = new OLECMD[1];
ole[0] = new OLECMD();
ole[0].cmdID = (int)commandId;
OLECMDTEXT ret = new OLECMDTEXT();
((IOleCommandTarget)Document).QueryStatus( ref cmdGuid, 1, ole, ret );
return ( (int)ole[0].cmdf >> 1 );
}
internal bool IsEnabled( MiscCommandTarget commandId )
{
return ( (this.GetCommandInfo( commandId ) & 1) != 0);
}
internal object Execute( MiscCommandTarget command )
{
return this.Execute( command, null );
}
internal object Execute( MiscCommandTarget command, object[] arguments )
{
object[] array = new object[1];
int n = ((IOleCommandTarget)Document).Exec( ref cmdGuid, (int)command,
2, ref arguments, ref array );
if ( n != 0 )
{
MessageBox.Show( string.Concat( "Execution of MSHTML command ID '",
command, "' failed.") );
}
return array[0];
}
public bool CanUndo
{
get
{
return this.IsEnabled(MiscCommandTarget.Undo);
}
}
public void Undo()
{
if ( this.CanUndo ) this.Execute( MiscCommandTarget.Undo );
}
}
[StructLayout(LayoutKind.Sequential)]
public struct OLECMD
{
public int cmdID;
public int cmdf;
}
[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Unicode)]
public struct OLECMDTEXT
{
public int cmdtextf;
public int cwActual;
private int cwBuf;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=256)]
public string text;
}
[ComImport, Guid("b722bccb-4e68-101b-a2bc-00aa00404770"),
InterfaceType(ComInterfaceType.InterfaceIsIUnknown )]
public interface IOleCommandTarget
{
[PreserveSig()]
int QueryStatus(ref Guid pguidCmdGroup,
int cCmds,
[In(), Out()]
OLECMD[] prgCmds,
[In(), Out()]
OLECMDTEXT cmdText );
[PreserveSig()]
int Exec(ref Guid pguidCmdGroup, int nCmdId, int nCmdExecOpt, ref object[]
pvaIn, ref object[] pvaOut);
}