首页 > 开发 > .Net > 正文

vb.net点击按钮无效的toolbar

2020-02-03 15:22:45
字体:
来源:转载
供稿:网友
  • 本文来源于网页设计爱好者web开发社区http://www.html.org.cn收集整理,欢迎访问。
  •   
    大家写程序的时候,都会遇到现有控件不能满足要求的问题,这时需要借助第三方控件或者自己重新改写现有控件。前者就不多说了,网上找,公司里找,同学中找。如果是自己写呢?我的体会如下:
    1.看清需求。知道自己想做什么,需要完成什么样的功能。拿下面代码举例:需求-toolbarbutton必须为可用;当左键点击toolbarbutton时,由主程序来通过一些条件(如用户是否按照规定的步骤操作)判断是否忽略该消息,忽略消息后界面应该没有任何变化。
    2.寻找差距。找出自己想要的功能和现有控件的差别。拿下面代码举例:现有控件toolbar中,只要左键点击可用的toolbarbutton,该button都会有所反映;而需求是不让它有反应。
    3.寻找现有控件如何实现差距。拿下面代码举例:toolbar在绘制过程中没有使用可重写的onpaint方法,所以重写onpaint方法不能完成需求。在哪能提取到重绘的信息呢?wndproc。
    4.设计好类的接口。之所以我们要重写现有控件,是因为我们要使用它现在没有的功能,所以把接口设计好,对以后的修改大有裨益。拿下面代码举例:提供给主程序的事件参数中就包含了toolbarbuttons,可能以后主程序要根据鼠标的左右键作一些判断,或修改一些外观。
    5.开始编码。尽量规范,以便以后修改、查看。
    代码如下:
    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‘鼠标左键为&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’移动鼠标为&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
    发表评论 共有条评论
    用户名: 密码:
    验证码: 匿名发表