gpt4 book ai didi

c# - CKEditor 可以在 WinForms 应用程序中用于 (X)HTML 编辑吗?

转载 作者:太空狗 更新时间:2023-10-30 01:25:56 25 4
gpt4 key购买 nike

我已经修改了 winforms html editor 中的代码到 C#(见下文)。是否可以改用 CKEditor?

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WebEditorTest
{
/// <summary>
/// https://stackoverflow.com/questions/214124/winforms-html-editor
/// </summary>
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
webBrowser1.Navigate("about:blank");
Application.DoEvents();
webBrowser1.Document.OpenNew(false).Write("<html><body><div id=\"editable\">Edit this text</div></body></html>");
foreach (HtmlElement el in webBrowser1.Document.All)
{
el.SetAttribute("unselectable", "on");
el.SetAttribute("contenteditable", "false");
}
foreach (HtmlElement el in webBrowser1.Document.All.GetElementsByName("editable"))
{
el.SetAttribute("width", webBrowser1.Width + "px");
el.SetAttribute("height", "100%");
el.SetAttribute("contenteditable", "true");
}
webBrowser1.Document.DomDocument.GetType().GetProperty("designMode").SetValue(webBrowser1.Document.DomDocument, "on", null);
webBrowser1.IsWebBrowserContextMenuEnabled = false;
}

private void button1_Click(object sender, EventArgs e)
{
textBoxMarkup.Text = webBrowser1.DocumentText;
}
}
}

最佳答案

是的。由于您已经在使用 WebBrowser 控件,因此没有什么可以阻止您加载包含 CKEditor 的 HTML 页面并通过 DOM 和 InvokeScript 与其交互。

更新 - 这是一个交互的工作示例:

表单.cs

public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
webBrowser1.Navigate("C:\\blank.htm");
Application.DoEvents();
}

private void button1_Click(object sender, EventArgs e)
{
webBrowser1.Document.InvokeScript("InitEditor");
}
}

空白.htm

<html>
<head>
<script src='http://ckeditor.com/apps/ckeditor/4.2/ckeditor.js?mriyyd'></script>
<script type='text/javascript'>
function InitEditor() { CKEDITOR.replace('editor1'); }
</script>
</head>
<body>
<textarea cols='80' id='editor1' name='editor1' rows='10'>
<span>Lorem Ipsum</span>
</textarea>
</body>
</html>

关于c# - CKEditor 可以在 WinForms 应用程序中用于 (X)HTML 编辑吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5987432/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com