展开全部
VERSION 5.00 Begin VB.Form FrmMain AutoRedraw = -1 'True Caption = "光滑直线" ClientHeight = 6780 ClientLeft = 60 ClientTop = 450 ClientWidth = 9795 LinkTopic = "Form1" ScaleHeight = 452 ScaleMode = 3 'Pixel ScaleWidth = 653 StartUpPosition = 3 '窗口缺省 Begin VB.CommandButton CmdDraw Caption = "绘制" Height = 375 Left = 120 TabIndex = 0 Top = 120 Width = 1215 End Begin VB.Line Line1 Visible = 0 'False X1 = 48 X2 = 272 Y1 = 128 Y2 = 88 End End Attribute VB_Name = "FrmMain" Attribute VB_GlobalNameSpace = False Attribute VB_Creatable = False Attribute VB_PredeclaredId = True Attribute VB_Exposed = False Private Declare Function SetPixel Lib "gdi32" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long, ByVal crColor As Long) As Long Function Min(s1 As Long, s2 As Long) As Long If s1 < s2 Then Min = s1 Else Min = s2 End Function Function Max(s1 As Long, s2 As Long) As Long If s1 > s2 Then Max = s1 Else Max = s2 End Function Private Sub CmdDraw_Click() Dim Scal As Single, Gray As Long, MinX As Long, MaxX As Long, MinY As Long, MaxY As Long Dim X1 As Long, X2 As Long, Y1 As Long, Y2 As Long, X3 As Single, Y3 As Single Line1.Visible = False MinX = Min(Line1.X1, Line1.X2): MaxX = Max(Line1.X1, Line1.X2) MinY = Min(Line1.Y1, Line1.Y2): MaxY = Max(Line1.Y1, Line1.Y2) If (Line1.Y1 < Line1.Y2 And Line1.X1 < Line1.X2) Or (Line1.Y1 > Line1.Y2 And Line1.X1 > Line1.X2) Then If Abs(Line1.X1 - Line1.X2) > Abs(Line1.Y1 - Line1.Y2) Then Scal = Abs(Line1.Y2 - Line1.Y1) / Abs(Line1.X2 - Line1.X1) For i = 0 To MaxX - MinX Y3 = i * Scal + MinY: Y1 = Int(Y3): Y2 = Y1 + 1 Gray = (Y2 - Y3) * 255 SetPixel Me.hdc, i + MinX, Y2, RGB(Gray, Gray, Gray) Gray = (Y3 - Y1) * 255 SetPixel Me.hdc, i + MinX, Y1, RGB(Gray, Gray, Gray) Next i Else Scal = Abs(Line1.X2 - Line1.X1) / Abs(Line1.Y2 - Line1.Y1) For i = 0 To MaxY - MinY X3 = i * Scal + MinX: X1 = Int(X3): X2 = X1 + 1 Gray = (X2 - X3) * 255 SetPixel Me.hdc, X2, i + MinY, RGB(Gray, Gray, Gray) Gray = (X3 - X1) * 255 SetPixel Me.hdc, X1, i + MinY, RGB(Gray, Gray, Gray) Next i End If Else If Abs(Line1.X1 - Line1.X2) > Abs(Line1.Y1 - Line1.Y2) Then Scal = -Abs(Line1.Y2 - Line1.Y1) / Abs(Line1.X2 - Line1.X1) For i = 0 To MaxX - MinX Y3 = i * Scal + MaxY: Y1 = Int(Y3): Y2 = Y1 + 1 Gray = (Y2 - Y3) * 255 SetPixel Me.hdc, i + MinX, Y2, RGB(Gray, Gray, Gray) Gray = (Y3 - Y1) * 255 SetPixel Me.hdc, i + MinX, Y1, RGB(Gray, Gray, Gray) Next i Else Scal = -Abs(Line1.X2 - Line1.X1) / Abs(Line1.Y2 - Line1.Y1) For i = 0 To MaxY - MinY X3 = i * Scal + MaxX: X1 = Int(X3): X2 = X1 + 1 Gray = (X2 - X3) * 255 SetPixel Me.hdc, X2, i + MinY, RGB(Gray, Gray, Gray) Gray = (X3 - X1) * 255 SetPixel Me.hdc, X1, i + MinY, RGB(Gray, Gray, Gray) Next i End If End If Me.Refresh End Sub Private Sub Form_Load() Me.ScaleMode = vbPixels Me.AutoRedraw = True End Sub Private Sub Form_MouseDown(Button As Integer, Shift As Integer, x As Single, y As Single) Line1.Visible = True Line1.X1 = x: Line1.Y1 = y End Sub Private Sub Form_MouseMove(Button As Integer, Shift As Integer, x As Single, y As Single) If Button = vbLeftButton Then Line1.X2 = x: Line1.Y2 = y End Sub Private Sub Form_MouseUp(Button As Integer, Shift As Integer, x As Single, y As Single) Line1.X2 = x: Line1.Y2 = y End Sub
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询