最近剛在學REALbasic,在使用ContextualMenu控制項時,卻遇到一個蠻莫名其妙的問題。不知道各位REALbaisc的使用者有沒有遇到過。
問題如下:
先開啟一個新的專案,然後把一個ContextualMenu控制項放在Window1上,預設的Name是ContextualMenu1
screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt='Click here to open new window\nCTRL+Mouse wheel to zoom in/out';}" ?nclick="if(!this.resized) {return true;} else {window.open('http://img293.imageshack.us/img293/822/63662362cu8.jpg');}" ?nmouseover="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='Click here to open new window\nCTRL+Mouse wheel to zoom in/out';}" ?nmousewheel="return imgzoom(this);">
接著在ContextualMenu1的Open事件中加入三個選項
screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt='Click here to open new window\nCTRL+Mouse wheel to zoom in/out';}" ?nclick="if(!this.resized) {return true;} else {window.open('http://img261.imageshack.us/img261/6445/19152918em6.jpg');}" ?nmouseover="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='Click here to open new window\nCTRL+Mouse wheel to zoom in/out';}" ?nmousewheel="return imgzoom(this);">
然後,在ContextualMenu1的Action事件中加入當ContextualMenu1的選項被點選時,所要反應的動作
screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt='Click here to open new window\nCTRL+Mouse wheel to zoom in/out';}" ?nclick="if(!this.resized) {return true;} else {window.open('http://img328.imageshack.us/img328/7313/23tj6.jpg');}" ?nmouseover="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='Click here to open new window\nCTRL+Mouse wheel to zoom in/out';}" ?nmousewheel="return imgzoom(this);">
再來,在Window1的MouseDown事件中加入判斷是否點了滑鼠右鍵;若是的話,則顯示剛剛加入的ContextualMenu1
screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt='Click here to open new window\nCTRL+Mouse wheel to zoom in/out';}" ?nclick="if(!this.resized) {return true;} else {window.open('http://img261.imageshack.us/img261/8110/21zb3.jpg');}" ?nmouseover="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='Click here to open new window\nCTRL+Mouse wheel to zoom in/out';}" ?nmousewheel="return imgzoom(this);">
最後直接運行程式,正常的話,在程式中點右鍵時,會出現一個有三個選項的選單
screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt='Click here to open new window\nCTRL+Mouse wheel to zoom in/out';}" ?nclick="if(!this.resized) {return true;} else {window.open('http://img328.imageshack.us/img328/92/76829428ue1.jpg');}" ?nmouseover="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='Click here to open new window\nCTRL+Mouse wheel to zoom in/out';}" ?nmousewheel="return imgzoom(this);">
接著問題來了,當點了某一個選項時,理應會觸發ContextualMenu1的Action事件中的其中一項才對,不過...事實上並沒有任何反應。
難道是ContextualMenu1的Action事件中的If..ElseIf...都沒有被觸發到嗎?為了確認Action事件是否有被觸發,我直接在其中顯示出被點選的選項字串及其字串的長度
screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt='Click here to open new window\nCTRL+Mouse wheel to zoom in/out';}" ?nclick="if(!this.resized) {return true;} else {window.open('http://img261.imageshack.us/img261/7376/11bu9.jpg');}" ?nmouseover="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='Click here to open new window\nCTRL+Mouse wheel to zoom in/out';}" ?nmousewheel="return imgzoom(this);">
執行後,點右鍵後並點第一個選項後,接下來顯示的結果更是讓我嚇到啊~~~
screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt='Click here to open new window\nCTRL+Mouse wheel to zoom in/out';}" ?nclick="if(!this.resized) {return true;} else {window.open('http://img261.imageshack.us/img261/3168/12ug5.jpg');}" ?nmouseover="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='Click here to open new window\nCTRL+Mouse wheel to zoom in/out';}" ?nmousewheel="return imgzoom(this);">
疑~怎麼沒有顯示出字串的長度咧?是我寫錯的嗎?為了再確認,我把它拆開來顯示看看
screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt='Click here to open new window\nCTRL+Mouse wheel to zoom in/out';}" ?nclick="if(!this.resized) {return true;} else {window.open('http://img261.imageshack.us/img261/2271/13ng3.jpg');}" ?nmouseover="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='Click here to open new window\nCTRL+Mouse wheel to zoom in/out';}" ?nmousewheel="return imgzoom(this);">
執行後,點右鍵後並點第一個選項後,第一個顯示的是所點選的Menu 1字串
screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt='Click here to open new window\nCTRL+Mouse wheel to zoom in/out';}" ?nclick="if(!this.resized) {return true;} else {window.open('http://img328.imageshack.us/img328/5599/14ag5.jpg');}" ?nmouseover="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='Click here to open new window\nCTRL+Mouse wheel to zoom in/out';}" ?nmousewheel="return imgzoom(this);">
沒錯~這是我剛點的選項,按下確定後,出現的應該是Menu 1字串的長度6吧~
screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt='Click here to open new window\nCTRL+Mouse wheel to zoom in/out';}" ?nclick="if(!this.resized) {return true;} else {window.open('http://img261.imageshack.us/img261/8018/15iq9.jpg');}" ?nmouseover="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='Click here to open new window\nCTRL+Mouse wheel to zoom in/out';}" ?nmousewheel="return imgzoom(this);">
哇~顯示的是12耶,這是怎麼一回事...長度變兩倍了!再試其它的選項,也都是這樣,難道這是ContextualMenu控制項的bug嗎?找了一些討論區的文章來看,也沒看到解法。而ContextualMenu控制項在新版的REALbasic 2007 Release 1中也被棄用了,看來真的是有問題吧!
在REALbasic官網的ReleaseNote裡有這麼一段:
screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt='Click here to open new window\nCTRL+Mouse wheel to zoom in/out';}" ?nclick="if(!this.resized) {return true;} else {window.open('http://img261.imageshack.us/img261/7983/17pk2.jpg');}" ?nmouseover="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='Click here to open new window\nCTRL+Mouse wheel to zoom in/out';}" ?nmousewheel="return imgzoom(this);">
官方建議我們使用Window的ConstructContextualMenu事件及ContextualMenuAction事件;或者使用MenuItem.Pupup()。
目前手上沒有REALbasic 2007 Release 1,不過若不用控制項的話,就是要透過MenuItem類別來做到同樣的效果。一樣首先在Window1的MouseDown事件中加入判斷IsContexnualClick()的動作
screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt='Click here to open new window\nCTRL+Mouse wheel to zoom in/out';}" ?nclick="if(!this.resized) {return true;} else {window.open('http://img153.imageshack.us/img153/1632/18im9.jpg');}" ?nmouseover="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='Click here to open new window\nCTRL+Mouse wheel to zoom in/out';}" ?nmousewheel="return imgzoom(this);">
這邊的DoContextualMenu()是一個自設的方法,要用來建立並加入MenuItem用的
screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt='Click here to open new window\nCTRL+Mouse wheel to zoom in/out';}" ?nclick="if(!this.resized) {return true;} else {window.open('http://img261.imageshack.us/img261/3466/24dx6.jpg');}" ?nmouseover="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='Click here to open new window\nCTRL+Mouse wheel to zoom in/out';}" ?nmousewheel="return imgzoom(this);">
用MenuItem.PopUp()時會回傳一個被點選的MenuItem,因此就能直接來比對看是點到了那一個。這樣就能正常的使用ContextualMenu的功能了。
screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt='Click here to open new window\nCTRL+Mouse wheel to zoom in/out';}" ?nclick="if(!this.resized) {return true;} else {window.open('http://img261.imageshack.us/img261/7780/20cs6.jpg');}" ?nmouseover="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='Click here to open new window\nCTRL+Mouse wheel to zoom in/out';}" ?nmousewheel="return imgzoom(this);">
不過我目前能用的是REALbasic 2006 R4,總覺得不能直接用ContextualMenu控制項還蠻惋惜的!在我試了幾次後,發現了一種較有效的方法來順利的使用ContextualMenu控制項。
我使用的方法是用InStr()來比對選項字串,若所點選的選項字串跟比對的是一樣的,就表示點選了這一個選項
Window1的MouseDown事件中的程式不變
screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt='Click here to open new window\nCTRL+Mouse wheel to zoom in/out';}" ?nclick="if(!this.resized) {return true;} else {window.open('http://img261.imageshack.us/img261/8599/21rz3.jpg');}" ?nmouseover="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='Click here to open new window\nCTRL+Mouse wheel to zoom in/out';}" ?nmousewheel="return imgzoom(this);">
ContextualMenu控制項中Open事件中的程式也不變
screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt='Click here to open new window\nCTRL+Mouse wheel to zoom in/out';}" ?nclick="if(!this.resized) {return true;} else {window.open('http://img261.imageshack.us/img261/545/24218974mh9.jpg');}" ?nmouseover="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='Click here to open new window\nCTRL+Mouse wheel to zoom in/out';}" ?nmousewheel="return imgzoom(this);">
只要修改ContextualMenu控制項中Action事件的程式碼即可
screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt='Click here to open new window\nCTRL+Mouse wheel to zoom in/out';}" ?nclick="if(!this.resized) {return true;} else {window.open('http://img261.imageshack.us/img261/8199/25pb6.jpg');}" ?nmouseover="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='Click here to open new window\nCTRL+Mouse wheel to zoom in/out';}" ?nmousewheel="return imgzoom(this);">
這樣改用InStr()來比對後,果然能順利的使用ContextualMenu控制項。雖然ContextualMenu控制項中在REALbasic 2007 Release 1中被棄用了,不過舊版的就可以使用InStr()來避免掉這bug!