webbrowser selstart selLength

附件:http://files.cnblogs.com/xe2011/Webbrowser_SelStart.rar

1 获得webBrowser光标所在的位置

2 设置webBrowser光标的位置

3 获得webBrowser选中的文本长度

4 选中webBrowser指定的字符串

webbrowser selstart selLength

项目添加引用Microsoft.mshtml

webbrowser selstart selLength

单元引用

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>"
上一篇:pytest文档21-pytest-html报告优化(nodeid中文显示[\u6350\u52a9\u6211\u4eec]问题解决)


下一篇:html5上传文件并监听进度