ニーズはそれなりにありそうなのに、検索してもそのものずばりのコードがみつからなかったので、LAW launcherで使っているコードを貼り付けておきます。
private void SearchProc(string SearchKeyWord)
{
if (webBrowser1.Document != null)
{
HtmlDocument doc = webBrowser1.Document;
mshtml.IHTMLDocument2 doc2 =doc.DomDocument as mshtml.IHTMLDocument2;
mshtml.IHTMLTxtRange textRange = ((mshtml.IHTMLBodyElement)doc2.body).createTextRange();
bool isFound;
textRange = doc2.selection.createRange() as mshtml.IHTMLTxtRange;
if ((Control.ModifierKeys & Keys.Shift) == Keys.Shift) //Shift押してたら上方向に検索
{
textRange.moveStart("textedit", -1);
if (textRange.text != null) textRange.moveEnd("character", -1);
else textRange.moveEnd("textedit", 1);
isFound = textRange.findText(SearchKeyWord, -1, 2);
}
else //Shift押してなかったら下方向に検索
{
if (textRange.text!=null) textRange.moveStart("character", 1);
textRange.moveEnd("textedit", 1);
isFound = textRange.findText(SearchKeyWord, 1, 2);
}
if (isFound)
{
textRange.scrollIntoView(true);//見つかったらスクロール
textRange.select();//そして範囲選択
}
else
{
MessageBox.Show("「"+SearchKeyWord+"」は見つかりませんでした。");//見つからなかったらメッセージ
}
}
}
コメント