核心 API:mshtml.IHTMLDocument2 + IHTMLTxtRange
using mshtml; // 需加入參考 Microsoft.mshtml
private void button1_Click(object sender, EventArgs e) {
IHTMLDocument2 doc = (IHTMLDocument2)webBrowser1.Document.DomDocument;
IHTMLTxtRange range = doc.selection.createRange() as IHTMLTxtRange;
range.pasteHTML(range.text + "<div>jinjazz 路過</div>");
}
關鍵步驟
- 取得
Document.DomDocument 轉型為 IHTMLDocument2。
- 呼叫
selection.createRange() 獲得目前選取範圍。
- 使用
pasteHTML 在游標處插入任意 HTML 片段。
應用場景
- 自訂編輯器工具列 (插入表格、圖片、樣式)。
- 表單自動填寫、範本套用。
- 內容管理系統前端編輯增強。