软件开发
开发分享
软件下载

C# WebBrowser实现HTML编辑器控件源码

时间:2010-07-14 来源:juhe99 点击量:

C# WebBrowser实现HTML编辑器控件源码

一直想弄个好用的htmlEditor,苦于水平不行,迟迟写不出,导致博客群发、邮件群发等软件开发无限延期,为了解决这个问题。查了不少资料,测试了无数次,终于把它搞定了。效果跟.NET Web开发环境的html编辑器很相似,有html和文本视图,下面是该控件的源码,希望对大家有用。

核心实现代码

1. 工具栏状态刷新

////// 刷新按钮状态
///private void RefreshToolBar()
{
    BeginUpdate();

    try
    {
        mshtml.IHTMLDocument2 document = (mshtml.IHTMLDocument2)webBrowserBody.Document.DomDocument;

        toolStripComboBoxName.Text = document.queryCommandValue("FontName").ToString();
        toolStripComboBoxSize.SelectedItem = FontSize.Find((int)document.queryCommandValue("FontSize"));
        toolStripButtonBold.Checked = document.queryCommandState("Bold");
        toolStripButtonItalic.Checked = document.queryCommandState("Italic");
        toolStripButtonUnderline.Checked = document.queryCommandState("Underline");

        toolStripButtonNumbers.Checked = document.queryCommandState("InsertOrderedList");
        toolStripButtonBullets.Checked = document.queryCommandState("InsertUnorderedList");

        toolStripButtonLeft.Checked = document.queryCommandState("JustifyLeft");
        toolStripButtonCenter.Checked = document.queryCommandState("JustifyCenter");
        toolStripButtonRight.Checked = document.queryCommandState("JustifyRight");
        toolStripButtonFull.Checked = document.queryCommandState("JustifyFull");
    }
    catch (Exception e)
    {
        System.Diagnostics.Debug.WriteLine(e);
    }
    finally
    {
        EndUpdate();
    }
}

2. 更新状态管理

#region 更新相关

private int dataUpdate;
private bool Updating
{
    get
    {
        return dataUpdate != 0;
    }
}

private void BeginUpdate()
{
    ++dataUpdate;
}
private void EndUpdate()
{
    --dataUpdate;
}

#endregion

3. 字体相关操作

private void toolStripComboBoxName_SelectedIndexChanged(object sender, EventArgs e)
{
    if (Updating)
    {
        return;
    }

    webBrowserBody.Document.ExecCommand("FontName", false, toolStripComboBoxName.Text);
}

private void toolStripComboBoxSize_SelectedIndexChanged(object sender, EventArgs e)
{
    if (Updating)
    {
        return;
    }

    int size = (toolStripComboBoxSize.SelectedItem == null) ? 1 : (toolStripComboBoxSize.SelectedItem as FontSize).Value;
    webBrowserBody.Document.ExecCommand("FontSize", false, size);
}

private void toolStripButtonBold_Click(object sender, EventArgs e)
{
    if (Updating)
    {
        return;
    }

    webBrowserBody.Document.ExecCommand("Bold", false, null);
    RefreshToolBar();
}

private void toolStripButtonItalic_Click(object sender, EventArgs e)
{
    if (Updating)
    {
        return;
    }

    webBrowserBody.Document.ExecCommand("Italic", false, null);
    RefreshToolBar();
}

private void toolStripButtonUnderline_Click(object sender, EventArgs e)
{
    if (Updating)
    {
        return;
    }

    webBrowserBody.Document.ExecCommand("Underline", false, null);
    RefreshToolBar();
}

4. 颜色选择操作

private void toolStripButtonColor_Click(object sender, EventArgs e)
{
    if (Updating)
    {
        return;
    }

    int fontcolor = (int)((mshtml.IHTMLDocument2)webBrowserBody.Document.DomDocument).queryCommandValue("ForeColor");

    ColorDialog dialog = new ColorDialog();
    dialog.Color = Color.FromArgb(0xff, fontcolor & 0xff, (fontcolor >> 8) & 0xff, (fontcolor >> 16) & 0xff);

    DialogResult result = dialog.ShowDialog();
    if (result == DialogResult.OK)
    {
        string color = dialog.Color.Name;
        if (dialog.Color.IsNamedColor)
        {
            color = "#" + color.Remove(0, 2);
        }

        webBrowserBody.Document.ExecCommand("ForeColor", false, color);
    }
    RefreshToolBar();
}

5. 列表和对齐操作

private void toolStripButtonNumbers_Click(object sender, EventArgs e)
{
    if (Updating)
    {
        return;
    }

    webBrowserBody.Document.ExecCommand("InsertOrderedList", false, null);
    RefreshToolBar();
}

private void toolStripButtonBullets_Click(object sender, EventArgs e)
{
    if (Updating)
    {
        return;
    }

    webBrowserBody.Document.ExecCommand("InsertUnorderedList", false, null);
    RefreshToolBar();
}

private void toolStripButtonLeft_Click(object sender, EventArgs e)
{
    if (Updating)
    {
        return;
    }

    webBrowserBody.Document.ExecCommand("JustifyLeft", false, null);
    RefreshToolBar();
}

private void toolStripButtonCenter_Click(object sender, EventArgs e)
{
    if (Updating)
    {
        return;
    }

    webBrowserBody.Document.ExecCommand("JustifyCenter", false, null);
    RefreshToolBar();
}

6. 视图切换实现

//切换视图
private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
{
    iLastFocus = tabControl1.SelectedIndex;
    if (iLastFocus == 0)
    {
        webBrowserBody.Document.Body.InnerHtml = txtRichbox.Text.Trim();
    }
    else
    {
        txtRichbox.Text = webBrowserBody.Document.Body.InnerHtml;
        //webBrowserBody.DocumentText;
    }
}

7. 字体大小转换类

#region 字体大小转换

private class FontSize
{
    private static ListallFontSize = null;
    public static ListAll
    {
        get
        {
            if (allFontSize == null)
            {
                allFontSize = new List();
                allFontSize.Add(new FontSize(8, 1));
                allFontSize.Add(new FontSize(10, 2));
                allFontSize.Add(new FontSize(12, 3));
                allFontSize.Add(new FontSize(14, 4));
                allFontSize.Add(new FontSize(18, 5));
                allFontSize.Add(new FontSize(24, 6));
                allFontSize.Add(new FontSize(36, 7));
            }

            return allFontSize;
        }
    }

    public static FontSize Find(int value)
    {
        if (value < 1)
        {
            return All[0];
        }

        if (value > 7)
        {
            return All[6];
        }

        return All[value - 1];
    }

    private FontSize(int display, int value)
    {
        displaySize = display;
        valueSize = value;
    }

    private int valueSize;
    public int Value
    {
        get
        {
            return valueSize;
        }
    }

    private int displaySize;
    public int Display
    {
        get
        {
            return displaySize;
        }
    }

    public override string ToString()
    {
        return displaySize.ToString();
    }
}

#endregion

技术特点:

  • 双视图模式:支持HTML视图和文本视图切换

  • 完整工具栏:包含字体、字号、加粗、斜体、下划线、颜色、对齐等常用功能

  • 状态同步:实时刷新工具栏按钮状态

  • 更新控制:防止操作冲突的BeginUpdate/EndUpdate机制

  • 兼容性好:基于WebBrowser控件,界面效果类似Web开发环境