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

首頁 > 編程 > .NET > 正文

從頭創建 Visual Basic .NET 控件 (五)

2024-07-10 13:04:02
字體:
來源:轉載
供稿:網友
第 4 步:繪制控件的外觀
要使控件具有一個可視的外觀,我們需要在 paint 事件中放置邏輯。然后,每次控件需要刷新其可視外觀時,就會運行該邏輯。

windows 窗體中的 paint 邏輯使用 .net 中 gdi+ 部分中的類。這些類基本上包括了 windows api 圖形功能。由于適合 .net,所以比 api 更易于使用。但是,有關它們的工作原理,需要理解以下幾點。

在 windows api 中,圖形操作需要一個窗口句柄,有時稱為 hwnd。在 gdi+ 中,它由 graphics 對象取代,該對象不僅代表了繪圖區域,還提供在該區域執行的操作(方法)。

例如,graphics 對象具有以下方法,可用來繪制各種屏幕元素:

drawcurve
drawellipse
drawline
drawpolygon
drawrectangle
drawstring
fillellipse
fillpolygon
這些都是很容易理解的,只是可用方法的示例。一些更復雜的方法還允許旋轉對象。我們將使用 drawrectangle 方法繪制邊框,使用 fillellipse 方法繪制彩色的圓。

大多數繪圖方法都要求使用 pen 或 brush 對象。pen 對象用于繪制直線并確定直線的顏色和粗細。brush 對象用于填充區域、確定填充區域所使用的顏色,以及一些特殊效果(例如,用位圖填充區域)。我們將使用特殊的 brush 效果使當前沒有亮起的燈的顏色變暗。

下面是處理控件的 paint 事件的代碼:

protected overrides sub onpaint(byval pe as _
                        system.windows.forms.painteventargs)
    mybase.onpaint(pe)

    dim grfgraphics as system.drawing.graphics
    grfgraphics = pe.graphics

    ' 首先繪制三個代表燈的圓。
    ' 一個亮起,其余兩個熄滅。
    drawlight(trafficlightstatus.statusgreen, grfgraphics)
    drawlight(trafficlightstatus.statusyellow, grfgraphics)
    drawlight(trafficlightstatus.statusred, grfgraphics)

    ' 現在繪制紅綠燈周圍的輪廓
    ' 用畫筆繪制輪廓,將它涂成黑色。
    dim pendrawingpen as new _
        system.drawing.pen(system.drawing.color.black, msngborderwidth)

    ' 在控件上繪制紅綠燈的輪廓。
    ' 首先定義要繪制的矩形。
    dim rectborder as system.drawing.rectangle

    rectborder.x = 1
    rectborder.y = 1
    rectborder.height = me.height - 2
    rectborder.width = me.width - 2
    grfgraphics.drawrectangle(pendrawingpen, rectborder)

    ' 釋放圖形對象
    pendrawingpen.dispose()
    grfgraphics.dispose()

end sub

首先使用基類繪制,它通常使用控件的背景顏色繪制背景。然后,從事件參數中獲取控件的 graphics 對象。

接下來,用一個函數畫出三個圓。有關該函數的內容稍后介紹。請注意,我們必須向該函數傳遞一個 graphics 對象的引用,同時還要指示要畫的圓(紅、黃、綠)。

然后是繪制輪廓的代碼。聲明一個具有適當位置和大小的矩形,然后傳遞給 graphics 對象的 drawrectangle 方法。

最后,圖形對象激活其 dispose 方法。使用 gdi+ 時,最好在完成圖形對象后立即釋放它們。這有助于清除操作系統繪圖時所用的資源。如果要在 windows® 98 或 windows me 中使用控件,管理圖形資源就更加重要,因為這些操作系統處理這種資源的能力較差。

下面是繪制圓的函數:

