首先要明白mshtml的属性方法:
{IHTMLWindow2 属性:}frames ????????????//返回窗口中的框架数组location ??????????//返回窗口的 location 对象history ???????????//返回窗口的 history 对象navigator ?????????//返回窗口的 navigator 对象document ??????????//返回窗口的 document 对象screen ????????????//返回 Screen 对象self ??????????????//返回当前窗口window ????????????//同 self 属性top ???????????????//返回最顶层的窗口parent ????????????//返回父窗口opener ????????????//返回创建此窗口的窗口closed ????????????//窗口是否已被关闭event ?????????????//当前事件的状态, 很有用external ??????????//执行外部命令, 譬如加入收藏等clientInformation ?//当前浏览器信息offscreenBuffering //是否使用双缓冲, 就是对象在显示之前是否要先在内存中绘制Option ????????????//可建立 Option 的接口 ?Image ?????????????//可建立 Image 的接口 ?item ??????????????//用于获取子框架 ?length ????????????//返回窗口中框架的数目 ?name ??????????????//设置或返回窗口的名称defaultStatus ?????//设置或返回窗口状态栏中的默认文本status ????????????//设置或返回窗口状态栏中的文本{IHTMLWindow2 方法:}setTimeout ????????//在指定时间(毫秒)后调用函数或计算表达式setInterval ???????//按照指定时间周期(毫秒)调用函数或计算表达式clearTimeout ??????//取消由 setTimeout 的设置clearInterval ?????//取消由 setInterval 的设置alert ?????????????//显示警告框confirm ???????????//显示可以确认或取消的对话框prompt ????????????//用户输入的对话框showModalDialog ???//创建并显示 HTML 文档的模式对话框showHelp ??????????//显示帮助文件navigate ??????????//设置新的 URL, 类似 TWebBrowser.Navigateopen ??????????????//打开一个新的浏览器窗口或查找一个已命名的窗口close ?????????????//关闭浏览器窗口focus ?????????????//获取焦点blur ??????????????//移除焦点moveTo ????????????//移动窗口到指定位置moveBy ????????????//移动窗口到相对位置resizeTo ??????????//设置窗口宽度与高度resizeBy ??????????//设置窗口相对的宽度与高度scrollTo ??????????//滚动到指定位置scrollBy ??????????//滚动到相对位置scroll ????????????//同 scrollToexecScript ????????//执行脚本{IHTMLWindow2 事件:}onload ????????????//装载完成后触发onunload ??????????//退出时触发onbeforeunload ????//退出时触发, 会发生在 onunload 之前, 如果还想后头应该用这个onhelp ????????????//显示帮助时触发onfocus ???????????//获得焦点时触发onblur ????????????//失去焦点时触发onerror ???????????//错误时触发onresize ??????????//改变大小时触发onscroll ??????????//滚动时触发{IHTMLWindow3 方法:}attachEvent ???????//绑定事件detachEvent ???????//取消事件绑定setTimeout ????????//在指定时间(毫秒)后调用函数或计算表达式setInterval ???????//按照指定时间周期(毫秒)调用函数或计算表达式print ?????????????//打印showModelessDialog //创建一个显示HTML内容的非模态对话框{IHTMLWindow3 属性:}screenLeft ???//screenTop ????//clipboardData //剪切板数据{IHTMLWindow3 事件:}onbeforeprint //打印前onafterprint ?//打印后{IHTMLWindow4 方法:}createPopup ?//创建弹出菜单{IHTMLWindow4 属性:}frameElement //获取窗口所在的框架对象
把上面相关资料搞明白,事情就会简单很多。
1、希望只有部分内容可以编辑:注意【contentEditable=true】的使用:
<BODY ><P contentEditable=true>USEGEAR(只有这里可以编辑)</p><P> </P><P> </P><P>68767896897</P><P> </P><P>yutyuty</P><P> </P></BODY>
2、希望在可编辑区光标处插入html文本or其他
((WebBrowser1.Document as IHTMLDocument2).selection.createRange as IHtmlTxtRange).pasteHTML(‘hello usegear!‘);
3、html文件中有关图像文件的处理
由于编辑结果是一个html文件,需要如同word文档带上图像的一个文件,而不可能还有个单独图像文件 ,所以把图像文件转成base64的文本放在html中
uses EncdDecd;//d自带的单元,就不要到处找了=======================var ?myStringStream : TStringStream; ?myStream :TStream;===================StringStream := TStringStream.Create(‘‘);myStream := TFileStream.Create(‘f:\QQ图片20170923175727.png‘,fmOpenRead);EncdDecd.EncodeStream(myStream,myStringStream);mmo1.Text :=‘<img contentEditable="true" src="data:image/png;base64,‘ + myStringStream.DataString +‘"</img >‘;((WebBrowser1.Document as IHTMLDocument2).selection.createRange as IHtmlTxtRange).pasteHTML(mmo1.Text);
4、其他的编辑功能,网上很多。
///网页查看模式 ???????(WebBrowser1.Document as IHTMLDocument2).designMode := ‘on‘; ???(WebBrowser1.Document as IHTMLDocument2).execCommand(‘EditMode‘,False,1);///网页编辑模式 ????(WebBrowser1.Document as IHTMLDocument2).designMode := ‘off‘; ???(WebBrowser1.Document as IHTMLDocument2).execCommand(‘BrowseMode‘,False,1); ///选中文本编辑///设置字体//////名称 ???????(WebBrowser1.Document as IHTMLDocument2).execCommand(‘FontName‘, False, ‘宋体‘);//////大小 ???????(WebBrowser1.Document as IHTMLDocument2).execCommand(‘FontSize‘, False, 7);///字体大小是从1到7////样式//////粗体 ???????(WebBrowser1.Document as IHTMLDocument2).execCommand(‘Bold‘, False, 1);//////斜体 ???????(WebBrowser1.Document as IHTMLDocument2).execCommand(‘Italic‘, False, 1);/////下划线 ??????(WebBrowser1.Document as IHTMLDocument2).execCommand(‘Underline‘, False, 1);/////删除线 ??????(WebBrowser1.Document as IHTMLDocument2).execCommand(‘StrikeThrough‘, False, 1); ???///超链接 ????????(WebBrowser1.Document as IHTMLDocument2).execCommand(‘CreateLink‘, False, 1);///删除超链接 ????(WebBrowser1.Document as IHTMLDocument2).execCommand(‘UnLink‘, False, 1); ///设置///设置文字前景色 (WebBrowser1.Document as IHTMLDocument2).execCommand(‘ForeColor‘,False, ‘Red‘);///设置文字背景色 (WebBrowser1.Document as IHTMLDocument2).execCommand(‘BackColor‘,False, ‘Blue‘); ///设置下标 ??????(WebBrowser1.Document as Ihtmldocument2).execCommand(‘SubScript‘,False,1);///设置上标 ??????(WebBrowser1.Document as Ihtmldocument2).execCommand(‘SuperScript‘,False,1); ///设置对齐方式///设置左对齐 ??????(WebBrowser1.Document as IHTMLDocument2).execCommand(‘JustifyLeft‘, False, 0);///设置中对齐 ??????(WebBrowser1.Document as IHTMLDocument2).execCommand(‘JustifyCenter‘, False, 0);///设置右对齐 ??????(WebBrowser1.Document as IHTMLDocument2).execCommand(‘JustifyRight‘, False, 0);///设置两端对齐 ????(WebBrowser1.Document as IHTMLDocument2).execCommand(‘JustifyFull‘, False, 0); ?///缩进/// ???向右缩进 ???(WebBrowser1.Document as Ihtmldocument2).execCommand(‘Indent‘,True,1);/// ???向左缩进 ???(WebBrowser1.Document as Ihtmldocument2).execCommand(‘Outdent‘,True,1); ///清除格式 ????(WebBrowser1.Document as Ihtmldocument2).execCommand(‘Removeformat‘,True,0); ///序列查看/// ???数字格式 ?(WebBrowser1.Document as Ihtmldocument2).execCommand(‘InsertOrderedList‘,True,0);/// ???圆点查看 ?(WebBrowser1.Document as Ihtmldocument2).execCommand(‘InsertUnorderedList‘,True,0); ///插入图片(静态) ??????(WebBrowser1.Document as IHTMLDocument2).execCommand(‘InsertImage‘, True, ‘‘); ?????????????????????????(WebBrowser1.Document as IHTMLDocument2).execCommand(‘InsertImage‘, False, ‘C:\1.png‘); ///插入HTML组件///后面的字符串为这个控件的ID号///直线 ?????????Line ????????(WebBrowser1.Document as IHTMLDocument2).execCommand(‘InsertHorizontalRule‘, True, ‘‘);///按钮 ?????????Button ??????(WebBrowser1.Document as IHTMLDocument2).execCommand(‘InsertButton‘, True, ‘‘);///复选框 ???????CheckBox ????(WebBrowser1.Document as IHTMLDocument2).execCommand(‘InsertInputCheckbox‘, True, ‘‘);///单选框 ???????Radio ???????(WebBrowser1.Document as IHTMLDocument2).execCommand(‘InsertInputRadio‘, True, ‘‘);///编辑框 ???????Edit ????????(WebBrowser1.Document as IHTMLDocument2).execCommand(‘InsertInputText‘, True, ‘‘);///文本框 ???????Memo ????????(WebBrowser1.Document as IHTMLDocument2).execCommand(‘InsertTextArea‘, True, ‘‘);///密码框 ???????PswEdit ?????(WebBrowser1.Document as IHTMLDocument2).execCommand(‘InsertInputPassword‘, True, ‘‘);///组框架 ???????GroupBox ????(WebBrowser1.Document as IHTMLDocument2).execCommand(‘InsertIFrame‘, True, ‘‘);///列表框 ???????ListBox ?????(WebBrowser1.Document as IHTMLDocument2).execCommand(‘InsertSelectListbox‘, True, ‘‘);///组合框 ???????Combobox ????(WebBrowser1.Document as IHTMLDocument2).execCommand(‘InsertSelectDropdown‘, True, ‘‘);
2ccc上有个高人n年前写的浏览器编辑器,可以参考Register_HtmlEdit
用Twebbrowser做可控编辑器与MSHTML
原文地址:https://www.cnblogs.com/usegear/p/8150562.html