图形网格组件






4.70/5 (19投票s)
2004 年 1 月 14 日
4分钟阅读

176534

3220
图形网格的易于操作
引言
GraphicGrid 是一个 .NET 应用程序控件,可简化图形网格的manipulation。GraphicGrid 可用于广泛的任务,包括图形应用程序和游戏等。
GraphicGrid 将其工作区划分为单元格,允许您控制要在网格的各个单元格中放置的内容。您可以填充部分或全部单元格,使其包含图片、颜色,甚至图形文本,而其余单元格保持空白。GraphicGrid 还允许您将图像加载到其工作区的背景中,从而可以创建真正有趣且引人注目的效果。
本次发布
我包含了五个供您下载的文件
- GraphicGrid_DLL.zip
这是 GraphicGrid DLL 的源代码和已编译的版本。
- GraphicGrid_Game.zip
GraphicGrid_GameSrc.zip
一个非常简单的游戏,您需要点击屏幕上出现的兔子。非常类似于街机游戏“打地鼠”。
- GraphicGrid_Sample.zip
GraphicGrid_SampleSrc.zip
一个演示如何使用 GraphicGrid 的示例。
将 GraphicGrid 添加到您的工具箱
我还没有为该控件制作安装程序或酷炫的图标,所以您需要这样做。
- 将 GraphicGrid_DLL.zip 解压到 Visual Studio Projects 下的一个目录中。
- 创建一个新项目并进入设计视图。
- 查看您的工具箱,右键单击您想放置 GraphicGrid 的选项卡的名称,然后选择“添加/删除项...”
- 单击浏览按钮,然后从您解压它的目录中选择 GraphicGrid.DLL。
- 单击“确定”,您将在工具箱中看到 GraphicGrid 工具。
特殊属性
ShowGrid |
如果希望网格显示在控件的工作区中,此属性为 true 或 false。 示例: object.ShowGrid = True |
---|---|
GridColor |
您希望网格呈现的颜色。 示例: object.GridColor = color.red |
格子 |
您想要的网格数量(行 x 列)。 示例: object.Cells = new size(10, 10) |
特殊方法
setCell |
定义: 定义: 定义: 定义: |
---|---|
removeCell |
定义: 定义: |
clearCells |
定义: clearCells() 示例: object.clearCells() 注意: 这会从网格中移除所有单元格。 |
特殊事件
gridClick |
当您单击网格中的某个位置时会触发此事件。 返回值:Object 和 GridPosition |
---|---|
gridDoubleClick |
当您双击网格中的某个位置时会触发此事件。 返回值:Object 和 GridPosition |
gridMouseMove |
当您将鼠标移动到网格中的某个位置时会触发此事件。 返回值:Object 和 GridPosition |
gridMouseUp |
当您单击网格中的某个位置后会触发此事件。 返回值:Object,MouseEventArgs 和 GridPosition |
gridMouseDown |
当您单击网格中的某个位置后会触发此事件。 返回值:Object,MouseEventArgs 和 GridPosition |
GridPosition
始终以 Point 的形式表示。假设您的网格是 10x10,用户单击了第 5 行的第 5 个单元格。在这些特殊事件中的任何一个中,gridPosition 的值都将是 point(5, 5)。
详细的注释也可以在 DLL 源代码文件中找到。
示例应用
我们首先将 panel 控件和 GraphicGrid
控件拖到窗体上,并将它们设置为停靠。在 panel 中添加几个按钮,并让它们执行以下操作。
Private Sub allButtons(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles Button1.Click, Button2.Click, Button3.Click, _
Button4.Click, Button5.Click, Button6.Click
' This is just a big array of points
' (Used in create onscreen cells)
'
Dim points() As Point = { _
New Point(0, 0), New Point(0, 1), New Point(0, 2), New Point(0, 3), _
New Point(1, 0), New Point(2, 1), New Point(3, 0), _
New Point(4, 0), New Point(4, 1), New Point(4, 2), New Point(4, 3)}
Select Case sender.name
Case "Button1"
' Create On Screen Cells
'
Graphicgrid1.setCell(points, Color.Red)
' Off screen (Use Resize button to see them)
'
Graphicgrid1.setCell(New Point(9, 15), Color.White)
Graphicgrid1.setCell(New Point(10, 15), Color.Green)
Graphicgrid1.setCell(New Point(11, 15), Color.Yellow)
Graphicgrid1.setCell(New Point(12, 15), Color.Gold)
Case "Button2"
' Toggle the grid
'
Graphicgrid1.ShowGrid = Not Graphicgrid1.ShowGrid
Case "Button3"
' Toggle Big or small size
'
If Graphicgrid1.Cells.Equals(New Size(20, 20)) Then
Graphicgrid1.Cells = New Size(10, 10)
Else
Graphicgrid1.Cells = New Size(20, 20)
End If
Case "Button4"
' Remove a cell
'
Graphicgrid1.removeCell(New Point(2, 1))
Case "Button5"
' Clear all the cells
'
Graphicgrid1.clearCells()
End Select
End Sub
就是这样,没有什么别的可做的了。这是一个简单、简单、简单的应用程序。我添加了另一个按钮“Button6”,它在 GraphicGrid_Sample.zip 的代码中。它只是一个灯光秀,单元格以随机顺序打开和关闭,并改变颜色。
示例游戏
“打地鼠”(点击兔子),但要小心,随着时间的推移,它的速度会越来越快!这是一个我为了演示控件而制作的非常简单的游戏。代码非常简单。
' A few bars to keep track of things durring the game
'
Public Cell As Point ' Last location where we put the cell
Public Gothim = False ' Did we click of the bunny?
' This is the start and stop button on the form
'
Private Sub startStop_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles startStop.Click
' Start a nice slow game
'
Timer.Interval = 2000
If Not Timer.Enabled Then
' Reset and start the game
'
BunniesMissed.Text = -1
BunniesHit.Text = 0
Timer.Start()
Else
' Stop the game
'
Timer.Stop()
End If
End Sub
' Find a new cell for the bunny and put him in it.
'
Private Sub redraw()
Dim rnd As New Random
Dim newPoint As Point
' Reset inGame vars
'
Gothim = False
' Generate a new point but make sure it's not the same as the old
' point
'
Do
newPoint = New Point(rnd.Next(0, 4), rnd.Next(0, 2))
Loop While newPoint.Equals(Cell)
' Draw the cell
'
Cell = newPoint
Graphicgrid1.clearCells()
Graphicgrid1.setCell(New Point(Cell.X, Cell.Y), Images.Images(0))
End Sub
' The timer went off, did we get the bunny?
'
Private Sub Timer_Tick(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles Timer.Tick
If Not Gothim Then BunniesMissed.Text += 1
If BunniesMissed.Text = 10 Then
Timer.Stop()
MsgBox("You stink!", MsgBoxStyle.Exclamation, "Bunnie Hunter")
Else
redraw()
End If
End Sub
' This is where the magic happens.
' They clicked the grid and fired off this event.
'
Private Sub Graphicgrid1_gridClick( _
ByVal sender As Object, ByVal GridPoint As _
System.Drawing.Point) Handles Graphicgrid1.gridClick
' Where they clicked, is this where the bunny is?
'
If GridPoint.Equals(Cell) Then ' Yup
' Change the bitmap to splater bunny
'
Graphicgrid1.removeCell(Cell)
Graphicgrid1.setCell(New Point(Cell.X, Cell.Y), _
Images.Images(1))
' Game stuff
'
Gothim = True
BunniesHit.Text += 1
Timer.Interval -= 25
Else ' Humm, the clicked but no bunny here
' Clicked on the wrong cell
'
BunniesMissed.Text += 1
End If
End Sub
待办事项
- 为 GraphicGrid 添加编辑功能(当您鼠标悬停在网格上时,它会高亮显示该单元格)。
- 增强拖放功能
总结
希望您喜欢使用此控件。如果您有任何想法或任何可以使其变得更好的建议,请告诉我。另外,如果您使用此控件创建了任何东西,我将非常感激在致谢中提及 :-)