我刚刚开始使用GDI - 第一行还可以 - 但是第二行...... 我想画一条从(0,0)到(MaxWidth,MaxHeight)的简单对角线。
这是我的代码:
Public Class Form1
Dim g As Graphics = Me.CreateGraphics
Dim stift As New Pen(Brushes.Black, 3)
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim x1 = 0, y1 = 0, x2 = Me.Size.Width, y2 = Me.Size.Height
g.DrawLine(stift, x1, y1, x2, y2)
End Sub
End Class
但该行以第一个未更改形式的MaxWidth和MaxHeight结束。 有人有什么想法吗?
答案 0 :(得分:1)
在Paint()事件中绘制线条的快速示例,并允许使用Button切换它:
Public Class Form1
Private x1 As Integer = 0
Private y1 As Integer = 0
Private x2 As Integer = 0
Private y2 As Integer = 0
Private DrawLine As Boolean = False
Private stift As New Pen(Brushes.Black, 3)
Public Sub New()
InitializeComponent()
x2 = Me.ClientSize.Width
y2 = Me.ClientSize.Height
End Sub
Private Sub Form1_SizeChanged(sender As Object, e As EventArgs) Handles MyBase.SizeChanged
x2 = Me.ClientSize.Width
y2 = Me.ClientSize.Height
Me.Refresh()
End Sub
Private Sub Form1_Paint(sender As Object, e As PaintEventArgs) Handles MyBase.Paint
If DrawLine Then
Dim g As Graphics = e.Graphics
g.DrawLine(stift, x1, y1, x2, y2)
End If
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
DrawLine = Not DrawLine
Me.Refresh()
End Sub
End Class
此方法允许您从其他地方更改coords并调用Refresh()来更新屏幕。对于多行,请考虑使用List()来保存有关coords的信息,然后在Paint()事件中迭代它。