webbrowser selstart selLength
附件:http://files.cnblogs.com/xe2011/Webbrowser_SelStart.rar
1 获得webBrowser光标所在的位置
2 设置webBrowser光标的位置
3 获得webBrowser选中的文本长度
4 选中webBrowser指定的字符串

项目添加引用Microsoft.mshtml

单元引用
using mshtml;
获得webBrowser光标所在的位置
当有文本被选中时返回 的位置应为当 getSelectionStart() - getSelectionLength()/*
function getSelectionStart(){
var range=document.selection.createRange();
range.moveStart('character', -document.body.innerText.length);
return range.text.length;
}
*/
private int getSelectionStart(WebBrowser webBrowser)
{
try
{
IHTMLDocument2 document = (IHTMLDocument2)webBrowser.Document.DomDocument;
IHTMLTxtRange range = (IHTMLTxtRange)document.selection.createRange();
range.moveStart("character", -document.body.innerText.Length);
return range.text.Length;
}
catch (System.Exception ex)
{
//MessageBox.Show(ex.Message);
//当光标在0这个位置使用功能引起错误
return ;
}
}
设置webBrowser光标的位置
请看这个 选中指定的字符串 设置length=0,设置start的值就是光标的所在的位置
webBrowser1.Document.Focus();
setSelection(webBrowser1, , );
获得webBrowser选中的文本长度
/*
function getSelectionLength()
{
return document.selection.createRange().text.length;
}
*/
private int getSelectionLength(WebBrowser webBrowser)
{
try
{
IHTMLDocument2 document = (IHTMLDocument2)webBrowser.Document.DomDocument;
IHTMLTxtRange range = (IHTMLTxtRange)document.selection.createRange();
return range.text.Length;
}
catch (System.Exception ex)
{
return ;
//当光标在0这个位置使用功能引起错误
}
}
选中webBrowser指定的字符串
/*
function setSelection(start,length){
var range=document.selection.createRange();
range.collapse(true);
range.moveStart('character', start);
range.moveEnd('character', length);
range.select();
}
*/
public void setSelection(WebBrowser webBrowser, int start, int length)
{
IHTMLDocument2 document = (IHTMLDocument2)webBrowser.Document.DomDocument;
IHTMLTxtRange range = (IHTMLTxtRange)document.selection.createRange();
range.collapse(true);
range.move("textedit", -); //光标移动到第0位处
range.moveStart("character", start);
range.moveEnd("character", length);
range.select();
}
完整代码
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;
using mshtml; namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent(); } private void Form1_Load(object sender, EventArgs e)
{
webBrowser1.DocumentText = @"2013-12-07 19:14:20";
webBrowser1.Document.ExecCommand("EditMode", false, null);
} /*
function getSelectionStart(){
var range=document.selection.createRange();
range.moveStart('character', -document.body.innerText.length);
return range.text.length;
}
*/
private int getSelectionStart(WebBrowser webBrowser)
{
try
{
IHTMLDocument2 document = (IHTMLDocument2)webBrowser.Document.DomDocument;
IHTMLTxtRange range = (IHTMLTxtRange)document.selection.createRange();
range.moveStart("character", -document.body.innerText.Length);
return range.text.Length;
}
catch (System.Exception ex)
{
//MessageBox.Show(ex.Message);
//当光标在0这个位置使用功能引起错误
return ;
}
} /*
function getSelectionLength()
{
return document.selection.createRange().text.length;
}
*/
private int getSelectionLength(WebBrowser webBrowser)
{
try
{
IHTMLDocument2 document = (IHTMLDocument2)webBrowser.Document.DomDocument;
IHTMLTxtRange range = (IHTMLTxtRange)document.selection.createRange();
return range.text.Length;
}
catch (System.Exception ex)
{
return ;
//当光标在0这个位置使用功能引起错误
}
} /*
function setSelection(start,length){
var range=document.selection.createRange();
range.collapse(true);
range.moveStart('character', start);
range.moveEnd('character', length);
range.select();
}
*/
public void setSelection(WebBrowser webBrowser, int start, int length)
{
IHTMLDocument2 document = (IHTMLDocument2)webBrowser.Document.DomDocument;
IHTMLTxtRange range = (IHTMLTxtRange)document.selection.createRange();
range.collapse(true);
range.move("textedit", -); //光标移动到第0位处
range.moveStart("character", start);
range.moveEnd("character", length);
range.select();
} //选中指定的字符串
private void button1_Click(object sender, EventArgs e)
{
webBrowser1.Document.Focus();
setSelection(webBrowser1, , );
} //光标所在的位置
private void button2_Click(object sender, EventArgs e)
{
webBrowser1.Document.Focus();
int i = (int)getSelectionStart(webBrowser1);
Text = i.ToString();
} //选中的字符串长度
private void button3_Click(object sender, EventArgs e)
{
webBrowser1.Document.Focus();
Text = getSelectionLength(webBrowser1).ToString();
} //设置光标的位置
private void button4_Click(object sender, EventArgs e)
{
webBrowser1.Document.Focus();
setSelection(webBrowser1, , );
}
}
}
这是翻译JAVA SCRIPT的代码
"<script>
function getCommandValue(commandId){
return document.queryCommandValue(commandId);
}
function getSelectionStart(){
var range=document.selection.createRange();
range.moveStart('character', -document.body.innerText.length);
return range.text.length;
}
function getSelectionLength(){
return document.selection.createRange().text.length;
}
function setSelection(start,length){
var range=document.selection.createRange();
range.collapse(true);
range.moveStart('character', start);
range.moveEnd('character', length);
range.select();
}
</script>"
webbrowser selstart selLength的更多相关文章
- DBGridEh 在粘贴中文时出现乱码和错位 100zhx_888]
http://www.fx114.net/qa-29-3439.aspx 回复于: -- :: unit DBGridEh; 把下面这个函数替换成这样 procedure TDBGridInplace ...
- VBA精彩代码分享-1
今天下班前分享一下之前在网上搜到的两段好用的VBA代码,貌似都来自国外,觉得挺好,模仿不来. 第一段的功能是修改VBA控件中的文本框控件,使其右键可以选择粘贴.复制.剪切等: Option Expli ...
- Delphi 字符串函数 StrUtils(大全)
引用单元: StrUtils; 首部 function AnsiResemblesText(const AText, AOther: string): Boolean; $[StrUtils.pas ...
- c#如何判断webbrowser已经加载完毕
最近有个小程序需要采集网页源代码,但有的网页中JS脚本又会生成额外的代码,比如http://www.cnblogs.com/lidabo/p/4169396.html 红框部分便是另外加载的代码. 此 ...
- 【手记】WebBrowser响应页面中的blank开新窗口及window.close关闭本窗体
注:本文适用.net 2.0+的winform项目 目的: 点击页面中的target="_blank"链接时,弹出新窗体 页面中有window.close()操作时,关闭窗体 上述 ...
- WPF 开发 WebBrowser
WebBrowser WebBrowser 报错如何屏蔽 CEF(Chromium Embedded Framework) 参考 WPF, Chrome Embedded and WebA ...
- C# Webbrowser 常用方法及多线程调用
设置控件的值 /// <summary> /// 根据ID,NAME双重判断并设置值 /// </summary> /// <param name="tagNa ...
- C#中的WebBrowser控件的使用
0.常用方法 Navigate(string urlString):浏览urlString表示的网址 Navigate(System.Uri url):浏览url表示的网址 Navigate(st ...
- 【总结】使用WebBrowser遇到的陷阱
一.前言 一直想用WebBrowser做一些好玩的东西,比如抓取分析感兴趣的网站页面.自动点击提交页面等,所以最近在研究WebBrowser.WebBrowser的功能十分强大,就是一个微型的Brow ...
随机推荐
- UIWebView1-b
随着H5的强大,hybrid app已经成为当前互联网的大方向,单纯的native app和web app在某些方面显得就很劣势.关于H5的发展史,这里有一篇文章推荐给大家,今天我们来学习最基础的基于 ...
- 得到bundle seed id
- (NSString *)bundleSeedID { NSDictionary *query = [NSDictionary dictionaryWithObjectsAndKeys: (__br ...
- go与rpc
Go语言的RPC介绍(含Protobuf-RPC) http://www.open-open.com/lib/view/open1389251727289.html
- Android 图片比较
Android中图片比较大致的流程如下: 将Drawable对象转化成Bitmap对象.(实际比较的是Bitmap对象,假设为B1和B2) 将B1和B2中的像素取出,分别放在2个数组中.(实际存储的是 ...
- linux操作Oracle导入导出dmp数据命令
--清空该表数据 :非索引清空TRUNCATE TABLE GE_INTERFACE_MESSAGE; --数据库导出 指定导出某个用户的所有数据 包括表 索引 序列 存储过程 函数 等exp CX ...
- 转:十八、java中this的用法
http://blog.csdn.net/liujun13579/article/details/7732443 我知道很多朋友都和我一样:在JAVA程序中似乎经常见到“this”,自己也偶尔用到它, ...
- WordPress get_allowed_mime_types函数(wp-includes/functions.php)存在跨站脚本漏洞
漏洞版本: WordPress 3.6 漏洞描述: CVE ID:CVE-2013-5738 WordPress是一种使用PHP语言开发的博客平台,用户可以在支持PHP和MySQL数据库的服务器上架设 ...
- BZOJ2464: 中山市选[2009]小明的游戏
2464: 中山市选[2009]小明的游戏 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 280 Solved: 124[Submit][Statu ...
- 【canvas】伸缩 / 剪裁 / 文本 / 阴影 / 填充图案 / 填充渐变
<!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="utf-8& ...
- ☀【单位】REM
CSS3的REM设置字体大小 支持的浏览器还是蛮多的,比如:Mozilla Firefox 3.6+.Apple Safari 5+.Google Chrome.IE9+和Opera11+.只是可怜的 ...