You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

像素画工具开发求助:实现网格单元格点击上色功能

嘿,你的思路完全没问题——用Pixel类来封装每个单元格的状态确实是实现点击上色的最优解之一,比单纯画网格线要灵活得多。我来给你一步步拆解可行的实现方案:

第一步:创建Pixel类

这个类要封装每个像素单元格的位置、大小、颜色,还要提供绘制自身和判断点击范围的方法:

Public Class Pixel
    ' 单元格的位置与尺寸
    Public Property X As Integer
    Public Property Y As Integer
    Public Property Width As Integer
    Public Property Height As Integer
    ' 单元格的填充颜色
    Public Property Color As Color

    ' 构造函数,初始化单元格的基础属性
    Public Sub New(x As Integer, y As Integer, width As Integer, height As Integer)
        Me.X = x
        Me.Y = y
        Me.Width = width
        Me.Height = height
        Me.Color = Color.White ' 默认白色背景
    End Sub

    ' 绘制单元格:先填充颜色,再画边框
    Public Sub Draw(g As Graphics)
        Using fillBrush As New SolidBrush(Me.Color)
            g.FillRectangle(fillBrush, Me.X, Me.Y, Me.Width, Me.Height)
        End Using
        g.DrawRectangle(Pens.Black, Me.X, Me.Y, Me.Width, Me.Height)
    End Sub

    ' 判断某个鼠标点是否落在当前单元格内
    Public Function Contains(point As Point) As Boolean
        Return point.X >= Me.X AndAlso point.X <= Me.X + Me.Width AndAlso
               point.Y >= Me.Y AndAlso point.Y <= Me.Y + Me.Height
    End Function
End Class
第二步:在窗体中管理Pixel集合

在你的窗体类里声明几个全局变量,用来存储所有单元格、基础配置:

' 存储所有像素单元格的集合
Private pixels As New List(Of Pixel)
' 单元格的尺寸(和你原来的64一致)
Private cellSize As Integer = 64
' 网格的起始位置(和你原来的(10,2)一致)
Private originX As Integer = 10
Private originY As Integer = 2
第三步:生成网格的方法

替换你原来的drawGrid,用这个方法根据用户指定的行列数生成所有Pixel对象:

Private Sub GenerateGrid(rows As Integer, columns As Integer)
    pixels.Clear() ' 清空旧网格
    ' 遍历行列创建每个Pixel
    For rowIndex As Integer = 0 To rows - 1
        For colIndex As Integer = 0 To columns - 1
            Dim cellX As Integer = originX + colIndex * cellSize
            Dim cellY As Integer = originY + rowIndex * cellSize
            pixels.Add(New Pixel(cellX, cellY, cellSize, cellSize))
        Next
    Next
    Panel1.Invalidate() ' 触发Panel重绘,显示新网格
End Sub
第四步:处理Panel的Paint事件

让Panel在重绘时自动绘制所有Pixel:

Private Sub Panel1_Paint(sender As Object, e As PaintEventArgs) Handles Panel1.Paint
    ' 遍历所有单元格,逐个绘制
    For Each pixel In pixels
        pixel.Draw(e.Graphics)
    Next
End Sub
第五步:实现点击上色功能

处理Panel的鼠标点击事件,找到被点击的单元格并修改其颜色:

Private Sub Panel1_MouseClick(sender As Object, e As MouseEventArgs) Handles Panel1.MouseClick
    ' 遍历所有单元格,找到被点击的那个
    For Each pixel In pixels
        If pixel.Contains(e.Location) Then
            ' 这里可以替换成用户选择的颜色(比如用ColorDialog)
            pixel.Color = Color.Red
            ' 只重绘被修改的单元格,提升性能
            Panel1.Invalidate(New Rectangle(pixel.X, pixel.Y, pixel.Width, pixel.Height))
            Exit For ' 找到目标后退出循环,避免不必要的遍历
        End If
    Next
End Sub
额外优化建议
  • 添加一个ColorDialog控件,让用户可以自由选择上色的颜色,替换示例中的Color.Red
  • 如果需要支持撤销功能,可以给Pixel类添加历史颜色记录,或者维护一个操作日志
  • 当用户修改行列数时,直接调用GenerateGrid(newRows, newColumns)即可刷新网格

内容的提问来源于stack exchange,提问作者Gerardo Sanchez

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 09:57:29