如何在面板中添加矩形?

时间:2015-04-23 14:27:56

标签: vb.net graphics drawing

这是我用来创建面板和矩形的代码,但它不起作用:

Public ribbon_holder As New Panel
Public BluePen As New Pen(Main.mi_blue, 5)

With ribbon_holder
  .Parent = Main
  .Width = Main.Width
  .Height = 75
  .BackColor = Color.White
  .BringToFront()
End With

Dim myGraphics As Graphics = ribbon_holder.CreateGraphics
myGraphics.DrawRectangle(BluePen, 0, 0, 100, 50)

我只是想在我的面板中创建一个空矩形。 感谢。

1 个答案:

答案 0 :(得分:0)

您可以使用WithEvents修饰符创建该变量,以便您可以附加下面的绘制事件:

Friend WithEvents ribbon_holder As New Panel

Private Sub rh_Paint(sender As Object, e As PaintEventArgs) Handles ribbon_holder.Paint
 'GDI drawing in here persist
 e.Graphics.DrawRectangle(BluePen, 0, 0, 100, 50)
End Sub