for-loop - 矩形的自动对齐取决于vb.net中的纸张大小
问题描述
我有一个矩形,它添加另一个矩形取决于要打印多少邮票/收据。
我添加了另一个矩形,for loop
但如果我设法添加超过 3 个矩形,其他矩形将不会显示,因为它保持对齐,或者如果我使用if else
if e.pagebounds.widht <(我的矩形的最后一个宽度)它会下降并且它是很好,但是下一个怎么样...下一个...下一个第 3 行及以上我怎么才能知道这是我的代码到目前为止
Dim rec As Rectangle
Dim x1, y1 As Integer
Dim nextline As Integer
x1 = 40
y1 = 40
Dim b As Integer = 0
Dim containerrectangle As Rectangle
containerrectangle = New Rectangle(e.PageBounds.X, e.PageBounds.Y, e.PageBounds.Width, e.PageBounds.Height)
For i = 0 To 6
If e.PageBounds.Width - 100 < x1 + b Then
nextline = 40
'reset the x-axis of the rectangle
y1 = 250
b = 40
rec = New Rectangle(b, y1 + 50, 250, 250)
e.Graphics.DrawRectangle(Pens.Sienna, rec)
y1 += 250
b += 10
Else
rec = New Rectangle(x1 + b, 40, 250, 250)
e.Graphics.DrawRectangle(Pens.Sienna, rec)
x1 += 250
b += 10
End If
Next
解决方案
看看这段代码 - 我会说它更直接:
Dim RectTemplate As New Rectangle(0, 0, 250, 250)
Dim GapPx As Int32 = 12 ' set gap between rectangles
Dim StaPt As New Point(GapPx, GapPx * 2) ' set starting point
Dim N As Int32 = 15 ' set (or get) the totalnumber of rectangles
Dim nL As Int32 = Math.Floor((e.PageBounds.Width - GapPx) / (RectTemplate.Width + GapPx)) ' number of rectangles per row
Dim nR As Int32 = Math.Ceiling(N / nL) ' number of rows of rectangles
For ir = 0 To nR - 1 ' process all rows
For il = 0 To nL - 1 ' process all rectangles in a row
Dim rect As New Rectangle(StaPt.X + il * (RectTemplate.Width + GapPx), StaPt.Y + ir * (RectTemplate.Height + GapPx), RectTemplate.Width, RectTemplate.Height)
e.graphics.DrawRectangle(Pens.Sienna, rect)
Next
Next
推荐阅读
- docker - 不保留多 dockerfile 的权限
- flutter - 动态创建颤振滑块
- azure-devops - 如何过滤掉 Power BI 中另一个表中已经存在的表中的行?
- flutter - 如何在颤动中使用 list.view builder 构建嵌套扩展图块?
- c# - C# LiteDB LiteCollection.FindOne 返回错误值
- c# - 如何通过 API 将文件上传到 Azure DataLake?
- python - 如何解决python中的index not found错误
- javascript - email-templates 图片在预览中显示,但不在 Gmail 中
- sendgrid-templates - SendGrid 动态模板
- networking - 如何验证给定子网掩码范围内的ip?