首页 > 开发 > 综合 > 正文

自定义控件--图片按钮

2024-07-21 02:24:17
字体:
来源:转载
供稿:网友
图片按钮:

思路:很简单,就是在一个picturebox控件上放置一个button控件,然后将这个button添加进picturebox上(确保先拖拽picturebox,后拖拽button),设置这个button的背景色(这个时候是相对于picturebox)为透明。

 

imports system.componentmodel

public class picturebutton

    inherits system.windows.forms.usercontrol

 

#region " windows 窗体设计器生成的代码 "

 

    'usercontrol 重写 dispose 以清理组件列表。

    protected overloads overrides sub dispose(byval disposing as boolean)

        if disposing then

            if not (components is nothing) then

                components.dispose()

            end if

        end if

        mybase.dispose(disposing)

    end sub

 

    'windows 窗体设计器所必需的

    private components as system.componentmodel.icontainer

 

    '注意:以下过程是 windows 窗体设计器所必需的

    '可以使用 windows 窗体设计器修改此过程。

    '不要使用代码编辑器修改它。

    friend withevents picturebox1 as system.windows.forms.picturebox

    friend withevents button1 as system.windows.forms.button

    <system.diagnostics.debuggerstepthrough()> private sub initializecomponent()

        me.picturebox1 = new system.windows.forms.picturebox()

        me.button1 = new system.windows.forms.button()

        me.suspendlayout()

        '

        'picturebox1

        '

        me.picturebox1.name = "picturebox1"

        me.picturebox1.size = new system.drawing.size(136, 40)

        me.picturebox1.tabindex = 0

        me.picturebox1.tabstop = false

        '

        'button1

        '

        me.button1.name = "button1"

        me.button1.tabindex = 1

        me.button1.text = "button1"

        '

        'picturebutton

        '

        me.controls.addrange(new system.windows.forms.control() {me.button1, me.picturebox1})

        me.name = "picturebutton"

        me.resumelayout(false)

 

    end sub

 

#end region

    public sub new()

        mybase.new()

 

        '该调用是 windows 窗体设计器所必需的。

        initializecomponent()

 

        '在 initializecomponent() 调用之后添加任何初始化

        me.button1.width = 100 ‘设置按钮的初始大小

        me.button1.height = 23

      

 

        me.button1.backcolor = color.transparent ‘背景色透明

        me.button1.forecolor = color.black

        me.picturebox1.controls.add(me.button1)

    end sub

    private m_text as string ‘设置按钮标题

    private a as integer

    'private m_image as image

    <description("picturebox图片。")> _

    public property image() as image

        get

            return me.picturebox1.image

        end get

        set(byval value as image)

            me.picturebox1.image = value

            invalidate()

        end set

    end property

 

    shadows property forecolor() as color

        get

            return me.button1.forecolor

        end get

        set(byval value as color)

            me.button1.forecolor = value

            invalidate()

        end set

    end property

    shadows sub resetforecolor()

        me.button1.forecolor = systemcolors.controltext

    end sub

    '////

    '按钮的单击事件

    event btnclick(byval sender as object, byval e as system.eventargs)

 

    private sub button1_click(byval sender as object, byval e as system.eventargs) handles button1.click

        raiseevent btnclick(me, e)

    end sub

    '////

 

    '控件改变大小时,需重绘控件,以使子控件排位美观

    private sub filetextbox_resize(byval sender as object, byval e as system.eventargs) handles mybase.resize

        redrawcontrols()

    end sub

 

    '子控件会自动继续容器的font属性,所以改变容器的font属性时也要重绘控件

    protected overrides sub onfontchanged(byval e as system.eventargs)

        '让基控件更新文本框

        mybase.onfontchanged(e)

        '重绘控件

        redrawcontrols()

    end sub

 

    '重绘控件

    private sub redrawcontrols()

        '控件宽度

        dim width as integer = me.clientrectangle.width '获得工作区宽

        '以按钮的高度来确定控件高度

        dim btnside as integer = button1.height

        dim btnwidth as integer = button1.width

        if me.clientrectangle.height <> btnside then

            '设置控件工作区的大小

            'me.setclientsizecore(btnwidth, btnside)

            me.setclientsizecore(width, btnside) '这里使用工作区的宽是因为:按钮和picturebox可以调整宽度

            '上面的语句激发了嵌套的resize事件,因此需要立即退出,如果不退出,就会反复调用进入死循环

            exit sub

        end if

 

        '调整子控件的大小

        'txt.setbounds(0, 0, width, btnside)

        'btn.setbounds(width - 19, 2, 17, btnside - 4)

 

        me.picturebox1.setbounds(0, 0, width, btnside)

        me.picturebox1.sizemode = pictureboxsizemode.stretchimage

        me.button1.setbounds(0, 0, width, btnside)

 

    end sub end class

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表