本帖最后由 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 (wcmatch buttons "*No*") (setq no "No" ~no "否"))
- (if (wcmatch buttons "*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" "输入"))
|