Option Explicit Private Declare Function SetPixel Lib "gdi32" _ (ByVal hdc As Long, ByVal x As Long, _ ByVal y As Long, ByVal crColor As Long) As Long
Private Sub Command1_Click() Dim i As Integer, re As Long For i = 0 To 2000 Step 2 re = SetPixel(Picture1.hdc, i, i, RGB(255, 0, 0)) Next For i = 0 To 2000 Step 2 re = SetPixel(Picture1.hdc, i, 100, RGB(255, 0, 0)) Next For i = 0 To 2000 Step 2 re = SetPixel(Picture1.hdc, 100, i, RGB(255, 0, 0)) Next End Sub
改成函数Option ExplicitPrivate Declare Function SetPixel Lib "gdi32" _ (ByVal hdc As Long, ByVal x As Long, _ ByVal y As Long, ByVal crColor As Long) As Long
Private Sub Command1_Click() Call sub_LineXY(50, 30, 200, 40)End Sub Private Sub sub_LineXY(ByVal x1 As Double, ByVal y1 As Double, ByVal x2 As Double, ByVal y2 As Double) Dim xs As Double Dim ys As Double Dim i As Integer
xs = (x2 - x1) / 2000 ys = (y2 - y1) / 2000
For i = 0 To 2000 Call SetPixel(Picture1.hdc, x1 + xs * i, y1 + ys * i, RGB(255, 0, 0)) Next i
End Sub
随意写的,可以根据需要修改, For i = 0 To 2000画了2001个点
一定要用SetPixel? line不行?
过两点(x1,y1),(x2,y2)的直线方程为y-y1=k(x-x1)。其中k=(y2-y1)/(x2-x1)。所以: Sub DarwLine(x1 as single,y1 as single,x2 as single,y2 as single) Dim k as single,y as single If x1=x2 Then '描点(x1,y)。y从y1到y2 Else k=(y2-y1)/(x2-x1) '描点(x,y)。其中x从x1到x2。y=k(x-x1)+y1 End If End Sub
你下这样去做会更方便些,点你任意选,不用去计算斜率。Private Sub Command1_Click() Picture1.DrawStyle = vbDot Picture1.ScaleMode = vbPixels Picture1.Line (50, 20)-(300, 30), vbRed Picture1.Line (50, 20)-(100, 400), vbRedEnd Sub
Option Explicit
Private Declare Function SetPixel Lib "gdi32" _
(ByVal hdc As Long, ByVal x As Long, _
ByVal y As Long, ByVal crColor As Long) As Long
Private Sub Command1_Click()
Dim i As Integer, re As Long
For i = 0 To 2000 Step 2
re = SetPixel(Picture1.hdc, i, i, RGB(255, 0, 0))
Next
For i = 0 To 2000 Step 2
re = SetPixel(Picture1.hdc, i, 100, RGB(255, 0, 0))
Next
For i = 0 To 2000 Step 2
re = SetPixel(Picture1.hdc, 100, i, RGB(255, 0, 0))
Next
End Sub
(ByVal hdc As Long, ByVal x As Long, _
ByVal y As Long, ByVal crColor As Long) As Long
Private Sub Command1_Click()
Call sub_LineXY(50, 30, 200, 40)End Sub
Private Sub sub_LineXY(ByVal x1 As Double, ByVal y1 As Double, ByVal x2 As Double, ByVal y2 As Double)
Dim xs As Double
Dim ys As Double
Dim i As Integer
xs = (x2 - x1) / 2000
ys = (y2 - y1) / 2000
For i = 0 To 2000
Call SetPixel(Picture1.hdc, x1 + xs * i, y1 + ys * i, RGB(255, 0, 0))
Next i
End Sub
随意写的,可以根据需要修改,
For i = 0 To 2000画了2001个点
Sub DarwLine(x1 as single,y1 as single,x2 as single,y2 as single)
Dim k as single,y as single
If x1=x2 Then
'描点(x1,y)。y从y1到y2
Else
k=(y2-y1)/(x2-x1)
'描点(x,y)。其中x从x1到x2。y=k(x-x1)+y1
End If
End Sub
Picture1.DrawStyle = vbDot
Picture1.ScaleMode = vbPixels
Picture1.Line (50, 20)-(300, 30), vbRed
Picture1.Line (50, 20)-(100, 400), vbRedEnd Sub