private sub drawlight(byval lighttodraw as trafficlightstatus, _
                      byval grfgraphics as graphics)

    dim ncirclex as integer
    dim ncircley as integer
    dim ncirclediameter as integer
    dim ncirclecolor as color

    ' 找到所有圓的 x 坐標和直徑
    ncirclex = cint(me.size.width * 0.02)
    ncirclediameter = cint(me.size.width * 0.96)
    select case lighttodraw
        case trafficlightstatus.statusred
            if lighttodraw = me.status then
                ncirclecolor = color.orangered
            else
                ncirclecolor = color.maroon
            end if
            ncircley = cint(me.size.height * 0.01)
        case trafficlightstatus.statusyellow
            if lighttodraw = me.status then
                ncirclecolor = color.yellow
            else
                ncirclecolor = color.tan
            end if
            ncircley = cint(me.size.height * 0.34)
        case trafficlightstatus.statusgreen
            if lighttodraw = me.status then
                ncirclecolor = color.limegreen
            else
                ncirclecolor = color.forestgreen
            end if
            ncircley = cint(me.size.height * 0.67)

    end select
    dim bshbrush as system.drawing.brush
    if lighttodraw = me.status then

        bshbrush = new solidbrush(ncirclecolor)
    else
        bshbrush = new solidbrush(color.fromargb(60, ncirclecolor))
    end if

    ' 繪制代表紅綠燈的圓
    grfgraphics.fillellipse(bshbrush, ncirclex, ncircley, ncirclediameter, ncirclediameter)

    ' 釋放筆刷
    bshbrush.dispose()

end sub

這是整個控件中唯一的一個復雜圖形。在 gdi+ 中,在要繪制橢圓的矩形中指定左上角的 x 坐標和 y 坐標,然后指定矩形的高度和寬度即可繪制一個橢圓。我們分別將 x 坐標和 y 坐標稱為 ncirclex 和 ncircley。因為我們要繪制一個圓,因此矩形的高度等于寬度,用變量 ncirclediameter 來控制該值。

將 ncirclex 設置為剛好放到控件內(控件的寬度乘以 0.02)。ncircley 取決于要繪制哪個燈,可以設置成靠近控件的頂部(紅燈)、大約向下三分之一(黃燈)或大約向下三分之二(綠燈)。直徑 ncirclediameter 設置為等于控件寬度的 96%。

要繪制實心橢圓,還需完成一件事,即確定要使用的顏色。顏色取決于正在繪制哪個燈以及正在繪制的燈是否亮起。亮起的燈的顏色要比熄滅的燈的顏色亮。

創建繪圖要使用的筆刷時需要使用這些顏色。如果正在繪制的燈是亮起的,即使用該顏色。如果繪制的燈是熄滅的,則要使用不同的方法實例化筆刷。下面是熄滅的燈所使用筆刷的代碼行:

bshbrush = new solidbrush(color.fromargb(60, ncirclecolor))

這并不是 .net 中較好的方法名,但 fromargb 方法的作用是創建筆刷,并通過將筆刷與背景顏色相結合來淡化顏色。第一個參數使用的數字介于 0 至 255 之間,數字越小,背景顏色滲透越深。我們使用的值為 60,它將大大降低處于熄滅狀態的燈的顏色。您可以嘗試對該參數使用不同的值(或將它設置成可設置屬性),以獲得不同的效果。

最后,graphics 對象的 drawellipse 方法繪制出該圓,函數結束。記住,該函數需要調用三次以繪制三個不同的圓。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 望谟县| 新绛县| 吴川市| 修武县| 济南市| 定陶县| 南昌市| 富平县| 闸北区| 泸溪县| 个旧市| 七台河市| 介休市| 呼和浩特市| 嘉鱼县| 什邡市| 察雅县| 安化县| 内黄县| 巢湖市| 北京市| 乐东| 云林县| 高清| 名山县| 泰宁县| 彭水| 慈溪市| 涪陵区| 准格尔旗| 永州市| 贵港市| 楚雄市| 遵义县| 久治县| 海口市| 涞水县| 清涧县| 三亚市| 兴海县| 双鸭山市|