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
源代码网推荐