c# - 在 C# Windows 窗体中的 Web 浏览器控件中将粗体字大写为大写
问题描述
我在我的 C# windows 窗体中使用 Web 浏览器控件,并且在键入时需要默认将所有粗体字大写。我查了一下,发现 execcommand 支持 stylewithcss ( https://developer.mozilla.org/en-US/docs/Web/API/Document/execCommand )
我写的粗体按钮功能是:
private void boldButton_Click(object sender, EventArgs e)
{
webBrowser1.Document.ExecCommand("Bold", false, new { @class = "<span style= style=text-transform:uppercase;>" });
}
但它只会使文本变为粗体而不是大写,对此问题的任何帮助将不胜感激。
解决方案
WebBrowser
当您执行以下命令时,控件使用strong
标记使文本变为粗体:
webBrowser1.Document.ExecCommand("Bold", false, null);
要使强文本大写,您需要应用text-transform:uppercase
到strong
标签的样式:
dynamic document = webBrowser1.Document.DomDocument;
var range = document.selection.createRange();
range.execCommand("Bold", false, null);
range.parentElement.style.textTransform = "uppercase";
为了使它更好一点,如果您可以控制 initial html
,只需在标签上添加一个全局样式strong
,例如:
webBrowser1.DocumentText = @"
<html>
<head><style>strong {text-transform: uppercase;}</style></head>
<body>
<div contenteditable=""true"">Some <strong>contents</strong>.</div>
</body>
</html>
";
然后当使用ctrl+B或使用使文本变得强时execCommand
,您不需要使用代码使文本大写,它会自动大写。