日期:2014-05-18  浏览次数:21000 次

请教一个C#画图程序
我有一张图片,里面有很多格子,格子里面有很多数字,
我从外面点击一个数字,对应格子里的数字,怎么使格子变成红色

------解决方案--------------------
我写俄罗斯方块用的底层,是画方格,格里画图片,和你需求很像,你可以参考下
VB.NET code
Public Class pic
    Inherits PictureBox
    Public Sub New(ByVal x As Integer, ByVal y As Integer, ByVal sif As Integer)
        MyBase.New()
        Me.cols = x
        Me.rows = y
        Me.sizf = sif
        Me.Width = Me.cols * Me.sizf + 1
        Me.Height = Me.rows * Me.sizf + 1
        ReDim data(x - 1, y - 1)
    End Sub

    Protected Overrides Sub OnPaint(ByVal E As PaintEventArgs)
        Dim i, j
        Dim pen1 As New Pen(Color.Black, 1)
        pen1.DashStyle = Drawing2D.DashStyle.Dot
        For i = 0 To Me.cols - 1
            For j = unvisiblerow To Me.rows - 1
                DrawIcon(E.Graphics, images(data(i, j)), i * sizf, j * sizf)
                E.Graphics.DrawLine(pen1, i * sizf, j * sizf, i * sizf, (j + 1) * sizf)
                E.Graphics.DrawLine(pen1, i * sizf, j * sizf, (i + 1) * sizf, j * sizf)
                E.Graphics.DrawLine(pen1, (i + 1) * sizf, j * sizf, (i + 1) * sizf, (j + 1) * sizf)
                E.Graphics.DrawLine(pen1, i * sizf, (j + 1) * sizf, (i + 1) * sizf, (j + 1) * sizf)
            Next
        Next
        pen1.DashStyle = Drawing2D.DashStyle.Solid
        E.Graphics.DrawLine(pen1, 0, unvisiblerow * Me.sizf, Me.Width - 1, unvisiblerow * Me.sizf)
        E.Graphics.DrawLine(pen1, Me.Width - 1, unvisiblerow * Me.sizf, Me.Width - 1, Me.Height - 1)
        E.Graphics.DrawLine(pen1, 0, Me.Height - 1, Me.Width - 1, Me.Height - 1)
        E.Graphics.DrawLine(pen1, 0, unvisiblerow * Me.sizf, 0, Me.Height - 1)
    End Sub

    Public Sub DrawIcon(ByVal S_Graphics As Graphics, ByVal NodeICON As Image, ByVal x As Single, ByVal y As Single)
        Dim destRect As Rectangle
        If Not NodeICON Is Nothing Then
            destRect = New Rectangle(x, y, sizf, sizf)
            S_Graphics.DrawImage(NodeICON, destRect)
        End If
    End Sub
    Public unvisiblerow As Integer = 0
    Dim sizf As Integer = 20
    Public images() As Image
    Private cols As Integer
    Private rows As Integer
    Public data(,) As Integer
End Class

------解决方案--------------------
如果格子比较规范
都是固定大小的
可能好办点

楼主可以参考以下:
1.单击后获取当前鼠标坐标
2.将当前坐标换算成界面的坐标
3.寻找本坐标内的格子(所以要求格子大小一定要相同)
4.将本格画上红色
------解决方案--------------------
VB.NET code
Public Class p
    Inherits PictureBox
    Public Sub New(ByVal x As Integer, ByVal y As Integer, ByVal sif As Integer)
        MyBase.New()
        nlist = New ArrayList
    End Sub

    Protected Overrides Sub OnPaint(ByVal E As PaintEventArgs)
        '画格子
        Dim pen1 As New Pen(Color.Black, 1)
        pen1.DashStyle = Drawing2D.DashStyle.Dot
        If not Me.cursorn Is Nothing Then E.Graphics.FillRectangle(Brushes.Red, Me.cursorn.left, Me.cursorn.top, Me.cursorn.width, Me.cursorn.height)
        Dim i As Integer
        For i = 0 To Me.nlist.Count - 1
            Dim n1 As n = CType(Me.nlist.Item(i), n)
            E.Graphics.DrawRectangle(pen1, n1.left, n1.top, n1.width, n1.height)

            E.Graphics.DrawString(n1.num, New Font("宋体", 9), New SolidBrush(Color.Black), n1.left, n1.top)
        Next

    End Sub

    Public nlist As ArrayList
    Public cursorn As n
End Class

------解决方案--------------------
如此说来你每个图片格子一定得表示有意义,在什么地方来个格子意义tag数组,不然图片怎么知道上面画的是什么,再比较一下外面点了什么,再匹配一下,对应格子变化,