autoLisp 显示及控制 IE 浏览器的一种方法

用 autolisp 显示一个浏览器窗口,并展示来自程序或网络的内容。通常情况,可以创建一个 activex 对象进行操作。IE 文档对象的方法、事件、属性参考 MSDN 网站,但是在 win10 x64 环境中不可行,具体如下。

在 CAD 命令行中运行以下代码:
(setq ie (vlax-create-object “InternetExplorer.Application”))
(vlax-property-available-p ie 'Document) ;;返回 nil
(vlax-method-applicable-p ie 'Navigate) ;;返回nil
(vlax-dump-object ie t) ;;返回对象不支持 ITypeInfo 接口

这种方法在 win10 x64 环境中行不通,经过本人测试,在 AutoCAD 中如果不用 OpenDCL 第三方插件,可以用 Windows 的 hta 技术来创建并控制一个网页,完美解决 autolisp 控制网页,达到曲线救国的目的。

而如果需要 浏览器 向 CAD 回传数据,则需要借助 IE 的 activex 对象,通过 COM 接口向 CAD 输入数据。
将以下代码保存到磁盘并在 CAD 中加载后,在命令窗口依次输入:
(lsp2html) 创建一个 ie 对象
(add-dot) 向浏览器添加文档节点

;; lisp 创建及控制 ie 对象示例
(defun lsp2html (/ runStr) 
  (setq obj_ws (vlax-get-or-create-object "WScript.Shell")
        runStr (strcat (getenv "Windir")  ;;showintaskbar=no 任务栏不显示图标
                       "\\syswow64\\mshta.exe about:\"<head><hta:application><object id='shell' classid='clsid:8856F961-340A-11D0-A96B-00C04FD705A2'><param name=RegisterAsBrowser value=1></object><script>shell.putproperty('AutocadDemo',document.parentWindow);</script></head>\""
               )
  )
  (vlax-invoke obj_ws 'run runStr 0 0) ;; 最后一个 0 是异步执行
  (vlax-release-object obj_ws)
)

;; 获取 ie 窗口的控制句柄
;; 由于 ie 窗口异步创建,如果运行 (lsp2html) 后立即执行本函数,将无法获取窗口控制,需要一定的延迟时间
(defun get-windows (/ n counts) 
  (setq obj_app (vlax-get-or-create-object "Shell.Application")
        win     (vlax-invoke obj_app 'Windows)
        counts  (vlax-get-property win 'count)
        n       0
  )
  (while 
    (progn 
      (setq f (vlax-invoke win 'item n)
            n (1+ n)
      )
      (setq *html* (if f (vlax-invoke f 'GetProperty "AutocadDemo")))
      (and (<= n counts) (null *html*))
    )
  )
  (vlax-release-object obj_app)
)

;; 向浏览器窗口输出网页文字
;; 右键页面刷新后,仍将显示 write 的输出内容
(defun doc-write () 
  (if (null *html*) (get-windows))
  (setq *doc* (vlax-get-property *html* 'document)) ;; 获取浏览器的 document 对象
  (vlax-put-property *doc* 'title "AutoLisp调用浏览器展示")
  (vlax-invoke *doc* 
               'write
               "<html><head></head><body><h1>LISP生成的网页</h1><div id='divA'>使用 AutoLisp 控制浏览器对象,展示新的内容</div></body></html>"
  )
)

;; document 对象节点控制
(defun add-dot ()
  (if (null *doc*) (doc-write))
  (setq divA (vlax-invoke *doc* 'getElementById "divA")) ;; 获取 id 节点元素对象
  (if divA 
    (progn 
      (setq oldhtml (vlax-get-property divA 'innerHTML)
            newhtml (strcat oldhtml 
                            "<br><font color='red'>采用 Windows HTA 技术,</font><b>将网页整合</b>为文件" 
                            "<br><button οnclick='runAlert()'>修改标题</button><button οnclick='reWinSize()'>浏览器尺寸</button>" 
                            "<br><button οnclick='window.print()'>页面打印</button><button οnclick='winClose()'>窗口关闭</button>"
                            "<br><img class='picture' src='http://bbs.mjtd.com/static/image/common/logo.png'>"
                    ) 
      )
      (vlax-put-property divA 'innerHTML newhtml) ;; 修改 id 节点内容
    )
  )
)

;; 页面添加 css 样式文件
(defun add-css () 
  ;; 创建一个 css 样式组件 link 对象
  (setq css (vlax-invoke *doc* 'createElement "link"))
  (vlax-put-property css 'rel "stylesheet")
  (vlax-put-property css 'href "d:\\cad\\test\\demo.css")
  (setq heads (vlax-invoke *doc* 'getElementsByTagName "head")
        head  (vlax-invoke heads 'item 0)
  )
  (vlax-invoke head 'appendChild css) ;; 在 head 后添加
)

;; 页面导入 JavaScript 文件引用
(defun add-js () 
  ;; 创建一个 script 元素对象
  (setq js (vlax-invoke *doc* 'createElement "script"))
  (vlax-put-property js 'type "text/javascript")
  (vlax-put-property js 'src "d:\\cad\\test\\demo.js") ;; 文件引用式添加
  (setq heads (vlax-invoke *doc* 'getElementsByTagName "head")
        head  (vlax-invoke heads 'item 0)
  )
  (vlax-invoke head 'appendChild js) ;; 在 head 后添加
)

将以下 js 代码保存 d:\cad\test\demo.js 文件中,并在 CAD 中运行 (add-js) ,即可向页面导入 javascript 文件

// 修改窗口标题
function runAlert() {
  document.title = "CAD控制浏览器示例";
  alert('窗口标题已修改');
}


// 页面移动及尺寸
function reWinSize() {
  window.moveTo(20, 20);  // 窗口移动
  window.resizeTo(600, 350);  // 窗口大小
}


// 页面关闭
function winClose() {
  window.opener = null;
  window.open('', '_self');
  window.close();
}

将以下 css 样式文件保存在 d:\cad\test\demo.css 中,并在 CAD 命令行中运行 (add-css) 向页面添加 css 样式文件

* {
  padding: 0;
  margin: 0;
}

body {
  background-color: burlywood;
  overflow-y: hidden;
  margin: 0 auto;
}

#divA {
  padding: 10px;
  background-color: #ebd7a1;
  line-height: 20px;
  text-align: center;
  border-radius: 10px;
}

button {
  margin: 3px 0;
}

.picture {
  margin: 10px;
}

使用 CAD 命令生成浏览器窗口效果
在这里插入图片描述

  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

yxp_xa

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值