附件: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的更多相关文章

  1. DBGridEh 在粘贴中文时出现乱码和错位 100zhx_888]

    http://www.fx114.net/qa-29-3439.aspx 回复于: -- :: unit DBGridEh; 把下面这个函数替换成这样 procedure TDBGridInplace ...

  2. VBA精彩代码分享-1

    今天下班前分享一下之前在网上搜到的两段好用的VBA代码,貌似都来自国外,觉得挺好,模仿不来. 第一段的功能是修改VBA控件中的文本框控件,使其右键可以选择粘贴.复制.剪切等: Option Expli ...

  3. Delphi 字符串函数 StrUtils(大全)

    引用单元: StrUtils; 首部 function AnsiResemblesText(const AText, AOther: string): Boolean; $[StrUtils.pas ...

  4. c#如何判断webbrowser已经加载完毕

    最近有个小程序需要采集网页源代码,但有的网页中JS脚本又会生成额外的代码,比如http://www.cnblogs.com/lidabo/p/4169396.html 红框部分便是另外加载的代码. 此 ...

  5. 【手记】WebBrowser响应页面中的blank开新窗口及window.close关闭本窗体

    注:本文适用.net 2.0+的winform项目 目的: 点击页面中的target="_blank"链接时,弹出新窗体 页面中有window.close()操作时,关闭窗体 上述 ...

  6. WPF 开发 WebBrowser

    WebBrowser WebBrowser 报错如何屏蔽 CEF(Chromium Embedded Framework)       参考 WPF, Chrome Embedded and WebA ...

  7. C# Webbrowser 常用方法及多线程调用

    设置控件的值 /// <summary> /// 根据ID,NAME双重判断并设置值 /// </summary> /// <param name="tagNa ...

  8. C#中的WebBrowser控件的使用

    0.常用方法   Navigate(string urlString):浏览urlString表示的网址 Navigate(System.Uri url):浏览url表示的网址 Navigate(st ...

  9. 【总结】使用WebBrowser遇到的陷阱

    一.前言 一直想用WebBrowser做一些好玩的东西,比如抓取分析感兴趣的网站页面.自动点击提交页面等,所以最近在研究WebBrowser.WebBrowser的功能十分强大,就是一个微型的Brow ...

随机推荐

  1. C# net部署图片分布式存储服务器的小案例

    如果web服务用户多了,访问多了,用户上传的图片,文件等内容放在一块,想必服务器是承受不住的,这个时候,我们就需要考虑分布式存储的方法了. 如图所示:一个web服务器拖2个图片服务器 如何做到用户上传 ...

  2. 图片Base64编码 简单使用

    图片在线转换Base64,图片编码base64 http://tool.css-js.com/base64.html HTML5 + js <input type="file" ...

  3. android SurfaceView绘制 重新学习--基础绘制

    自从大二写了个android游戏去参加比赛,之后就一直写应用,一直没用过SurfaceView了,现在进入了游戏公司,准备从基础开始重新快速的学一下这个,然后再去研究openGL和游戏引擎. 直接上代 ...

  4. ios解决输入框弹出后position:fixed失效问题

    最近在使用AmazeUI进行仿App Mobile Web开发时遇到了讨论众多的position:fixed问题.position:fixed在安卓2.2以上已经实现,但是在ios8以下系统当小键盘激 ...

  5. SQL server 开启 cmdshell

    GO RECONFIGURE GO GO RECONFIGURE GO EXEC master..xp_cmdshell 'net use Z: \\192.168.11.1\192.168.11.4 ...

  6. QLGame 2d Engine Android端环境搭建(通过jni读取assets目录的文件)

    QLGame 2d Engine win端已经实现了一个动物的动画了,初步的东西已经完成,考虑在Android端也实现这些基本的东西,这样跨平台的引擎也就实现了! 要在Android下编程,首先要实现 ...

  7. OneAPM Cloud Test——系统性能监控神器

    2015 年 8 月,OneAPM 推出了一款系统性能监控产品--Cloud Test,产品上线以来以「两低一高」的特点迅速成为市场增长率最快的一匹黑马.「两低一高」,即低使用成本.低学习成本以及高服 ...

  8. Win7资源管理器已停止工作——StackHash_6c37,R6205错误

      2013-9-20 此问题由来已久,截图及"问题签名"如下: 问题签名: 问题事件名称: BEX64 应用程序名: Explorer.EXE 应用程序版本: 6.1.7601. ...

  9. 17.1.4 Replication and Binary Logging Options and Variables 复制和Binary logging 选项和变量

    17.1.4 Replication and Binary Logging Options and Variables 复制和Binary logging 选项和变量 下面的章节包含信息关于mysql ...

  10. LM393,LM741可以用作电压跟随器吗?

    应该不能,比较器一般为OC门,输出要上拉VCC,在跟随状态下为深度负反馈,恐怕不能正常工作,会振荡的,不过你可以试下嘛.