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

563 篇文章 114 订阅

版权声明:本文为博主原创文章,转载请在显著位置标明本文出处以及作者网名,未经作者允许不得用于商业目的。

编辑使用到的代码:

    '拷贝

    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, False, "")

        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, False, "")

    End Sub

    '斜体

    Private Sub tsbItalic_Click(sender As Object, e As EventArgs) Handles tsbItalic.Click

        wbBlog.Document.ExecCommand(HTML_COMMAND_ITALIC, False, "")

    End Sub

    '下划线

    Private Sub tsbUnderline_Click(sender As Object, e As EventArgs) Handles tsbUnderline.Click

        wbBlog.Document.ExecCommand(HTML_COMMAND_UNDERLINE, False, "")

    End Sub

    '左对齐

    Private Sub tsbLeft_Click(sender As Object, e As EventArgs) Handles tsbLeft.Click

        wbBlog.Document.ExecCommand(HTML_COMMAND_JUSTIFY_LEFT, False, "")

    End Sub

    '中间对齐

    Private Sub tsbCenter_Click(sender As Object, e As EventArgs) Handles tsbCenter.Click

        wbBlog.Document.ExecCommand(HTML_COMMAND_JUSTIFY_CENTER, False, "")

    End Sub

    '右对齐

    Private Sub tsbRight_Click(sender As Object, e As EventArgs) Handles tsbRight.Click

        wbBlog.Document.ExecCommand(HTML_COMMAND_JUSTIFY_RIGHT, False, "")

    End Sub

    '两端对齐

    Private Sub tsbJustifyFull_Click(sender As Object, e As EventArgs) Handles tsbJustifyFull.Click

        wbBlog.Document.ExecCommand(HTML_COMMAND_JUSTIFY_FULL, False, "")

    End Sub

    '剪切

    Private Sub tsbCut_Click(sender As Object, e As EventArgs) Handles tsbCut.Click

        wbBlog.Document.ExecCommand(HTML_COMMAND_TEXT_CUT, False, "")

    End Sub

    '撤销

    Private Sub tsbUndo_Click(sender As Object, e As EventArgs) Handles tsbUndo.Click

        wbBlog.Document.ExecCommand(HTML_COMMAND_TEXT_UNDO, False, "")

    End Sub

    '重做

    Private Sub tsbRedo_Click(sender As Object, e As EventArgs) Handles tsbRedo.Click

        wbBlog.Document.ExecCommand(HTML_COMMAND_TEXT_REDO, False, "")

    End Sub

    '清除样式

    Private Sub tsbClear_Click(sender As Object, e As EventArgs) Handles tsbClear.Click

        wbBlog.Document.ExecCommand(HTML_COMMAND_REMOVE_FORMAT, False, "")

    End Sub

    '修改字体

    Private Sub tscFont_SelectedIndexChanged(sender As Object, e As EventArgs) Handles tscFont.SelectedIndexChanged

        wbBlog.Document.ExecCommand(HTML_COMMAND_FONT_NAME, False, tscFont.Text)

    End Sub

    '修改字号

    Private Sub tscFontSize_SelectedIndexChanged(sender As Object, e As EventArgs) Handles tscFontSize.SelectedIndexChanged

        wbBlog.Document.ExecCommand(HTML_COMMAND_FONT_SIZE, False, 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, False, 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, False, imgpath)

    End Sub

运行效果:

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

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

版权声明:本文为CSDN博主「VB.Net」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值