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

前端之家收集整理的这篇文章主要介绍了vb.net 教程 12-6 webbrowser 文本编辑器 1前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
现在不少网页的blog都是利用了将网页设置为编辑模式实现。
本节探讨使用WebBrowser做一个本地文本编辑器。
使用到的知识包括:Vb.net、Javascript、Html

由于之前已经对WebBrowser控件有了很好的认识,实际本节更多的是调用Document.ExecCommand方法
设计界面:

使用到的常量:
    Private Const HTML_COMMAND_BOLD = "Bold"                       '加粗
    Private Const HTML_COMMAND_UNDERLINE = "Underline"             '下划线
    Private Const HTML_COMMAND_ITALIC = "Italic"                   '斜体
    Private Const HTML_COMMAND_SUBSCRIPT = "Subscript"             '下标
    Private Const HTML_COMMAND_SUPERSCRIPT = "Superscript"         '上标
    Private Const HTML_COMMAND_STRIKE_THROUGH = "StrikeThrough"    '删除线
    Private Const HTML_COMMAND_FONT_NAME = "FontName"              '字体
    Private Const HTML_COMMAND_FONT_SIZE = "FontSize"              '字号
    Private Const HTML_COMMAND_FORE_COLOR = "ForeColor"            '字体前景色
    Private Const HTML_COMMAND_BACK_COLOR = "BackColor"            '字体背景色
    Private Const HTML_COMMAND_INSERT_FORMAT_BLOCK = "FormatBlock" '加粗
    Private Const HTML_COMMAND_REMOVE_FORMAT = "RemoveFormat"      '清除样式
    Private Const HTML_COMMAND_JUSTIFY_LEFT = "JustifyLeft"        '文本左对齐
    Private Const HTML_COMMAND_JUSTIFY_CENTER = "JustifyCenter"    '文本中间对齐
    Private Const HTML_COMMAND_JUSTIFY_RIGHT = "JustifyRight"      '文本右对齐
    Private Const HTML_COMMAND_JUSTIFY_FULL = "JustifyFull"        '文本两端对齐
    Private Const HTML_COMMAND_INDENT = "Indent"                   '增大缩进量
    Private Const HTML_COMMAND_OUTDENT = "Outdent"                 '减小缩进量
    Private Const HTML_COMMAND_INSERT_LINE = "InsertHorizontalRule" '插入分割符
    Private Const HTML_COMMAND_INSERT_LIST = "Insert{0}List"         ' replace with (Un)Ordered 插入项目符号或项目编号
    Private Const HTML_COMMAND_INSERT_IMAGE = "InsertImage"         '插入图像
    Private Const HTML_COMMAND_INSERT_LINK = "CreateLink"           '插入链接
    Private Const HTML_COMMAND_REMOVE_LINK = "Unlink"               '移除链接
    Private Const HTML_COMMAND_TEXT_CUT = "Cut"                     '剪切
    Private Const HTML_COMMAND_TEXT_COPY = "Copy"                   '复制
    Private Const HTML_COMMAND_TEXT_PASTE = "Paste"                 '粘贴
    Private Const HTML_COMMAND_TEXT_DELETE = "Delete"               '删除
    Private Const HTML_COMMAND_TEXT_UNDO = "Undo"                   '撤销
    Private Const HTML_COMMAND_TEXT_REDO = "Redo"                   '恢复
    Private Const HTML_COMMAND_TEXT_SELECT_ALL = "SelectAll"        '全选
    Private Const HTML_COMMAND_TEXT_UNSELECT = "Unselect"           '取消选择
    Private Const HTML_COMMAND_TEXT_PRINT = "Print"                 ' 打印
    Private Const HTML_COMMAND_EDITMODE = "EditMode"                ' 编辑模式
    Private Const HTML_COMMAND_BROWSEMODE = "BrowseMode"            ' 浏览模式
    Private Const HTML_COMMAND_OVERWRITE = "OverWrite"             '转换插入、覆写模式

窗体载入时设置字体和字号
    Private Sub frmBlog_Load(sender As Object,e As EventArgs) Handles Me.Load
        Dim linkhtm As String = "about:blank" 'Application.StartupPath & "\view.htm"
        wbBlog.Url = New Uri(linkhtm)

        wbBlog.Document.ExecCommand("EditMode",False,"")          '设置为可编辑模式

        Call setFontName()
        Call setFontSize()
    End Sub

    ' 设置字体
    Private Sub setFontName()
        Dim computerFont As New System.Drawing.Text.InstalledFontCollection
        For Each font_family As FontFamily In computerFont.Families
            If font_family.IsStyleAvailable(FontStyle.Regular) Then
                tscFont.Items.Add(font_family.Name)
            End If
        Next
        tscFont.SelectedIndex = 0
    End Sub

    '设置字号
    Private Sub setFontSize()

        tscFontSize.Items.Add("一号字")
        tscFontSize.Items.Add("二号字")
        tscFontSize.Items.Add("三号字")
        tscFontSize.Items.Add("四号字")
        tscFontSize.Items.Add("五号字")
        tscFontSize.Items.Add("六号字")
        tscFontSize.Items.Add("七号字")
        tscFontSize.SelectedIndex = 0
    End Sub



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

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

猜你在找的VB相关文章