首页 > 开发 > 综合 > 正文

自定义控件--xp风格按钮(可设置文字颜色)

2024-07-21 02:24:16
字体:
来源:转载
供稿:网友

imports system.drawing

imports system.componentmodel

public class winxpbutton

    inherits system.windows.forms.button

  

    private my_mousedown as boolean = false '鼠标按下

    private my_mousehover as boolean = false '鼠标移到上面

    private m_textcolor as color = system.drawing.color.black '字体颜色

    <description("字体颜色。")> _

    public property textcolor() as color

        get

            return m_textcolor

        end get

        set(byval value as color)

            m_textcolor = value

            me.invalidate()

        end set

    end property

    public sub new()

        mybase.new()

  

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

        initializecomponent()

  

        '在 initializecomponent() 调用之后添加任何初始化,true表示将指定的样式应用到控件

  

        '设置控件样式位能够充分地更改控件行为

        me.setstyle(controlstyles.userpaint, true)

        '关联事件委托

        addhandler me.mousedown, addressof my_onmousedown

        addhandler me.mouseup, addressof my_onmouseup

        addhandler me.mouseenter, addressof my_onmouseenter

      

        addhandler me.mouseleave, addressof my_onmouseleave

        height = 23        

width = 75

    end sub

  

    protected overrides sub onpaint(byval pevent as system.windows.forms.painteventargs)

        'pevent.cliprectangle指在其中绘制的矩形,即使用父控件的背景色来画这个矩形按钮

        pevent.graphics.fillrectangle(new solidbrush(me.parent.backcolor), pevent.cliprectangle)

        if (enabled = false) then

            '画不可用状态

            drawdisablebutton(pevent.graphics)

        elseif (my_mousedown) then '画鼠标按下状态

            drawmousedownbutton(pevent.graphics)

        elseif (my_mousehover) then '画鼠标移动到其上状态

            drawmousehoverbutton(pevent.graphics)

        elseif (focused) then '有焦点,但鼠标未移动到其上

            drawcontainfocusbutton(pevent.graphics)

        else '一般情况下

            drawnormalbutton(pevent.graphics)

        end if

        '写文本

        writetext(pevent.graphics)

    end sub

    '鼠标按下的状态处理

    private sub my_onmousedown(byval sender as object, byval e as mouseeventargs)

        my_mousedown = true '鼠标按下

    end sub

    '鼠标松开状态的处理

    private sub my_onmouseup(byval sender as object, byval e as mouseeventargs)

        my_mousedown = false '鼠标松开

        '重新绘制控件时发生 paint 事件。painteventargs 指定绘制控件所用的 graphics

        '以及绘制控件所在的 cliprectangle。

        dim pe as painteventargs = new painteventargs(creategraphics(), clientrectangle)

        onpaint(pe)

    end sub

    '鼠标进入

    private sub my_onmouseenter(byval sender as object, byval e as eventargs)

        my_mousehover = true '鼠标移动到其上

        '

        dim pe as painteventargs = new painteventargs(creategraphics(), clientrectangle)

        onpaint(pe)

    end sub

    '鼠标移动开

    private sub my_onmouseleave(byval sender as object, byval e as eventargs)

        my_mousehover = false '鼠标移动开

        '

        dim pe as painteventargs = new painteventargs(creategraphics(), clientrectangle)

        onpaint(pe)

    end sub

  

    private sub drawborder(byval g as graphics, byval state as integer)

        if (state = 1) then '绘制一般边框

            '绘制一个画笔,高光点,宽度2

            dim p as pen = new pen(systemcolors.controllightlight, 2)

            'g.drawline画线,p是画笔,后面是第一个点的坐标,第二个点的坐标

            g.drawline(p, 1, 1, 1, height - 2) '绘制左侧竖线

            g.drawline(p, 1, 1, width - 2, 1) '绘制上面横线

            g.drawline(p, width - 1, 2, width - 1, height - 2) '绘制右侧竖线,由于已经在上面绘制了横线(纵坐标为1),所以从2开始

            g.drawline(p, 2, height - 1, width - 2, height - 1) '绘制下面横线

        elseif (state = 2) then '绘制移动到其上的边框

            '与一般边框用高光区别的是显示黄色

            dim p as pen = new pen(color.yellow, 2)

            g.drawline(p, 1, 1, 1, height - 2)

            g.drawline(p, 1, 1, width - 2, 1)

            g.drawline(p, width - 1, 2, width - 1, height - 2)

            g.drawline(p, 2, height - 1, width - 2, height - 1)

  

        elseif (state = 3) then '绘制按下的显示边框

            '与一般边框用高光区别的是显示暗褐色

            dim p as pen = new pen(systemcolors.controldark, 2)

            g.drawline(p, 1, 1, 1, height - 2)

            g.drawline(p, 1, 1, width - 2, 1)

            g.drawline(p, width - 1, 2, width - 1, height - 2)

            g.drawline(p, 2, height - 1, width - 2, height - 1)

  

        elseif (state = 4) then '绘制不可用状态边框

            '与一般边框用高光区别的是显示亮色

            dim p as pen = new pen(systemcolors.controllight, 2)

            g.drawline(p, 1, 1, 1, height - 2)

            g.drawline(p, 1, 1, width - 2, 1)

            g.drawline(p, width - 1, 2, width - 1, height - 2)

            g.drawline(p, 2, height - 1, width - 2, height - 1)

  

        elseif (state = 5) then '绘制有焦点但鼠标不在其上的状态

            '与一般边框用高光区别的是显示兰色

            dim p as pen = new pen(color.skyblue, 2)

            g.drawline(p, 1, 1, 1, height - 2)

            g.drawline(p, 1, 1, width - 2, 1)

            g.drawline(p, width - 1, 2, width - 1, height - 2)

            g.drawline(p, 2, height - 1, width - 2, height - 1)

        end if

        '//做完如上的处理后再对可用和不可用做圆化边缘处理(也就是把按钮的4个角进行圆化处理)

        if (state = 4) then '不可用时

            '使用画笔,color.fromargb(161, 161, 146)是从一个32位的argb值创建系统颜色,宽度为1

            dim p as pen = new pen(color.fromargb(161, 161, 146), 1)

            g.drawline(p, 0, 2, 0, height - 3) '左侧竖线(除了两个边角剩下的线)

            g.drawline(p, 2, 0, width - 3, 0) '上面横线(除了两个边角剩下的线)

            g.drawline(p, width - 1, 2, width - 1, height - 3) '右侧竖线(除了两个边角剩下的线)

            g.drawline(p, 2, height - 1, width - 3, height - 1) '下面的横线

            g.drawline(p, 0, 2, 2, 0) '左上角

            g.drawline(p, 0, height - 3, 2, height - 1) '左下角

            g.drawline(p, width - 3, 0, width - 1, 2) '右上角

            g.drawline(p, width - 3, height - 1, width - 1, height - 3) '右下角

  

        else 'draw normal style border

            '采用默认的黑色进行绘制边角

            g.drawline(pens.black, 0, 2, 0, height - 3)

            g.drawline(pens.black, 2, 0, width - 3, 0)

            g.drawline(pens.black, width - 1, 2, width - 1, height - 3)

            g.drawline(pens.black, 2, height - 1, width - 3, height - 1)

            g.drawline(pens.black, 0, 2, 2, 0)

            g.drawline(pens.black, 0, height - 3, 2, height - 1)

            g.drawline(pens.black, width - 3, 0, width - 1, 2)

            g.drawline(pens.black, width - 3, height - 1, width - 1, height - 3)

        end if

  

  

    end sub

    '一般状态

    private sub drawnormalbutton(byval g as graphics)

        '绘制边框,宽度为1

        drawborder(g, 1)

        '绘制背景,用高光点颜色

        paintback(g, systemcolors.controllightlight)

    end sub

    '鼠标移动到其上的状态

    private sub drawmousehoverbutton(byval g as graphics)

        drawborder(g, 2)

        paintback(g, systemcolors.controllightlight)

    end sub

  

    private sub drawmousedownbutton(byval g as graphics)

        drawborder(g, 3)

        '绘制背景,用三维元素的亮色

        paintback(g, systemcolors.controllight)

    end sub

  

    private sub drawdisablebutton(byval g as graphics)

        drawborder(g, 4)

        '亮色

        paintback(g, systemcolors.controllight)

    end sub

  

    private sub drawcontainfocusbutton(byval g as graphics)

        drawborder(g, 5)

        '高光点

        paintback(g, systemcolors.controllightlight)

    end sub

  

    '绘制背景色

    private sub paintback(byval g as graphics, byval c as color)

        '填充时采用:单色画刷,相对与(0,0)坐标(3,3)的位置,大小为宽-6,高-6

        g.fillrectangle(new solidbrush(c), 3, 3, width - 6, height - 6)

    end sub

    '写文本

    private sub writetext(byval g as graphics)

        '计算文本的位置

        dim x as integer = 0

        dim y as integer = 0

        'size用宽高有序对表示矩形区域

        dim s as size = g.measurestring(text, font).tosize()

        x = (width - s.width) / 2 '文字相对控件x偏移

        y = (height - s.height) / 2 '文字相对控件y偏移

        '写文本

        if (enabled) then '如果控件可用,则黑色文字

            'g.drawstring(text, font, brushes.black, x, y)

            dim b as new solidbrush(m_textcolor)

            g.drawstring(text, font, b, x, y)

        else '如果控件不可用,则灰色文字

            g.drawstring(text, font, brushes.gray, x, y)

        end if

    end sub

  

end class

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