c# - 在固定的方形图片框中制作方形矩形,如网格
问题描述
我正在使用 c# 和 Visual Studio(.NET Framework) 开发一个项目,在该项目中,用户可以使用 PictureBox 与排列在网格中的一些方形框进行交互。默认大小为 20 x 20 用户可以更改 PictureBox 中的正方形数量,但 PictureBox 的大小应保持不变。我使用 WinForms。
我通过使用我的逻辑和一些互联网的代码尝试过的代码,我让它作为一个带有正方形的正方形网格工作,但是当我增加 xNum 或 yNum 时,网格开始变得越来越小。这是我执行矩形绘制的函数:
public Graphics CreateRectangles(int xNum, int yNum, Graphics g)
{
rectList.Clear();
int width = (pictureBox1.Image.Height / xNum) - 1;
int height = (pictureBox1.Image.Height / yNum) - 1;
int both = (width + height) / 2;
Rectangle rect = new Rectangle();
rect.Size = new Size(both, both);
for(int x = 0; x < xNum; x++)
{
rect.X = x * rect.Width;
for(int y = 0; y < yNum; y++)
{
rect.Y = y * rect.Height;
rectList.Add(rect);
}
}
pictureBox1.Refresh();
foreach (Rectangle rec in rectList)
{
Pen p = new Pen(Color.Blue);
g.DrawRectangle(p, rec);
}
g.DrawLine(new Pen(Color.Blue), 0, yNum * height, xNum * width, yNum * height);
return g;
}
每当我想绘制时,我都会清除屏幕
pictureBox1.Image = new Bitmap((Screen.PrimaryScreen.Bounds.Width / 8) * 7, (Screen.PrimaryScreen.Bounds.Height / 8) * 7);
请帮我解决这个问题,使 pictureBox1 中的正方形区域的大小保持最大,而不会与其他 GUI 元素重叠。提前致谢。
编辑:我知道如果我有相对大小,那么单元格大小会变小,但这是预期的行为,但我不希望表格的整个框架变小,就像我增加 xNum 时一样,外框/盒子的大小应保持不变
解决方案
推荐阅读
- python - 突出显示包含python中特定点的散点图的一部分
- html - 为什么一个按钮覆盖而另一个没有?
- c - 如何禁用有关在 GCC 中使用弃用获取的警告?
- asp.net - 解决 ASP.NET Core 3.1 API 中的模型绑定问题
- android - 想要删除firebase(Android)中实时数据库的多个(超过1000个节点)节点
- php - 如何仅在服务器端处理中显示某些行?
- java - 错误:包 org.apache.commons 不可访问
- flutter - 颤动如何强制字体大小日期时间选择器颤动?
- php - 使用 window.location.replace(response.path)
- javascript - 使用 javascript 在 HTML 表单中自动填写字段