110,537
社区成员
发帖
与我相关
我的任务
分享
public class REOBJECT
{
public int cbStruct = Marshal.SizeOf(typeof(REOBJECT)); // Size of structure
public int cp; // Character position of object
public Guid clsid; // Class ID of object
public IntPtr poleobj; // OLE object interface
public IStorage pstg; // Associated storage interface
public IOleClientSite polesite; // Associated client site interface
public Size sizel; // Size of object (may be 0,0)
public uint dvAspect; // Display aspect to use
public uint dwFlags; // Object status flags
public uint dwUser; // Dword for user's use
}
包含了丰富的信息,其中CP属性描述当前Ole位于RichtextBox中的位置(他是动态的,会因为实际位置改变而自动变化 ),
poleobj 属性则是一个指针,指向你的图片控件, 是你InsertControl()时插入的控件, 如果插入的是GifBox,可以使用以下代码转换:
GifBox ibx = Marshal.GetObjectForIUnknown(REOBJECT.poleobj) as GifBox;
获取到了图片位置,以及图片,剩下的就是如何将他们告诉给接收者了。[quote=引用 24 楼 air123456789 的回复:] [quote=引用 23 楼 hyblusea 的回复:] 这是效果,需要选择第三种方式:GifBox
[quote=引用 23 楼 hyblusea 的回复:] 这是效果,需要选择第三种方式:GifBox
这是效果,需要选择第三种方式:GifBox
欢迎大家踊跃参加回答问题。
WebBrowser可以做编辑器的 如果是VS自带的WebBrowser控件,有一个MODE的标识的,只要设置为EditMode就是编辑模式了! 更简单的方法是: 在html的body里添加contenteditable="true"属性 如<html><head><title>example</title></head><body contenteditable="true"> 这里是可以编辑的</body> </html>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Diagnostics;
using System.ComponentModel;
namespace ControlLib
{
public class RichTextBoxEx : RichTextBox
{
[DllImport("User32.dll", CharSet = CharSet.Auto)]
public static extern int SendMessage(IntPtr hWnd, int message, IntPtr wParam, IntPtr lParam);
[DllImport("User32.dll", CharSet = CharSet.Auto, PreserveSig = false)]
internal static extern IRichEditOle SendMessage(IntPtr hWnd, int message, int wParam);
[StructLayout(LayoutKind.Sequential)]
private struct CHARFORMAT2_STRUCT
{
public UInt32 cbSize;
public UInt32 dwMask;
public UInt32 dwEffects;
public Int32 yHeight;
public Int32 yOffset;
public Int32 crTextColor;
public byte bCharSet;
public byte bPitchAndFamily;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 32)]
public char[] szFaceName;
public UInt16 wWeight;
public UInt16 sSpacing;
public int crBackColor;
public int lcid;
public int dwReserved;
public Int16 sStyle;
public Int16 wKerning;
public byte bUnderlineType;
public byte bAnimation;
public byte bRevAuthor;
public byte bReserved1;
}
private const int WM_USER = 0x0400;
private const int EM_GETCHARFORMAT = WM_USER + 58;
private const int EM_SETCHARFORMAT = WM_USER + 68;
private const int SCF_SELECTION = 0x0001;
private const UInt32 CFE_LINK = 0x0020;
private const UInt32 CFM_LINK = 0x00000020;
IRichEditOle _richEditOle = null;
IRichEditOle richEditOle
{
get
{
if (this._richEditOle == null)
{
IntPtr ptr = Marshal.AllocCoTaskMem(Marshal.SizeOf(typeof(IntPtr)));
IntPtr richEditOleIntPtr = IntPtr.Zero;
Marshal.WriteIntPtr(ptr, IntPtr.Zero);
try
{
int msgResult = SendMessage(this.Handle, RichEditOle.EM_GETOLEINTERFACE, IntPtr.Zero, ptr);
if (msgResult != 0)
{
IntPtr intPtr = Marshal.ReadIntPtr(ptr);
try
{
if (intPtr != IntPtr.Zero)
{
Guid guid = new Guid("00020D00-0000-0000-c000-000000000046");
Marshal.QueryInterface(intPtr, ref guid, out richEditOleIntPtr);
this._richEditOle = (IRichEditOle)Marshal.GetTypedObjectForIUnknown(richEditOleIntPtr, typeof(IRichEditOle));
}
}
finally
{
Marshal.Release(intPtr);
}
}
}
catch (Exception err)
{
Trace.WriteLine(err.ToString());
}
finally
{
Marshal.FreeCoTaskMem(ptr);
}
}
return this._richEditOle;
}
}
class RichEditOle
{
[DllImport("ole32.dll", PreserveSig = false)]
static extern int CreateILockBytesOnHGlobal(IntPtr hGlobal, bool fDeleteOnRelease, [Out] out ILockBytes ppLkbyt);
[DllImport("ole32.dll")]
static extern int StgCreateDocfileOnILockBytes(ILockBytes plkbyt, uint grfMode, uint reserved, out IStorage ppstgOpen);
public const int EM_GETOLEINTERFACE = 0x0400 + 60;
private RichTextBoxEx richTextBox;
private IRichEditOle ole;
internal RichEditOle(RichTextBoxEx richEdit)
{
this.richTextBox = richEdit;
this.ole = SendMessage(this.richTextBox.Handle, EM_GETOLEINTERFACE, 0);
}
internal void InsertControl(Control ctrl)
{
if (ctrl == null)
return;
Guid guid = Marshal.GenerateGuidForType(ctrl.GetType());
ILockBytes lockBytes;
CreateILockBytesOnHGlobal(IntPtr.Zero, true, out lockBytes);
IStorage storage;
StgCreateDocfileOnILockBytes(lockBytes, (uint)(STGM.STGM_SHARE_EXCLUSIVE | STGM.STGM_CREATE | STGM.STGM_READWRITE), 0, out storage);
IOleClientSite oleClientSite;
this.ole.GetClientSite(out oleClientSite);
REOBJECT reObject = new REOBJECT()
{
cp = this.richTextBox.SelectionStart,
clsid = guid,
pstg = storage,
poleobj = Marshal.GetIUnknownForObject(ctrl),
polesite = oleClientSite,
dvAspect = (uint)(DVASPECT.DVASPECT_CONTENT),
dwFlags = (uint)0x00000002,
};
try
{
reObject.dwUser = 1;
}
catch { }
this.ole.InsertObject(reObject);
Marshal.ReleaseComObject(lockBytes);
Marshal.ReleaseComObject(oleClientSite);
Marshal.ReleaseComObject(storage);
}
}
public RichTextBoxEx()
: base()
{
base.BorderStyle = BorderStyle.FixedSingle;
this.DetectUrls = false;
}
public new bool ReadOnly { get; set; }
protected override void WndProc(ref Message m)
{
switch (m.Msg)
{
case 0x0100:
if (this.ReadOnly)
return;
break;
case 0X0102:
if (this.ReadOnly)
return;
break;
default:
break;
}
base.WndProc(ref m);
}
List<MyGIF> gifList = new List<MyGIF>();
Panel gifPanel = new Panel();
[DefaultValue(false)]
public new bool DetectUrls
{
get { return base.DetectUrls; }
set { base.DetectUrls = value; }
}
public void InsertGIF(string Name, Image Data)
{
//MyGIF gif = new MyGIF(Name, Data);
//gif.Box.Invalidate();
//this.gifPanel.Controls.Add(gif.Box);
//this.gifList.Add(gif);
//RichEditOle ole = new RichEditOle(this);
//ole.InsertControl(gif);
//this.Invalidate();
}
public void InsertControl(Control ctrl)
{
this.gifPanel.Controls.Add(ctrl);
RichEditOle ole = new RichEditOle(this);
ole.InsertControl(ctrl);
this.Invalidate();
}
public string GetGIFInfo()
{
string imageInfo = "";
REOBJECT reObject = new REOBJECT();
for (int i = 0; i < this.richEditOle.GetObjectCount(); i++)
{
this.richEditOle.GetObject(i, reObject, GETOBJECTOPTIONS.REO_GETOBJ_ALL_INTERFACES);
MyGIF gif = this.gifList.Find(p => p != null && p.Index == reObject.dwUser);
if (gif != null)
{
imageInfo += reObject.cp.ToString() + ":" + gif.Name + "|";
}
}
return imageInfo;
}
}
}
<html><head><title>example</title></head><body contenteditable="true"> 这里是可以编辑的</body> </html>
向richtextbox中插入一个picturebox控件 控件中放置gif 可以插入任何control http://www.codeproject.com/Articles/134358/TRichTextBox-A-universal-RichTextBox-which-can-dis
复制 粘贴的话 楼主可以修改源码中的 List<MetaInfo> ControlList = new List<MetaInfo>(); 为public 就可以获取当前richtextbox中所有已插入的控件 找到对应的picturebox 复制其中的image 粘贴的时候 再用image生成一个picturebox插入到另外一个richtextbox中