'富文本框 WebBrowser里的KindEditor方法属性
<System.Runtime.InteropServices.ComVisibleAttribute(True)> _
Public Class KindEditor
Public isReadOnly As Boolean =False '是否只读
Private _FullHtmlBindingField As String
Private _InnerHtmlBindingField As String
Private _FullHtml As String
Private _InnerHtml As String
'完整html内容
Property FullHtml() As String
Get '获取属性的值
Return _FullHtml
End Get
Set(ByVal value As String) '设定属性的值
_FullHtml = value
'同步更新绑定的列
If _FullHtmlBindingField <> "" Then
Dim vs() As String = _FullHtmlBindingField.split(".")
If vs.Length =2 Then
Tables(vs(0)).Current(vs(1)) = value
End If
End If
End Set
End Property
'纯文本内容
Property InnerHtml() As String
Get '获取属性的值
Return _InnerHtml
End Get
Set(ByVal value As String) '设定属性的值
'1用正则表达式去掉所有空格和换行符
Static rgx As New RegularExpressions.Regex("\s")
value = rgx.Replace(value,"")
'2去掉base64的图片
Dim si As Integer = value.IndexOf("<imgsrc=""data:image/png;base64,")
If si>-1 Then
Dim ei As Integer = value.IndexOf("/>",si)
Do While (ei>si AndAlso si >-1)
value = value.Remove(si,ei-si+2)
si = value.IndexOf("<imgsrc=""data:image/png;base64,")
If si >-1 Then
ei = value.IndexOf("/>",si)
End If
Loop
End If
'3去掉第二种格式的base64的图片
si = value.IndexOf("<imgwidth=")
If si>-1 Then
Dim ei As Integer = value.IndexOf("/>",si)
Do While (ei>si AndAlso si >-1)
value = value.Remove(si,ei-si+2)
si = value.IndexOf("<imgwidth=")
If si >-1 Then
ei = value.IndexOf("/>",si)
End If
Loop
End If
_InnerHtml =value
'同步更新绑定的列
If _InnerHtmlBindingField <> "" Then
Dim vs() As String = _InnerHtmlBindingField.split(".")
If vs.Length =2 Then
Tables(vs(0)).Current(vs(1)) = value
End If
End If
End Set
End Property
'把html内容绑定到某个列
Property FullHtmlBindingField() As String
Get '获取属性的值
Return _FullHtmlBindingField
End Get
Set(ByVal value As String) '设定属性的值
'把当前列内容写入到FullHtml
If value <> "" Then
Dim vs() As String = value.split(".")
If vs.Length =2 Then
_FullHtml = Tables(vs(0)).Current(vs(1))
End If
End If
_FullHtmlBindingField= value
End Set
End Property
'把纯文本内容绑定到某个列
Property InnerHtmlBindingField() As String
Get '获取属性的值
Return _InnerHtmlBindingField
End Get
Set(ByVal value As String) '设定属性的值
'把当前列内容写入到InnerHtml
If value <> "" Then
Dim vs() As String = value.split(".")
If vs.Length =2 Then
_InnerHtml = Tables(vs(0)).Current(vs(1))
End If
End If
_InnerHtmlBindingField =value
End Set
End Property
End Class'从注册表升级WebBrowser内核为IE11,HKEY_CURRENT_USER的注册表修改不需要管理员权限.
'Foxtable安装时自动提升过,但是编译后的程序,依然默认使用IE6内核,需要手动升,否则会出现截图无法直接粘贴
Try
Registry.SetValue("HKEY_CURRENT_USER\SOFTWARE\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BROWSER_EMULATION",System.Diagnostics.Process.GetCurrentProcess.ProcessName & ".exe",11001)
Catch ex As Exception
MessageBox.Show("狐表调用IE11内核失败,会导致富文本框无法粘贴图片" & vbcrlf & "Error:" & ex.Message )
End Try
Dim wb = e.Form.Controls("webBrowser1").BaseControl
wb.Url = new System.Uri(ProjectPath & "KindEditor\e.html", System.UriKind.Absolute)
Dim ke As new KindEditor '必须创建一个新实例
wb.ObjectForScripting =ke3.3把html写入富文本框
Dim wb = e.Form.Controls("WebBrowser1").BaseControl
Dim ke = wb.ObjectForScripting
Dim r As Row = Tables("表A").Current
If r IsNot Nothing Then
'把内容写入富文本框里
ke.FullHtml = r("完整html内容")
wb.refresh '刷新
End IfDim wb = e.Form.Controls("WebBrowser1").BaseControl
Dim ke = wb.ObjectForScripting
MessageBox.show(ke.FullHtml)3.5从富文本框读取纯文本
Dim wb = e.Form.Controls("WebBrowser1").BaseControl
Dim ke = wb.ObjectForScripting
MessageBox.show(ke.InnerHtml)在全局代码,利用正则和截取,过滤好了空格、换行符、base64图片。这个纯文本,其实是为了方便存入数据库搜索。
Dim wb As Object = e.Form.Controls("webBrowser1").BaseControl
Dim ke = wb.ObjectForScripting
ke.IsReadOnly =True
wb.refreshDim dlg As New OpenFileDialog
dlg.Filter = "Word文件|*.doc;*.docx"
If dlg.ShowDialog =DialogResult.OK Then
Dim fl As String = dlg.FileName
Dim app As New MSWord.Application
Try
Dim wb = e.Form.Controls("WebBrowser1").BaseControl
wb.Url = new System.Uri(ProjectPath & "KindEditor\e.html", System.UriKind.Absolute)
Dim ke As new KindEditor '必须创建一个新实例
wb.ObjectForScripting =ke
Application.DoEvents
'1打开word并复制全文
Dim doc = app.Documents.Open(fl)
app.Selection.WholeStory()
app.Selection.Copy()
'2富文本框触发粘贴js
wb.Document.InvokeScript("eval", New String() {"editor.clickToolbar('paste');"})
'3关闭doc
doc.Close '必须关闭,否则word会一直在后台被占用
Catch ex As exception
MessageBox.Show(ex.message)
Finally
ClipBoard.Clear '清空剪切板
app.Quit
End Try
End If感觉有点bug,不推荐用,还是老老实实自己控制读写好
Dim wb = e.Form.Controls("webBrowser1").BaseControl
Dim ke = wb.ObjectForScripting
Dim r As Row = Tables("表A").Current
If r IsNot Nothing Then
'绑定某个字段,当富文本框变化时,自动变化
ke.FullHtmlBindingField = "表A.完整html内容"
ke.InnerHtmlBindingField = "表A.纯文本内容"
wb.refresh '刷新
End IfDim sr = New System.IO.StreamReader(ProjectPath & "我的记事本.txt") '直接从文件路径生成' Dim bigStr As String = sr.ReadToEnd() '读取所有内容 sr.Close '必须释放资源 Output.Show(bigStr)
不过一般也不建议写大量的内容,平时写100m的word文档,也基本只有上百万的标书才会有。
开发过程中,富文本框粘贴图片功能正常,但是发布后发现粘贴不了。那是因为WebBrowser内核版本过低,默认IE6导致的。
狐表程序安装的时候,改过注册表,把foxtable.exe进程的,都启动为IE11内核,但是我们编译后的程序,就不是叫foxtable.exe,于是就默认回IE6内核,导致粘贴功能失效了
所以可以在软件打开时,修改注册表提升好WebBrowser为IE11内核。修改一次后,以后都有效了
Try
Registry.SetValue("HKEY_CURRENT_USER\SOFTWARE\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BROWSER_EMULATION",System.Diagnostics.Process.GetCurrentProcess.ProcessName & ".exe",11001)
Catch ex As Exception
MessageBox.Show("狐表调用IE11内核失败,会导致富文本框无法粘贴图片" & vbcrlf & "Error:" & ex.Message )
End Try需要20210529后的普通开发版打开。没这个版本也问题不大,下载后把KindEditor文件夹拷贝出来,按照我上面的代码写就行了