惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

GbyAI
GbyAI
Cloudbric
Cloudbric
aimingoo的专栏
aimingoo的专栏
D
Docker
量子位
阮一峰的网络日志
阮一峰的网络日志
The GitHub Blog
The GitHub Blog
小众软件
小众软件
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
博客园 - Franky
S
Schneier on Security
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
T
Tor Project blog
Google Online Security Blog
Google Online Security Blog
T
Tenable Blog
C
Cyber Attacks, Cyber Crime and Cyber Security
I
InfoQ
罗磊的独立博客
P
Palo Alto Networks Blog
Security Latest
Security Latest
K
Kaspersky official blog
Apple Machine Learning Research
Apple Machine Learning Research
I
Intezer
C
Cybersecurity and Infrastructure Security Agency CISA
TaoSecurity Blog
TaoSecurity Blog
S
Security @ Cisco Blogs
Google DeepMind News
Google DeepMind News
W
WeLiveSecurity
Simon Willison's Weblog
Simon Willison's Weblog
V
V2EX
N
Netflix TechBlog - Medium
爱范儿
爱范儿
S
Secure Thoughts
S
Securelist
MyScale Blog
MyScale Blog
Webroot Blog
Webroot Blog
IT之家
IT之家
Cyberwarzone
Cyberwarzone
Martin Fowler
Martin Fowler
Project Zero
Project Zero
NISL@THU
NISL@THU
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
H
Hacker News: Front Page
H
Heimdal Security Blog
博客园_首页
V
Vulnerabilities – Threatpost
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Recent Announcements
Recent Announcements
Blog — PlanetScale
Blog — PlanetScale
The Cloudflare Blog

博客园 - jerreychen

常用的JavaScript验证正则表达式 用CSS设置打印换页 使用Form认证实现用户登录 (LoginView的使用) 向容器(PlaceHolder)中动态添加多个用户控件(UserControl) C#.Net中WinForm采用Active Directory进行身份认证 Windows XP 如何设置系统自动关机任务 - jerreychen DataList嵌套GridView实现文章分类列表显示(2) SQLite 数据库初学 学习笔记 - jerreychen - 博客园 如何将.xsd文件自动生成对象 ASP.NET页面事件:顺序与回传详解 读取自定义的config文件 - jerreychen - 博客园 Umbraco 设置Document Types 的默认值 格式化数据 Net支持gzip 压缩格式 压缩与解压 .net 序列化数据对象 .net 下,日期的格式化 太久太久没来了,久违了-博客园 showModelDialog 关闭后改变GridView某个单元格的值
visual studio 2008 sp1中如何让WebBrowser控件可编辑 - jerreychen
jerreychen · 2008-11-12 · via 博客园 - jerreychen

我这里强调:是Visual studio 2008 sp1。因为sp1中微软加入了一个WebBrowser控件,这样我们就不需要再去引用WinForm的webBrowser控件了。

<WebBrowser Name="editorWebBrowser"></WebBrowser>

接着我们把 WebBrowser 的 designMode 设置为 "On"

((HTMLDocument)(editorWebBrowser.Document)).designMode = "On";

这样 WebBrowser 就是可编辑状态了

之后,我们来为 WebBrowser 添加几个样式 B(bold),I(italic),U(underline)
首先,定义两个变量 _document, _htmlBody

        private HTMLDocument _document;    //editorWebBrowser.Document as HTMLDocument
private HTMLBody _htmlBody; //(HTMLBody)(_document.body)

创建方法,获取到 WebBrowser 中选定的对象

        private IHTMLTxtRange GetTextRange()
{
IHTMLTxtRange range
= null;
try
{
range
= (IHTMLTxtRange)_document.selection.createRange();
}
catch (Exception)
{
range
= null;
}
return range;
}

好了,然后写一个方法,添加属性到选定的 TextRange

        private bool AppendTagToTextRange(string commandName, object value)
{
_htmlBody
= (HTMLBody)_document.body;

IHTMLTxtRange range

= GetTextRange();if (range == null || string.IsNullOrEmpty(range.text))
return false;return range.execCommand(commandName, false, value);

}


最后就是去实现 BI、和 U 三个样式了

AppendTagToTextRange("Bold", "b");
AppendTagToTextRange("Italic", "i");


AppendTagToTextRange("UnderLine", "u");

呵呵,好像和 Web Editor(网页编辑器)里面用 Javascript 的实现方式相同哦

注意:HTMLDocument,HTMLBody 都要引用控件 using mshtml; (Add References  Microsoft.mshtml)