国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > 綜合 > 正文

自定義控件--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

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 上蔡县| 大连市| 晋中市| 稻城县| 兰西县| 宜阳县| 白银市| 油尖旺区| 任丘市| 盐源县| 垦利县| 习水县| 静乐县| 浮梁县| 宜章县| 南郑县| 廊坊市| 合作市| 绥阳县| 凤阳县| 赫章县| 绥芬河市| 天长市| 武穴市| 慈溪市| 灵台县| 德安县| 淮阳县| 水富县| 龙山县| 南京市| 焦作市| 宝清县| 崇信县| 射阳县| 曲周县| 嘉兴市| 嘉定区| 望谟县| 台中市| 乐平市|