動態定義POPUP,涉及字串處理、遞迴、菜單定義語句等等。
也挺費勁的,。
做出這個函數,就可擴充基類 _FORM, 以加上 介面:_c菜單定義 、_v菜單事件() 。
(1) 使用例子
TEXT to cMenu
檔案
開啟
EXCEL
WORD
關閉
Quit
編輯
Copy
paste
協助
ENDTEXT
glDefinePopup(cMenu,'abc')
Activate Popup abc
(2)函數定義
*******************************************************************
Function glDefinePopup()Function glDefinePopup
Lparameters tcPopupDefine, tcPopupName
*- 功能: 定義多級的關聯popup菜單
*-
*- 參數
*- (1) tcPopupDefine: 菜單定義字串,每項以換行分隔,以TAB確定層級
*- 例子:
*- text to cPpDefine
*- 檔案
*- 開啟
*- EXCEL
*- WORD
*- 關閉
*- endtext
*-
*- (2) tcPopupName: 定出來的POPUP的名字
*-
Local i,cLine,n當前級數,n上級id_
Release POPUPS (tcPopupName) extended
*- 轉變為鏈表結構 ----------------
Create Cursor _csDefine_menu_MenuStru (id_ i , fid_ i , 菜單定義 c(240), 級數 i,子女數 i )
For i=1 to GetWordCount(tcPopupDefine,Chr(13))
cLine = Alltrim( GetWordNum(tcPopupDefine,i,Chr(13)) , 1, Chr(10),Chr(32))
If Empty(cLine)
Loop
EndIf
n當前級數 = _n求級數(cLine)
n上級id_=0
Do while not bof() && 往回找,找到層級比自己小[1級]的, 就是父. 取其id作fid_, 父的子女數+1
If 級數 < n當前級數
n上級id_ = id_
Replace 子女數 with 子女數+1
Exit
EndIf
Skip -1
EndDo
Append Blank
Replace id_ with i, fid_ with n上級id_, 級數 with n當前級數, 菜單定義 with Ltrim(cLine,1,Chr(9))
Next
_vDefinePopUp(0,0,tcPopupName)
Return .t.
**********************************************
Function _vDefinePopup()Function _vDefinePopup
Lparameters tn上級id, tn上級barID, tc上級popUp名
Local nBarCnt,nRecNo,c本級popup名
If tn上級barID = 0
c本級popup名 = tc上級popUp名
Else
c本級popup名 = tc上級popUp名+'_'+Alltrim(Str(tn上級barID,10))
_vfp.DoCmd( Textmerge('On Bar <<tn上級barID>> of <<tc上級popUp名>> activate popup <<c本級popup名>>'))
EndIf
_vfp.DoCmd( Textmerge("Define Popup <<c本級popup名>> shortcut relative "))
_vfp.DoCmd( Textmerge("On Selection Popup <<c本級popup名>> Deactivate Popup <<c本級popup名>> "))
*---
nBarCnt=0
nRecNo=Recno()
Scan for fid_ = tn上級id
nBarCnt = nBarCnt + 1
_vfp.DoCmd( Textmerge("define Bar <<nBarCnt>> of <<c本級popup名>> prompt '<<Trim(菜單定義)>>' " ))
If 子女數 > 0
_vDefinePopup(id_,nBarCnt,c本級popup名)
EndIf
EndScan
Go (nRecNo)
Return
**********************************************
Function _n求級數()Function _n求級數
Lparameters tcStr
Local nCnt,i
nCnt=0
For i=1 To Len(tcStr)
If Substr(tcStr,i,1)=Chr(9)
nCnt = nCnt + 1
Else
Exit
Endif
Next
Return nCnt+1
**********************************************************