我有一个旧的 VB6 应用程序,它使用 DHTML 编辑控件作为 HTML 编辑器。 Microsoft DHTML 编辑控件,又名 DHTMLEd,可能只不过是一个在内部使用 IE 自己的本机编辑功能的 IE 控件。
I'd like to modify the app to implement smart quotes like Word. Specifically, " is replaced with “ or ” and ' is replaced with ‘ or ’ as appropriate as it is typed; and if the user presses Ctrl+Z immediately after the replacement, it goes back to being a straight quote.
有人有这样做的代码吗?
如果您没有 DHTML/VB6 代码,但有可在具有 contentEditable 区域的浏览器中运行的 JavaScript 代码,我也可以使用它
这是 VB6 版本:
Private Sub DHTMLEdit1_onkeypress()
Dim e As Object
Set e = DHTMLEdit1.DOM.parentWindow.event
'Perform smart-quote replacement'
Select Case e.keyCode
Case 34: 'Double-Quote'
e.keyCode = 0
If IsAtWordEnd Then
InsertDoubleUndo ChrW$(8221), ChrW$(34)
Else
InsertDoubleUndo ChrW$(8220), ChrW$(34)
End If
Case 39: 'Single-Quote'
e.keyCode = 0
If IsAtWordEnd Then
InsertDoubleUndo ChrW$(8217), ChrW$(39)
Else
InsertDoubleUndo ChrW$(8216), ChrW$(39)
End If
End Select
End Sub
Private Function IsLetter(ByVal character As String) As Boolean
IsLetter = UCase$(character) <> LCase$(character)
End Function
Private Sub InsertDoubleUndo(VisibleText As String, HiddenText As String)
Dim selection As Object
Set selection = DHTMLEdit1.DOM.selection.createRange()
selection.Text = HiddenText
selection.moveStart "character", -Len(HiddenText)
selection.Text = VisibleText
End Sub
Private Function IsAtWordEnd() As Boolean
Dim ch As String
ch = PreviousChar
IsAtWordEnd = (ch <> " ") And (ch <> "")
End Function
Private Function PreviousChar() As String
Dim selection As Object
Set selection = m_dom.selection.createRange()
selection.moveStart "character", -1
PreviousChar = selection.Text
End Function
注意:此解决方案在撤消链中插入一个附加级别。例如,输入“这是一个测试”会给出一系列“这是一个测试”->“这是一个测试”->“这是一个测试->“->”(粗体的额外级别)。要删除这个额外级别,您必须实现某种PostMessage+subclassing
不涉及取消本机按键的解决方案
编辑:不要忘记包括DHTML 编辑控件可再发行如果您的目标是 Windows Vista。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)