我在表单floorPlanImage
上有一个VB.Net PictureBox form1
。
我将图片加载到图片框中:
floorPlanImage.image = my.resources.ResourceManager.GetObject("level8") 'this is actually dynamic, and this part works
我正在尝试创建叠加层以突出显示图像的某个区域:
Public Sub highlightPrintArea(ByVal x1 As Integer, ByVal y1 As Integer, ByVal x2 As Integer, ByVal y2 As Integer)
'**** DOES NOT WORK
Dim g As Graphics = Me.CreateGraphics
Dim r As Rectangle = New Rectangle(x1, y1, x2 - x1, y2 - y1) 'these are args passed in to the function
Dim pen As Pen = New Pen(Color.FromArgb(128, 32, 100, 200), 1) 'semi-transparent
Dim b As Brush = New SolidBrush(pen.Color)
g.FillRectangle(b, r)
end sub
我需要在运行时动态执行此操作,例如,按下按钮。上面的函数似乎没有绘制矩形。
但是,如果我有一个Handles floorPlanImage.Paint
之类的函数,那么就像我期望的那样绘制矩形:
Private Sub floorPlanImage_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles floorPlanImage.Paint
'**** Works, but does not suit my workflow
Dim g As Graphics = e.Graphics
Dim r As Rectangle = New Rectangle(100, 100, 100, 100)
Dim pen As Pen = New Pen(Color.FromArgb(128, 32, 100, 200), 1)
Dim b As Brush = New SolidBrush(pen.Color)
g.FillRectangle(b, r)
End Sub
问题(最后)
如何修改我的onclick功能以正确覆盖我的PictureBox上的矩形?
答案 0 :(得分:4)
在onclick事件中,您需要将位置/点保存到成员变量并设置一个标志,以便app知道您已保存位置。要更新图片框,请调用Invalidate and Update。
floorPlanImage.Invalidate()
floorPlanImage.Update()
在onpaint事件测试中,您有一个点的标志然后使用保存的点来绘制叠加层。
Private Sub floorPlanImage_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles floorPlanImage.Paint
If hasPoint
'Draw with saved point
End If
End Sub