C#Webbrowser编辑模式:按“enter”导致新段落

问题描述 投票:0回答:1

我有一个小的WPF应用程序,它将webbrowser-control与mshtml结合使用作为html编辑器。到目前为止效果非常好,但是:当我按下“Enter”键时,webbrowser会添加一个新段落

into the mshtml document. I need a simple line-break instead, because the line spacing is too big with Paragraphs. Does anybody have an idea how I can get that effect?
<WebBrowser Name="wbbHTMLeMail" PreviewKeyDown="WbbHTMLeMail_PreviewKeyDown"/>

wbbHTMLeMail.NavigateToString(CurrentEmail.Body);
doc = wbbHTMLeMail.Document as HTMLDocument;
doc.designMode = "ON";
wbbHTMLeMail.PreviewKeyDown += WbbHTMLeMail_PreviewKeyDown;

我试图捕获PreviewKeyDown事件,但我不知道如何将换行注入到mshtml文档中。一尝试就是这样做:

 private void WbbHTMLeMail_PreviewKeyDown(object sender, System.Windows.Input.KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                var sText = doc.selection.createRange();
                sText.execCommand("Paste", false, "</br>");
                e.Handled = true;
            }
}

但这不起作用,它只将“/ br>”作为纯文本插入。有没有人知道在按Enter键时如何只获得一个简单的换行符?

c# mshtml
1个回答
0
投票

我打算推荐这个:

private void WbbHTMLeMail_PreviewKeyDown(object sender, System.Windows.Input.KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                var sText = doc.selection.createRange();
                sText.execCommand("Paste", false, Environment.NewLine);
                e.Handled = true;
            }
}
© www.soinside.com 2019 - 2024. All rights reserved.