vb.net點(diǎn)擊按鈕無效的toolbar
2024-07-10 13:00:55
供稿:網(wǎng)友
本文來源于網(wǎng)頁設(shè)計(jì)愛好者web開發(fā)社區(qū)http://www.html.org.cn收集整理,歡迎訪問。
大家寫程序的時(shí)候,都會(huì)遇到現(xiàn)有控件不能滿足要求的問題,這時(shí)需要借助第三方控件或者自己重新改寫現(xiàn)有控件。前者就不多說了,網(wǎng)上找,公司里找,同學(xué)中找。如果是自己寫呢?我的體會(huì)如下:
1.看清需求。知道自己想做什么,需要完成什么樣的功能。拿下面代碼舉例:需求-toolbarbutton必須為可用;當(dāng)左鍵點(diǎn)擊toolbarbutton時(shí),由主程序來通過一些條件(如用戶是否按照規(guī)定的步驟操作)判斷是否忽略該消息,忽略消息后界面應(yīng)該沒有任何變化。
2.尋找差距。找出自己想要的功能和現(xiàn)有控件的差別。拿下面代碼舉例:現(xiàn)有控件toolbar中,只要左鍵點(diǎn)擊可用的toolbarbutton,該button都會(huì)有所反映;而需求是不讓它有反應(yīng)。
3.尋找現(xiàn)有控件如何實(shí)現(xiàn)差距。拿下面代碼舉例:toolbar在繪制過程中沒有使用可重寫的onpaint方法,所以重寫onpaint方法不能完成需求。在哪能提取到重繪的信息呢?wndproc。
4.設(shè)計(jì)好類的接口。之所以我們要重寫現(xiàn)有控件,是因?yàn)槲覀円褂盟F(xiàn)在沒有的功能,所以把接口設(shè)計(jì)好,對以后的修改大有裨益。拿下面代碼舉例:提供給主程序的事件參數(shù)中就包含了toolbarbuttons,可能以后主程序要根據(jù)鼠標(biāo)的左右鍵作一些判斷,或修改一些外觀。
5.開始編碼。盡量規(guī)范,以便以后修改、查看。
代碼如下:
public class clstoolbar
inherits toolbar
public event previewbuttonclick as previewbuttonclickhandler
private m_blncandown as boolean = true
private function zgetmousedownbutton(byval point as point) as toolbarbutton
for each _tbtn as toolbarbutton in me.buttons
if _tbtn.rectangle.contains(point) then
return _tbtn
end if
next
return nothing
end function
protected overrides sub wndproc(byref m as system.windows.forms.message)
if m.msg = cint(&h201) orelse m.msg = cint(&h203) then‘鼠標(biāo)左鍵為&h201,雙擊為&h203
dim _point as point = me.pointtoclient(me.mouseposition)
dim _tbtntemp as toolbarbutton = zgetmousedownbutton(_point)
if not _tbtntemp is nothing then
dim _args as new mybuttonclickeventargs(mousebuttons.left, _tbtntemp)
raiseevent previewbuttonclick(me, _args)
if _args.cancel then
m_blncandown = false
exit sub
end if
end if
end if
if m.msg = cint(&hf) then重畫為&hf
if m_blncandown = false then
exit sub
end if
end if
if m.msg = cint(&h200) then移動(dòng)鼠標(biāo)為&h200
m_blncandown = true
end if
mybase.wndproc(m)
end sub
end class
public delegate sub previewbuttonclickhandler(byval s as object, byval e as mybuttonclickeventargs)
public class mybuttonclickeventargs
private m_blncancel as boolean = false
private m_btnclick as mousebuttons
public property cancel() as boolean
get
return me.m_blncancel
end get
set(byval value as boolean)
me.m_blncancel = value
end set
end property
public readonly property mousebutton() as mousebuttons
get
return me.m_btnclick
end get
end property
public toolbarbutton as toolbarbutton
public sub new(byval mousebutton as mousebuttons, byval button as toolbarbutton)
me.m_btnclick = mousebutton
me.toolbarbutton = button
end sub
end class