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

首頁 > 開發 > 綜合 > 正文

一個使用用戶控件(包括組件)的演示

2024-07-21 02:24:13
字體:
來源:轉載
供稿:網友
  • 本文來源于網頁設計愛好者web開發社區http://www.html.org.cn收集整理,歡迎訪問。
  •  

    1.

    imports system.componentmodel
    public class pic
        inherits system.windows.forms.usercontrol

    #region " windows 窗體設計器生成的代碼 "

        'usercontrol1 重寫 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 窗體設計器修改此過程。
        '不要使用代碼編輯器修改它。
        <system.diagnostics.debuggerstepthrough()> private sub initializecomponent()
            '
            'pic
            '
            me.name = "pic"
            me.size = new system.drawing.size(48, 48)

        end sub

    #end region

        public const m_maxlen as integer = 48 '固定的寬和高
        public const m_maxheight as integer = 48
        public sub new(byval m as image) '主要是用于在piccontrols組件中創建實例時使用
            mybase.new()

            '該調用是 windows 窗體設計器所必需的。
            initializecomponent()

            '在 initializecomponent() 調用之后添加任何初始化
            m_image = m
        end sub
        public sub new()
            mybase.new()

            '該調用是 windows 窗體設計器所必需的。
            initializecomponent()

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

        end sub

        private m_image as image = image.fromfile("g:/練習/重要的例程/使用問題(在格子中顯示圖片)/gounda takeshi.ico")
        <category("grid"), description("設置卡片的圖片。")> _
        public property image() as image
            get
                return m_image
            end get
            set(byval value as image)
                m_image = value
                me.refresh()
            end set
        end property
        '繪制邊框和圖象
        protected overrides sub onpaint(byval e as system.windows.forms.painteventargs)
            dim g as graphics = me.creategraphics
            me.backcolor = color.white
            g.drawrectangle(system.drawing.pens.black, 0, 0, me.width - 1, me.height - 1)
            dim ic as image = ctype(m_image, image)
            g.drawimage(ic, 0, 0)
        end sub
        '不允許調整大小
        protected overrides sub onsizechanged(byval e as system.eventargs)
            me.size = new size(m_maxlen, m_maxheight)
        end sub
        '匹配否標志
        private m_double as boolean = false
        <category("grid"), description("是否匹配的標志。")> _
        public property doubles() as boolean
            get
                return m_double
            end get
            set(byval value as boolean)
                m_double = value
            end set
        end property
        private m_id as integer
        <category("grid"), description("區分是否來自同一圖片的標志。")> _
        public property id() as integer
            get
                return m_id
            end get
            set(byval value as integer)
                m_id = value
            end set
        end property
     
    end class

    2.

    imports my_namespace
    imports system.componentmodel
    public class piccontrols
        inherits system.componentmodel.component

    #region " 組件設計器生成的代碼 "

        '組件重寫 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

        '組件設計器所必需的
        private components as system.componentmodel.icontainer

        '注意:以下過程是組件設計器所必需的
        '可以使用組件設計器修改此過程。
        '不要使用代碼編輯器修改它。
        <system.diagnostics.debuggerstepthrough()> private sub initializecomponent()
            components = new system.componentmodel.container()
        end sub

    #end region
        public sub new(byval container as system.componentmodel.icontainer)
            myclass.new()

            'windows.forms 類撰寫設計器支持所必需的
            container.add(me)

            changepic() '如果選擇的圖片發生了變化,那么創建卡片集合也要相應的變化。

        end sub

        public sub new()
            mybase.new()

            '該調用是組件設計器所必需的。
            initializecomponent()

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

            changepic() '如果選擇的圖片發生了變化,那么創建卡片集合也要相應的變化。

        end sub
        '////////////////////////////////////////////////////////////////

        dim m_piccontrols as new system.collections.arraylist()
        '總數量
        <category("grid"), description("集合內卡片的總數。")> _
            public readonly property count() as integer
            get
                return m_piccontrols.count
            end get
        end property
        '指定位置的卡片(這個對于vb.net是比較特殊的,具有帶參數的屬性)
        default public readonly property items(byval index as integer) as pic
            get
                if index >= 0 and index < m_piccontrols.count then
                    return ctype(m_piccontrols(index), pic)
                end if
            end get
        end property

        public sub shuffle() ' 混卡片,也就是生成一組隨機的卡片集合。(這里的算法不錯!)
            dim r as new system.random()
            dim d as new system.collections.arraylist()
            dim p as pic
            while (m_piccontrols.count > 0)
                dim removeindex as integer = r.next(0, m_piccontrols.count - 1)
                p = ctype(m_piccontrols(removeindex), my_namespace.pic)
                m_piccontrols.removeat(removeindex)
                d.add(p)
            end while
            m_piccontrols = d
        end sub

        private m_image as imagelist
        <category("grid"), description("選擇相應的imagelist控件。")> _
        public property imagelist() as imagelist
            get
                return m_image
            end get
            set(byval value as imagelist)
                m_image = value
                changepic()
            end set
        end property
        '/////////
        '這個事件比較重要,主要是根據圖片的變動來生成不同的卡片集合。
        private sub changepic()
            if m_image is nothing then exit sub
            dim i as integer
            for i = 0 to m_piccontrols.count - 1
                ctype(m_piccontrols(i), pic).dispose() '注意這里。
            next
            m_piccontrols.clear()
            dim j as integer
            for i = 0 to m_image.images.count - 1
                for j = 0 to 3
                    dim p as new pic(m_image.images(i))
                    p.id = i
                    m_piccontrols.add(p)
                next
            next
        end sub
        '由于在排列好后,每個在集合中的卡片的doubles屬性都會被設置成true,
        '所以要在開始一次新的排序時設置所有的卡片該屬性為false
        public sub setfalse()
            dim i as integer
            for i = 0 to m_piccontrols.count - 1
                dim apic as pic = ctype(m_piccontrols(i), pic)
                apic.doubles = false
            next
        end sub

    end class

    3.

    imports system.componentmodel
    public class picshow
        inherits system.windows.forms.usercontrol

    #region " windows 窗體設計器生成的代碼 "

        public sub new()
            mybase.new()

            '該調用是 windows 窗體設計器所必需的。
            initializecomponent()

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

        end sub

        '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 窗體設計器修改此過程。
        '不要使用代碼編輯器修改它。
        <system.diagnostics.debuggerstepthrough()> private sub initializecomponent()
            components = new system.componentmodel.container()
        end sub

    #end region
        '//                                 這個程序的原理                                                                        //
        '//先做一個pic控件,可以為其設置相應的圖片,不允許改變大小,要重寫sizechange,onpait事件  //
        '//做一個集合組件piccontrols來容納一定數量的pic卡片,但并不顯示它,因為是組件。只是容器  //
        '//最后做一個picshow控件,用于顯示piccontrols.count數量的卡片集合。                      //
        '//比較重要的地方就是如何對卡片進行隨機混排(piccontrols的shuffle方法)和picshow控件的   //
        '//contrains,start方法。尤其注意這里進行排序的方法:是將卡片在集合里就弄混(隨機),這樣//
        '//我們取得的每個卡片都是隨機的了,然后在picshow控件里根據每個卡片的doubles,id屬性來進行 //
        '//排序,把隨機和排序分開了。當然也可以把他們合并寫到picshow控件里。不過這里不建議這樣。 //
        '//因為對于piccontrols組件來說,它的集合就是一個隨機產生的卡片集合。這樣比較好理解。     //

        private const m_spacing as integer = 10 '間隔設置的常量

        private m_rows as integer = 2 ' 對于一個陣列來講,2行應該更有意義。
        <category("grid"), description("矩陣的行。"), defaultvalue(2)> _
            public property row() as integer
            get
                return m_rows
            end get
            set(byval value as integer)
                if value > 0 then
                    m_rows = value
                    me.refresh()
                end if
            end set
        end property
        private m_columns as integer = 2
        <category("grid"), description("矩陣的列。"), defaultvalue(2)> _
        public property columns() as integer
            get
                return m_columns
            end get
            set(byval value as integer)
                if (value > 0) and (value mod 2 = 0) then
                    m_columns = value
                    me.refresh()
                else
                    throw new exception("不是有效的列值!請輸入2的倍數的列值。")
                end if
            end set
        end property
        private m_collection as piccontrols
        <category("grid"), description("指定相應的piccontrols組件。")> _
        public property getcontrols() as piccontrols
            get
                return m_collection
            end get
            set(byval value as piccontrols)
                m_collection = value
            end set
        end property
        '繪制邊框,由于還沒有將卡片加入到me.controls集合,所以只有邊框。
        protected overrides sub onpaint(byval e as system.windows.forms.painteventargs)
            dim height as integer = my_namespace.pic.m_maxheight
            dim width as integer = my_namespace.pic.m_maxlen
            me.width = (width + m_spacing) * m_columns + m_spacing
            me.height = (height + m_spacing) * m_rows + m_spacing
            dim g as graphics = me.creategraphics
            dim row, column as integer
            for row = 0 to m_rows - 1
                for column = 0 to m_columns - 1
                    g.drawrectangle(system.drawing.pens.gray, column * (width + m_spacing) + m_spacing, _
                    row * (height + m_spacing) + m_spacing, width, height)
                next
            next
        end sub

        private m_double as pic '記錄相同的那個卡片

        private m_last as integer  '記錄格子中的最后一個卡片

        '開始排列
        public sub start()

            me.controls.clear() '先清空容器

            if not isnothing(m_collection) then '判斷行列之積和卡片數量是否相等
                if (m_collection.count <> m_rows * m_columns) then
                    throw new exception("卡片數量為:" & cstr(m_collection.count) & "格子數量為:" & cstr(m_rows * m_columns))
                end if

                '///////////////////////////////////

                m_last = -2 '初始化,因為從0開始是第一個格子,所以初始值為-2

                m_collection.setfalse() '因為開始一次排序就會把所有的卡片pic的double屬性全都設置為true。所以,這里要全都設置回false

                m_collection.shuffle() '將卡片弄混

                dim cardcount as integer = 0 '卡片指針
                dim row, column as integer

                for row = 0 to m_rows - 1
                    for column = 0 to m_columns - 1
                        dim apic as pic = ctype(m_collection(cardcount), pic)
                        '加入到me的控件集合
                        me.controls.add(apic)
                        '控件集合中的原有卡片進行遍歷,看是否有單個的與新加入的同一個圖片的卡片
                        dim rint as integer = contrains(apic)

                        select case rint
                            case 0 '匹配排列
                                apic.left = m_double.left + m_spacing + my_namespace.pic.m_maxlen
                                apic.top = m_double.top
                            case 1 '沒有匹配項,間隔排列

                                '行,注意一定要使用int進行轉化,否則會四舍五入。
                                dim r as integer = int(m_last / (m_rows))
                                '列
                                dim c as integer = m_last mod (m_rows)
                                '取得行列后就可以直接設置位置了。
                                apic.left = c * (pic.m_maxlen + m_spacing) + m_spacing
                                apic.top = r * (pic.m_maxheight + m_spacing) + m_spacing

                        end select
                        cardcount += 1 '下一個卡片
                    next
                next
                messagebox.show("排序完成!")

            end if
        end sub
        '排序的函數
        public function contrains(byval p as pic) as integer
            m_double = nothing '初始值為空,每次排序前要設置為空。
            dim apic as pic
            dim i as integer
            dim count as integer = me.controls.count - 1
            for i = 0 to count - 1 '從0到末尾-1,把自己排除掉,自己和自己不必要去比較
                apic = ctype(me.controls(i), pic)
                if (apic.id = p.id) and apic.doubles = false then  'and i <> count
                    apic.doubles = true '匹配
                    p.doubles = true '匹配
                    m_double = apic
                end if
            next
            '找到匹配的了
            if not (m_double is nothing) then
                return 0
            else
                m_last += 2 '分隔開一個格子( 這里的格子開始為0,最后為m_collection的count-1)
                return 1
            end if

        end function
    end class


    發表評論 共有條評論
    用戶名: 密碼:
    驗證碼: 匿名發表
    主站蜘蛛池模板: 峨山| 鸡东县| 宁都县| 洛阳市| 吉木萨尔县| 涪陵区| 策勒县| 繁昌县| 余江县| 榆林市| 巴东县| 建湖县| 赤峰市| 共和县| 商城县| 兴城市| 陆丰市| 齐齐哈尔市| 遂宁市| 清原| 明光市| 惠州市| 建瓯市| 泗阳县| 宁海县| 永新县| 右玉县| 察哈| 班戈县| 武山县| 将乐县| 酒泉市| 泌阳县| 凤阳县| 即墨市| 象山县| 东乡族自治县| 惠州市| 延津县| 玉龙| 泰兴市|