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 ...
随机推荐
- C++ 11 笔记 (二) : for循环
首先肯定的是,我不是标题党.. C++11的for循环确实有跟C++98不一样的地方,还是先上代码: , , , , }; for (int x : test_arr) { std::cout < ...
- cocos2dx 3.3创建新项目 和 VS2012解决方案加载失败问题
首先创建新项目,步骤如下: 1.进入cocos2d-x-3.3\tools\cocos2d-console\bin目录,按住shift+鼠标右键 2.输入 cocos new 项目名 –p 包名 – ...
- Cocoapod安装 - 管理第三方库
在我们开发移动应用的时候,一般都会使用到第三方工具,而由于第三方类库的种类繁多,我们在项目中进行管理也会相对麻烦,所以此时我们就需要一个包管理工具,在iOS开发中,我们使用最多的就是Cocoapods ...
- Cloud Insight 现在已经支持监控 Cassandra 啦!
Cassandra 是什么? Apache Cassandra 以其可扩展性和容错分布式数据库系统而被人所熟知.Cassandra 起源于Facebook 最初创建于 Amazon Dynamo 和谷 ...
- Axzue注册码
ahjesus Axure RP 7.0注册码 用户名:axureuser 序列号:8wFfIX7a8hHq6yAy6T8zCz5R0NBKeVxo9IKu+kgKh79FL6IyPD6lK7G6+t ...
- 【HDU 4276】The Ghost Blows Light(树形DP,依赖背包)
The Ghost Blows Light Problem Description My name is Hu Bayi, robing an ancient tomb in Tibet. The t ...
- ASP.NET Web API 2 入门(一)
前言 HTTP 不是只是为了服务的 web 页.这也是建设公开服务和数据的 Api 的强大平台.HTTP 是简单的. 灵活的和无处不在.你能想到的几乎任何平台有 HTTP 库,因此,HTTP 服务可以 ...
- 编译Firebird的源码
编译步骤:一.下载所需的软件 1.下载FB2.0 RC4 http://optusnet.dl.sourceforge.net/sourceforge/firebird/Firebird ...
- IT传道解惑:心累了就读读
写在开始 学习不是因为缺少时间而是缺少努力 Studies this matter, lacks the time, but is lacks diligently. 只要你想学好,用心去学,肯下功夫 ...
- C# 判断某程序是否运行
[DllImport("user32.dll")] private static extern bool SetForegroundWindow(IntPtr hWnd); [Dl ...