asen 发表于 2021-12-14 19:22:34

常用交互式消息框MsgBox 和输入框EditBox

本帖最后由 asen 于 2021-12-20 08:38 编辑

自己使用的两个通用函数, 现和大家分享.


MsgBox 消息框

EditBox 输入框



; 消息,按钮( OK,OKCancel,Yes,YesNo,YesNoCancel ),默认选中的按钮(Yes,No,Cancel),标题;
(defun MsgBox(Msg Buttons DefBt Title / FP CANCEL DEFBT1 DEFBT2 DEFBT3 DEFBTC DEFBTN DEFBTY FL FP HEIGHT I I-J ID J NO STD WIDTH YES ~CANCEL ~NO ~YES )
      (setq ~yes nil ~no nil ~cancel nil)
    (cond ((wcmatch buttons "*OK*") (setq yes "OK"~yes "确定"))
          ((wcmatch buttons "*Yes*") (setq yes "Yes" ~yes "是"))
    )
    (if (wcmatchbuttons "*No*") (setq no "No"~no "否"))
    (if (wcmatchbuttons "*Cancel*") (setq cancel "Cancel" ~cancel "取消"))

    (setq DefBtY "" DefBtN "" DefBtC "")
      (cond ((= DefBt "Yes") (setq DefBtY "is_default=true;"))
          ((= DefBt "No") (setq DefBtN "is_default=true;"))
          ((= DefBt "Cancel") (setq DefBtC "is_default=true;"))
    )
      (setq Msg (strcat "\n" Msg "\n\n"))
      ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
    (setq i -1 height 0 width 0)
      (while (setq i (vl-string-search "\n" Msg (1+ (setq j i))))
      (if (> (setq i-j (- i j)) 50)(setq height (+ height 1 (fix (/ i-j 50.0)))))
      (setq width (max width (- i j))height (1+ height))
    )
      ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
      (setq fp (vl-filename-mktemp "~HGCAD.dcl"))
      (if (not (setq fl (open fp "w")))(exit))
       (write-line (strcat "MsgBox:dialog{label=" title "; :column{")fl)
      (write-line (strcat ":row{spacer_1;:text_part{key=\"msg\";width=" (itoa (min width 50)) ";height=" (itoa height) ";}}") fl)
       (write-line ":row{spacer_1;" fl)
       (if ~yes (write-line (strcat ":cancel_button{label=" ~yes ";key=\"yes\";width=10;" DefBtY "}") fl))
       (if ~no (write-line (strcat ":cancel_button{label=" ~no";key=\"no\";width=10;" DefBtN "}") fl))
       (if ~cancel (write-line (strcat ":cancel_button{label=" ~cancel ";key=\"cancel\";width=10;" DefBtC "}") fl) )
       (write-line "spacer_1;}}}" fl)
      (close fl)
      
    (setq std 0 id (load_dialog fp))
    (new_dialog "MsgBox" id)
    (set_tile "msg" msg)
    (if ~yes (action_tile "yes" "(done_dialog 1)"))
    (if ~no(action_tile "no" "(done_dialog 2)"))
    (if ~cancel (action_tile "cancel" "(done_dialog 3)"))
    (setq std (start_dialog))
    (unload_dialog id)
    (vl-file-delete fp)


      (cond ((= std 1) yes)
          ((= std 2) no)
          ((= std 3) cancel)
    )

)

; 消息,输入框默认值,标题
(defun EditBox(Msg Edit Title /FL FP ID RTEDIT STD )
      ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
      (setq Msg (strcat " " Msg " "))
      (setq fp (vl-filename-mktemp "~HGCAD.dcl"))
      (if (not (setq fl (open fp "w")))(exit))
       (write-line (strcat "EditBox:dialog{label=" title "; initial_focus = \"edit\";:column{")fl)
      (write-line (strcat "spacer_1;:text_part{key=\"msg\";width=" (itoa (strlen msg)) ";}") fl)
      (write-line ":edit_box {key=\"edit\";}" fl)
       (write-line ":row{spacer_1;}" fl)
       (write-line ":row{spacer_1;" fl)
       (write-line ":cancel_button{label=\"确定\";key=\"OK\";width=8;is_default=true;}" fl)
       (write-line "spacer_1; :cancel_button{label=\"取消\";key=\"cancel\";width=8;}" fl)
       (write-line "spacer_1;}}}" fl)
      (close fl)
      
    (setq std 0 id (load_dialog fp))
    (new_dialog "EditBox" id)
    (set_tile "msg" Msg)
    (set_tile "edit" Edit)
    (action_tile "OK" "(setq Rtedit (get_tile \"edit\"))(done_dialog 1)")
    (action_tile "cancel" "(done_dialog 2)")
    (setq std (start_dialog))
    (unload_dialog id)
    (vl-file-delete fp)

      (cond ((= std 1) Rtedit)
          ((= std 2) "")
    )
)

(defun C:n ()

(setq ret (MsgBox "是否继续操作?" "YesNo" "No" "提示"))

(setq ret (EditBox "请输入数值" "123" "输入"))

)




测试:

(setq ret (MsgBox "是否继续操作?" "YesNo" "No" "提示"))
(setq ret (EditBox "请输入数值" "123" "输入"))

yanshengjiang 发表于 2021-12-19 12:51:48

mokson 发表于 2021-12-16 08:17
需要安装 OpenDCL 库吗?

你都是论坛长老了还在问这个问题,你都学了些什么?

asen 发表于 2021-12-15 21:41:57

xiongyuer 发表于 2021-12-15 21:41
editbox可以做多个输入box吗

可以, 你参考源码

nxchenjk 发表于 2022-6-23 11:47:28

两个程序很适用,感谢分享。另外输入框能不能加入回车也可以确定呢?

伪书虫86 发表于 2021-12-14 21:40:03

想法不错

夏生生 发表于 2021-12-15 07:12:36

建议加个point参数

Wanda 发表于 2021-12-15 08:23:15

雾草好牛批哦

言戲無軍 发表于 2021-12-15 09:10:39

不错 一直想写 没写出来

mokson 发表于 2021-12-15 09:27:00

对 ET 有依赖

asen 发表于 2021-12-15 21:36:46

mokson 发表于 2021-12-15 09:27
对 ET 有依赖

没有依赖, vl

xiongyuer 发表于 2021-12-15 21:41:00

editbox可以做多个输入box吗

mokson 发表于 2021-12-16 08:17:12

需要安装 OpenDCL 库吗?
页: [1] 2
查看完整版本: 常用交互式消息框MsgBox 和输入框EditBox