vb.net 教程 12-6 webbrowser 文本编辑器 2

前端之家收集整理的这篇文章主要介绍了vb.net 教程 12-6 webbrowser 文本编辑器 2前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

编辑使用到的代码

    '拷贝
    Private Sub tsbCopy_Click(sender As Object,e As EventArgs) Handles tsbCopy.Click
        wbBlog.Document.ExecCommand(HTML_COMMAND_TEXT_COPY,False,"")
    End Sub
    '粘贴
    Private Sub tsbPaste_Click(sender As Object,e As EventArgs) Handles tsbPaste.Click
        ' wbBlog.Document.ExecCommand(HTML_COMMAND_TEXT_PASTE,"")
        wbBlog.Focus()
        SendKeys.Send("^v")
    End Sub
注意:粘贴命令Paste似乎没有作用,这里用了直接发送键盘命令粘贴的方法方法
    '粗体
    Private Sub tsbBold_Click(sender As Object,e As EventArgs) Handles tsbBold.Click
        wbBlog.Document.ExecCommand(HTML_COMMAND_BOLD,"")
    End Sub

    '斜体
    Private Sub tsbItalic_Click(sender As Object,e As EventArgs) Handles tsbItalic.Click
        wbBlog.Document.ExecCommand(HTML_COMMAND_ITALIC,"")
    End Sub

    '下划线
    Private Sub tsbUnderline_Click(sender As Object,e As EventArgs) Handles tsbUnderline.Click
        wbBlog.Document.ExecCommand(HTML_COMMAND_UNDERLINE,"")
    End Sub

    '左对齐
    Private Sub tsbLeft_Click(sender As Object,e As EventArgs) Handles tsbLeft.Click
        wbBlog.Document.ExecCommand(HTML_COMMAND_JUSTIFY_LEFT,"")
    End Sub

    '中间对齐
    Private Sub tsbCenter_Click(sender As Object,e As EventArgs) Handles tsbCenter.Click
        wbBlog.Document.ExecCommand(HTML_COMMAND_JUSTIFY_CENTER,"")
    End Sub

    '右对齐
    Private Sub tsbRight_Click(sender As Object,e As EventArgs) Handles tsbRight.Click
        wbBlog.Document.ExecCommand(HTML_COMMAND_JUSTIFY_RIGHT,"")
    End Sub

    '两端对齐
    Private Sub tsbJustifyFull_Click(sender As Object,e As EventArgs) Handles tsbJustifyFull.Click
        wbBlog.Document.ExecCommand(HTML_COMMAND_JUSTIFY_FULL,"")
    End Sub

    '剪切
    Private Sub tsbCut_Click(sender As Object,e As EventArgs) Handles tsbCut.Click
        wbBlog.Document.ExecCommand(HTML_COMMAND_TEXT_CUT,"")
    End Sub

    '撤销
    Private Sub tsbUndo_Click(sender As Object,e As EventArgs) Handles tsbUndo.Click
        wbBlog.Document.ExecCommand(HTML_COMMAND_TEXT_UNDO,"")
    End Sub

    '重做
    Private Sub tsbRedo_Click(sender As Object,e As EventArgs) Handles tsbRedo.Click
        wbBlog.Document.ExecCommand(HTML_COMMAND_TEXT_REDO,"")
    End Sub

    '清除样式
    Private Sub tsbClear_Click(sender As Object,e As EventArgs) Handles tsbClear.Click
        wbBlog.Document.ExecCommand(HTML_COMMAND_REMOVE_FORMAT,"")
    End Sub

    '修改字体
    Private Sub tscFont_SelectedIndexChanged(sender As Object,e As EventArgs) Handles tscFont.SelectedIndexChanged
        wbBlog.Document.ExecCommand(HTML_COMMAND_FONT_NAME,tscFont.Text)
    End Sub

    '修改字号
    Private Sub tscFontSize_SelectedIndexChanged(sender As Object,e As EventArgs) Handles tscFontSize.SelectedIndexChanged
        wbBlog.Document.ExecCommand(HTML_COMMAND_FONT_SIZE,tscFontSize.SelectedIndex + 1)
    End Sub

    '字体颜色
    Private Sub tsbFontColor_Click(sender As Object,e As EventArgs) Handles tsbFontColor.Click
        Dim fontcolor As Color
        If cdColor.ShowDialog() = DialogResult.OK Then
            fontcolor = cdColor.Color
        Else
            Exit Sub
        End If
        wbBlog.Document.ExecCommand(HTML_COMMAND_FORE_COLOR,System.Drawing.ColorTranslator.ToHtml(fontcolor))
    End Sub

    '背景颜色
    Private Sub tsbBackColor_Click(sender As Object,e As EventArgs) Handles tsbBackColor.Click
        Dim bgcolor As Color
        If cdColor.ShowDialog() = DialogResult.OK Then
            bgcolor = cdColor.Color
        Else
            Exit Sub
        End If
        wbBlog.Document.BackColor = bgcolor
    End Sub

注意:ExecCommand命令BackColor修改的是文字背后的颜色,而不是我们希望的整个网页的背景色,这里直接修改WebBrowser.Document.BackColor


    '插入图片
    Private Sub tsbImage_Click(sender As Object,e As EventArgs) Handles tsbImage.Click
        Dim imgpath As String
        If ofDialog.ShowDialog = DialogResult.OK Then
            imgpath = ofDialog.FileName
        Else
            Exit Sub
        End If
        wbBlog.Document.ExecCommand(HTML_COMMAND_INSERT_IMAGE,imgpath)
    End Sub


运行效果

由于.net平台下C#和vb.NET很相似,本文也可以为C#爱好者提供参考。

学习更多vb.net知识,请参看 vb.net 教程 目录

原文链接:https://www.f2er.com/vb/256419.html

猜你在找的VB相关文章