
2009年4月21日
.Net中的RichTextBox control采用的是很古老的native Windows RichText control,它缺少支持表格元素中的\par,所以当粘贴表格进来时,如果有换行,则格式会错乱。Windows XP中的写字板对表格支持得很好,它采用了msftedit.dll。我们可以考虑自己写一个RichTextBox5,继承自System.Windows.Forms.RichTextBox,然后就可以彻底解决这个问题。代码如下:
using System;
using System.ComponentModel;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace EasyNote
{
public class RichTextBox5 : RichTextBox
{
private static IntPtr moduleHandle;
protected override CreateParams CreateParams
{
get
{
if (moduleHandle == IntPtr.Zero)
{
moduleHandle = LoadLibrary("msftedit.dll");
if ((long)moduleHandle < 0x20)
{
throw new Win32Exception(Marshal.GetLastWin32Error(), "Could not load Msftedit.dll");
}
}
CreateParams createParams = base.CreateParams;
createParams.ClassName = "RichEdit50W";
if (this.Multiline)
{
if (((this.ScrollBars & RichTextBoxScrollBars.Horizontal) != RichTextBoxScrollBars.None) && !base.WordWrap)
{
createParams.Style |= 0x100000;
if ((this.ScrollBars & ((RichTextBoxScrollBars)0x10)) != RichTextBoxScrollBars.None)
{
createParams.Style |= 0x2000;
}
}
if ((this.ScrollBars & RichTextBoxScrollBars.Vertical) != RichTextBoxScrollBars.None)
{
createParams.Style |= 0x200000;
if ((this.ScrollBars & ((RichTextBoxScrollBars)0x10)) != RichTextBoxScrollBars.None)
{
createParams.Style |= 0x2000;
}
}
}
if ((BorderStyle.FixedSingle == base.BorderStyle) && ((createParams.Style & 0x800000) != 0))
{
createParams.Style &= -8388609;
createParams.ExStyle |= 0x200;
}
return createParams;
}
}
// P/Invoke declarations
[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]
private static extern IntPtr LoadLibrary(string path);
}
}
posted @ 2009-04-21 08:53 清晨阳光 阅读(614) 评论(0)
编辑