📄 richtextbox.cs
字号:
[PreserveSig]
int SetLinkAvailable(int iob, bool fAvailable);
[return: MarshalAs(UnmanagedType.I4)]
[PreserveSig]
int SetDvaspect(int iob, uint dvaspect);
[return: MarshalAs(UnmanagedType.I4)]
[PreserveSig]
int HandsOffStorage(int iob);
[return: MarshalAs(UnmanagedType.I4)]
[PreserveSig]
int SaveCompleted(int iob, IStorage lpstg);
[return: MarshalAs(UnmanagedType.I4)]
[PreserveSig]
int InPlaceDeactivate();
[return: MarshalAs(UnmanagedType.I4)]
[PreserveSig]
int ContextSensitiveHelp(bool fEnterMode);
[return: MarshalAs(UnmanagedType.I4)]
[PreserveSig]
int GetClipboardData([In, Out] ref CHARRANGE lpchrg, [MarshalAs(UnmanagedType.U4)] GETCLIPBOARDDATAFLAGS reco, out IDataObject lplpdataobj);
[return: MarshalAs(UnmanagedType.I4)]
[PreserveSig]
int ImportDataObject(IDataObject lpdataobj, int cf, IntPtr hMetaPict);
}
#endregion
#region Public Enums
// Enum for possible RTF colors
public enum RtfColor
{
Black, Maroon, Green, Olive, Navy, Purple, Teal, Gray, Silver,
Red, Lime, Yellow, Blue, Fuchsia, Aqua, White
}
#endregion
public class myDataObject : IDataObject
{
private Bitmap mBitmap;
public FORMATETC mpFormatetc;
#region IDataObject Members
private const uint S_OK = 0;
private const uint E_POINTER = 0x80004003;
private const uint E_NOTIMPL = 0x80004001;
private const uint E_FAIL = 0x80004005;
public uint GetData(ref FORMATETC pFormatetc, ref STGMEDIUM pMedium)
{
IntPtr hDst = mBitmap.GetHbitmap();
pMedium.tymed = (int)TYMED.TYMED_GDI;
pMedium.unionmember = hDst;
pMedium.pUnkForRelease = IntPtr.Zero;
return (uint)S_OK;
}
public uint GetDataHere(ref FORMATETC pFormatetc, out STGMEDIUM pMedium)
{
Trace.WriteLine("GetDataHere");
pMedium = new STGMEDIUM();
return (uint)E_NOTIMPL;
}
public uint QueryGetData(ref FORMATETC pFormatetc)
{
Trace.WriteLine("QueryGetData");
return (uint)E_NOTIMPL;
}
public uint GetCanonicalFormatEtc(ref FORMATETC pFormatetcIn, out FORMATETC pFormatetcOut)
{
Trace.WriteLine("GetCanonicalFormatEtc");
pFormatetcOut = new FORMATETC();
return (uint)E_NOTIMPL;
}
public uint SetData(ref FORMATETC a, ref STGMEDIUM b, bool fRelease)
{
//mpFormatetc = pFormatectIn;
//mpmedium = pmedium;
Trace.WriteLine("SetData");
return (int)S_OK;
}
public uint EnumFormatEtc(uint dwDirection, IEnumFORMATETC penum)
{
Trace.WriteLine("EnumFormatEtc");
return (int)S_OK;
}
public uint DAdvise(ref FORMATETC a, int advf, IAdviseSink pAdvSink, out uint pdwConnection)
{
Trace.WriteLine("DAdvise");
pdwConnection = 0;
return (uint)E_NOTIMPL;
}
public uint DUnadvise(uint dwConnection)
{
Trace.WriteLine("DUnadvise");
return (uint)E_NOTIMPL;
}
public uint EnumDAdvise(out IEnumSTATDATA ppenumAdvise)
{
Trace.WriteLine("EnumDAdvise");
ppenumAdvise = null;
return (uint)E_NOTIMPL;
}
#endregion
public myDataObject()
{
mBitmap = new Bitmap(16, 16);
mpFormatetc = new FORMATETC();
}
public void SetImage(string strFilename)
{
try
{
mBitmap = (Bitmap)Bitmap.FromFile(strFilename, true);
mpFormatetc.cfFormat = CLIPFORMAT.CF_BITMAP; // Clipboard format = CF_BITMAP
mpFormatetc.ptd = IntPtr.Zero; // Target Device = Screen
mpFormatetc.dwAspect = DVASPECT.DVASPECT_CONTENT; // Level of detail = Full content
mpFormatetc.lindex = -1; // Index = Not applicaple
mpFormatetc.tymed = TYMED.TYMED_GDI; // Storage medium = HBITMAP handle
}
catch
{
}
}
public void SetImage(Image image)
{
try
{
mBitmap = new Bitmap(image);
mpFormatetc.cfFormat = CLIPFORMAT.CF_BITMAP; // Clipboard format = CF_BITMAP
mpFormatetc.ptd = IntPtr.Zero; // Target Device = Screen
mpFormatetc.dwAspect = DVASPECT.DVASPECT_CONTENT; // Level of detail = Full content
mpFormatetc.lindex = -1; // Index = Not applicaple
mpFormatetc.tymed = TYMED.TYMED_GDI; // Storage medium = HBITMAP handle
}
catch
{
}
}
}
public class MyExtRichTextBox : RichTextBox,IDisposable
{
#region RichTextBoxPlus Members
protected IRichEditOle IRichEditOleValue = null;
protected IntPtr IRichEditOlePtr = IntPtr.Zero;
public IRichEditOle GetRichEditOleInterface()
{
if (this.IRichEditOleValue == null)
{
//REOBJECT reObject = new REOBJECT();
//reObject.cp = 0;
//reObject.dwFlags = GetObjectOptions.REO_GETOBJ_POLEOBJ;
//IntPtr ptr = Marshal.AllocCoTaskMem(reObject.cbStruct);
//Marshal.StructureToPtr(reObject, ptr, false);
// Allocate the ptr that EM_GETOLEINTERFACE will fill in.
IntPtr ptr = Marshal.AllocCoTaskMem(Marshal.SizeOf(typeof(IntPtr))); // Alloc the ptr.
Marshal.WriteIntPtr(ptr, IntPtr.Zero); // Clear it.
try
{
if (0 != API.SendMessage(this.Handle, Messages.EM_GETOLEINTERFACE, IntPtr.Zero, ptr))
{
// Read the returned pointer.
IntPtr pRichEdit = Marshal.ReadIntPtr(ptr);
try
{
if (pRichEdit != IntPtr.Zero)
{
// Query for the IRichEditOle interface.
Guid guid = new Guid("00020D00-0000-0000-c000-000000000046");
Marshal.QueryInterface(pRichEdit, ref guid, out this.IRichEditOlePtr);
// Wrap it in the C# interface for IRichEditOle.
this.IRichEditOleValue = (IRichEditOle)Marshal.GetTypedObjectForIUnknown(this.IRichEditOlePtr, typeof(IRichEditOle));
if (this.IRichEditOleValue == null)
{
throw new Exception("Failed to get the object wrapper for the interface.");
}
}
else
{
throw new Exception("Failed to get the pointer.");
}
}
finally
{
Marshal.Release(pRichEdit);
}
}
else
{
throw new Exception("EM_GETOLEINTERFACE failed.");
}
}
catch (Exception err)
{
Trace.WriteLine(err.ToString());
this.ReleaseRichEditOleInterface();
}
finally
{
// Free the ptr memory.
Marshal.FreeCoTaskMem(ptr);
//Marshal.DestroyStructure(ptr, typeof(REOBJECT));
}
}
return this.IRichEditOleValue;
}
/// <summary>
/// Releases the IRichEditOle interface if it hasn't been already.
/// </summary>
/// <remarks>This is automatically called in Dispose if needed.</remarks>
public void ReleaseRichEditOleInterface()
{
if (this.IRichEditOlePtr != IntPtr.Zero)
{
Marshal.Release(this.IRichEditOlePtr);
}
this.IRichEditOlePtr = IntPtr.Zero;
this.IRichEditOleValue = null;
}
#endregion
#region IDisposable Members
// public void Dispose()
// {
// this.ReleaseRichEditOleInterface();
// }
#endregion
#region Imports and structs
// It makes no difference if we use PARAFORMAT or
// PARAFORMAT2 here, so I have opted for PARAFORMAT2.
[StructLayout( LayoutKind.Sequential )]
public struct PARAFORMAT
{
public int cbSize;
public uint dwMask;
public short wNumbering;
public short wReserved;
public int dxStartIndent;
public int dxRightIndent;
public int dxOffset;
public short wAlignment;
public short cTabCount;
[MarshalAs( UnmanagedType.ByValArray, SizeConst = 32 )]
public int[] rgxTabs;
// PARAFORMAT2 from here onwards.
public int dySpaceBefore;
public int dySpaceAfter;
public int dyLineSpacing;
public short sStyle;
public byte bLineSpacingRule;
public byte bOutlineLevel;
public short wShadingWeight;
public short wShadingStyle;
public short wNumberingStart;
public short wNumberingStyle;
public short wNumberingTab;
public short wBorderSpace;
public short wBorderWidth;
public short wBorders;
}
[ StructLayout( LayoutKind.Sequential )]
public struct CHARFORMAT
{
public int 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;
// CHARFORMAT2 from here onwards.
public short wWeight;
public short sSpacing;
public Int32 crBackColor;
public uint lcid;
public uint dwReserved;
public short sStyle;
public short wKerning;
public byte bUnderlineType;
public byte bAnimation;
public byte bRevAuthor;
public byte bReserved1;
}
[DllImport( "user32", CharSet = CharSet.Auto )]
private static extern int SendMessage( HandleRef hWnd,
int msg,
int wParam,
int lParam );
[DllImport( "user32", CharSet = CharSet.Auto )]
private static extern int SendMessage( HandleRef hWnd,
int msg,
int wParam,
ref PARAFORMAT lp );
[DllImport( "user32", CharSet = CharSet.Auto )]
private static extern int SendMessage( HandleRef hWnd,
int msg,
int wParam,
ref CHARFORMAT lp );
private const int EM_SETEVENTMASK = 1073;
private const int WM_SETREDRAW = 11;
[DllImport("User32.dll", CharSet=CharSet.Auto,PreserveSig=false)]
public static extern IRichEditOle SendMessage(IntPtr hWnd, int message, int wParam);
[DllImport("user32.dll", ExactSpelling=true, CharSet=CharSet.Auto)]
internal static extern bool GetClientRect(IntPtr hWnd, [In, Out] ref Rectangle rect);
[DllImport("user32.dll", ExactSpelling=true, CharSet=CharSet.Auto)]
internal static extern bool GetWindowRect(IntPtr hWnd, [In, Out] ref Rectangle rect);
[DllImport("user32.dll", ExactSpelling=true, CharSet=CharSet.Auto)]
internal static extern IntPtr GetParent(IntPtr hWnd);
[DllImport("ole32.dll")]
static extern int OleSetContainedObject([MarshalAs(UnmanagedType.IUnknown)]
object pUnk, bool fContained);
[DllImport("ole32.dll")]
static extern int OleLoadPicturePath(
[MarshalAs(UnmanagedType.LPWStr)] string lpszPicturePath,
[MarshalAs(UnmanagedType.IUnknown)][In] object pIUnknown,
uint dwReserved,
uint clrReserved,
ref Guid riid,
[MarshalAs(UnmanagedType.IUnknown)] out object ppvObj);
[DllImport("ole32.dll")]
static extern int OleCreateFromFile([In] ref Guid rclsid,
[MarshalAs(UnmanagedType.LPWStr)] string lpszFileName, [In] ref Guid riid,
uint renderopt, ref FORMATETC pFormatEtc, IOleClientSite pClientSite,
IStorage pStg, [MarshalAs(UnmanagedType.IUnknown)] out object ppvObj);
[DllImport("ole32.dll")]
static extern int OleCreateFromData(IDataObject pSrcDataObj,
[In] ref Guid riid, uint renderopt, ref FORMATETC pFormatEtc,
IOleClientSite pClientSite, IStorage pStg,
[MarshalAs(UnmanagedType.IUnknown)] out object ppvObj);
[DllImport("ole32.dll")]
static extern int OleCreateStaticFromData([MarshalAs(UnmanagedType.Interface)]IDataObject pSrcDataObj,
[In] ref Guid riid, uint renderopt, ref FORMATETC pFormatEtc,
IOleClientSite pClientSite, IStorage pStg,
[MarshalAs(UnmanagedType.IUnknown)] out object ppvObj);
[DllImport("ole32.dll")]
static extern int OleCreateLinkFromData([MarshalAs(UnmanagedType.Interface)]IDataObject pSrcDataObj,
[In] ref Guid riid, uint renderopt, ref FORMATETC pFormatEtc,
IOleClientSite pClientSite, IStorage pStg,
[MarshalAs(UnmanagedType.IUnknown)] out object ppvObj);
#endregion
#region MyExtRichTextBox Members
public void InsertOleObject(IOleObject oleObj)
{
RichEditOle ole = new RichEditOle(this);
ole.InsertOleObject(oleObj);
}
public void InsertMyControl(Control control)
{
RichEditOle ole = new RichEditOle(this);
ole.InsertControl(control);
}
public void InsertMyDataObject(myDataObject mdo)
{
RichEditOle ole = new RichEditOle(this);
ole.InsertMyDataObject(mdo);
}
public void UpdateObjects()
{
RichEditOle ole=new RichEditOle(this);
ole.UpdateObjects();
}
public void InsertMyImage(Image image)
{
myDataObject mdo = new myDataObject();
mdo.SetImage(image);
this.InsertMyDataObject(mdo);
}
public void InsertMyImage(string imageFile)
{
myDataObject mdo = new myDataObject();
mdo.SetImage(imageFile);
this.InsertMyDataObject(mdo);
}
public void InsertMyImageFromFile(string strFilename)
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -