无法在表格上画线 [英] Can not draw lines on form
本文介绍了无法在表格上画线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有Windows 7(64位)
我有一个名为Form1的表单.
我也有一个名为DrawGid
的过程
I have Windows 7 (64Bit)
I have a form named Form1.
I also have a Procedure called DrawGid
Public Sub DrawGrid()
Dim g As Graphics = Me.CreateGraphics
Dim XN, YN As Integer
Dim midx, midy As Single
Dim UnitX, UnitY, OffsetX, OffsetY As Single
Dim P As New Pen(Color.Black, 4)
XN = 3 : YN = 3
OffsetX = 30 : OffsetY = 30
XN = 3 : YN = 3
With Me
midx = (.Width / 2)
midy = (.Height / 2)
UnitX = (.Width - OffsetX) / (2 * XN)
UnitY = (.Height - OffsetY) / (2 * YN)
End With
'Draw Horizondal Lines of the grid
'---------------------------------
g.DrawLine(Pens.Orchid, 0, midy, Me.Width, midy)
For i = 1 To YN
g.DrawLine(Pens.Black, 0, midy + UnitY * i, Me.Width, midy + UnitY * i)
g.DrawLine(Pens.Black, 0, midy - UnitY * i, Me.Width, midy - UnitY * i)
Next
'Draw Vertical Lines of the grid
'---------------------------------
g.DrawLine(Pens.Orchid, midx, 0, midx, Me.Height)
For i = 1 To XN
g.DrawLine(Pens.PaleGoldenrod, midx + UnitX * i, 0, midx + UnitX * i, Me.Height)
g.DrawLine(Pens.PaleGoldenrod, midx - UnitX * i, 0, midx - UnitX * i, Me.Height)
Next
End Sub
End Class
加载Form1时,我编写以下代码:
When I load Form1 I write the following code:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
DrawGrid()
End Sub
当我运行程序时,没有画线.任何人都可以
When I run the programe no lines are drawn. Could anybody please
help me?
推荐答案
为此,您将不得不覆盖"OnPaint"方法形式,并在其中添加绘图代码,如下所示:
For this you''ll have to override the forms ''OnPaint'' method and add the drawing code there, like so:
Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
Dim xn, yn, i As Integer
Dim midx, midy As Single
Dim Unitx, UnitY, OffsetX, OffsetY As Single
Dim p As New Pen(Color.Black, 4)
xn = 3
yn = 3
OffsetX = 30
OffsetY = 30
With Me
midx = (.Width / 2)
midy = (.Height / 2)
Unitx = (.Width - OffsetX) / (2 * xn)
UnitY = (.Height - OffsetY) / (2 * yn)
End With
e.Graphics.DrawLine(Pens.Orchid, 0, midy, Me.Width, midy)
For i = 1 To yn
e.Graphics.DrawLine(Pens.Black, 0, midy + UnitY * i, Me.Width, midy + UnitY * i)
e.Graphics.DrawLine(Pens.Black, 0, midy - UnitY * i, Me.Width, midy - UnitY * i)
Next i
e.Graphics.DrawLine(Pens.Orchid, midx, 0, midx, Me.Height)
For i = 1 To xn
e.Graphics.DrawLine(Pens.PaleGoldenrod, midx + Unitx * i, 0, midx + Unitx * i, Me.Height)
e.Graphics.DrawLine(Pens.PaleGoldenrod, midx - Unitx * i, 0, midx - Unitx * i, Me.Height)
Next i
End Sub
尝试使用图片框.
请参阅:使用VB在WinForms中绘制图形 [
Try using a picture box.
See: Drawing with Graphics in WinForms using VB[^]
Hope it helps.
Ahmed Djobby
这篇关于无法在表格上画线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文