如何使用 System.Drawing 绘制表格 [英] How to draw a table using System.Drawing
问题描述
我想使用 System.Drawings 绘制一个表格,然后用一些文本填充单元格.这段文字每隔几秒钟就会在不同的时刻发生变化.这是一个游戏,其中有一个网格,每隔几秒钟,随机单元格会显示一个数字一秒钟,然后用户必须在其下方的文本框中键入答案.此外,用户必须单击刚刚显示数字的单元格.我在窗口中使用图形的经验很少,因此将不胜感激任何帮助.
I would like to draw a table using System.Drawings, and then fill cells with some text. This text will change every few seconds, in various moments of time. It's a game, where there is a grid and every few seconds, random cell displays a number for a split of a second, then the user has to type the answer in the text box below it. Also, user has to click the cell, which has just showed the number. I have little experience in working with Graphics in window so any help will be greatly appreciated.
推荐答案
您是否考虑过使用 DataGridView 控件 代替?
Have you considered using the DataGridView Control instead?
如果您更喜欢使用更底层的方法,那么绘制表格并不难.细分x
和y
坐标得到绘制点(System.Drawing.Point
)
If you prefer to use a more low-level approach, drawing a table is not that hard. Subdivide the x
and y
coordinates to obtain points for drawing (System.Drawing.Point
)
使用笔 (System.Drawing.Pen
) 和两个点作为 Graphics.DrawLine
方法的参数绘制线条.您可以将数字放在单元格的中点,因为您已经知道用于绘制表格线的所有点(及其坐标).
Draw lines using a pen (System.Drawing.Pen
) and two points as arguments to Graphics.DrawLine
method. You can position your numbers at midpoint of cells because you already know all of the points (and their coordinates) used to draw table lines.
您也可以通过坐标确定点击了哪个单元格.
You can determine which cell was clicked also by coordinates.
这篇关于如何使用 System.Drawing 绘制表格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!