使DHTMLEd控件的代码用带引号的引号替换直引号

时间:2008-09-14 19:51:49

标签: html dom vb6

我有一个旧的,传统的VB6应用程序,它使用DHTML编辑控件作为HTML编辑器。 Microsoft DHTML编辑控件,即.k.a。DHTMLEd,可能只不过是一个在内部使用IE自带的本机编辑功能的IE控件。

我想修改应用以实现像Word这样的智能引号。具体而言,替换为'替换为'或者'在键入时适当;如果用户在替换后立即按下Ctrl + Z,则会返回直线引号。

有没有人有代码可以做到这一点?

如果你没有DHTML / VB6的代码,但确实有一个可以在带有contentEditable区域的浏览器中运行的JavaScript代码,我也可以使用它

1 个答案:

答案 0 :(得分:15)

这是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

注意:此解决方案在撤消链中插入了一个额外的级别。例如,键入“This is a test”会给出一个“This is a test”链 - &gt; “这是一个测试” - &gt; “这是一个测试 - &gt;” - &gt;“(以粗体显示的额外级别)。要删除这个额外级别,您必须实现某种PostMessage+subclassing解决方案,该解决方案不涉及取消本机按键

编辑:如果您的目标是Windows Vista,请不要忘记添加DHTML Editing Control redistributable