picturebox默认的坐标系和我们常用的X,Y二维坐标系统不一样
而且其坐标数值的范围和数据点的数据范围也不一样
比如我们数据点的X在15-20,Y 在30-35
拟和的一元方程为Y = 0.9*a + 19
现在我们要在picturebox里绘制一系列的数据点和这条直线
如何调整坐标系,和显示范围,使得数据点和这条直线很好的得到显示!谢谢各位!!

解决方案 »

  1.   

    定义Picture1相对坐标:Picture1.Scale (ll, 20)-(6, -4)看一看我过去写的坐标X,Y的阶层变化图'Draw in the Form
    Public Sub PictureLine(Optional ByVal ScaleX As Long = 100, Optional ByVal ScaleY As Long = 100, Optional ByVal StartValueX As Long = 0, _
    Optional ByVal StartValueY As Long = 0, Optional ByVal MaxValueX As Long = 500, Optional ByVal MaxValueY As Long = 1000, Optional ByVal ScaleAmp As Single = 300, _
    Optional ByVal StartAmp As Single = 0, Optional ByVal EndAmp As Single, Optional ByVal ItemType As Integer = 1)
        
        Dim IndexX As Long
        Dim IndexY As Long
        Dim NumY As Integer
        Dim NumX As Integer
        Dim MaxY As Long
        Dim i As Long, II As Long, OldFontSize
        Dim LastAmp As Single
        
        LastAmp = EndAmp
        IndexX = 5
        IndexY = 5
    '    Width = 8640: Height = 5760   ' 按缇设置窗体大小
        AutoRedraw = -1   ' 打开AutoRedraw
        OldFontSize = FontSize   ' 保持旧的字体大小
        BackColor = QBColor(7)   ' 将背景设置为灰色
        Scale (0, 120)-(130, 0)   ' 设定自定义座标系统
        
        'Y轴与刻度线
        Line (20, 10)-(20, 115), RGB(0, 0, 255), BF
        II = 1
        For i = StartValueY To MaxValueY Step ScaleY
            If i <> 0 Then
                Line (20, 10 + IndexY * II)-(22, 10 + IndexY * II), RGB(0, 0, 255), BF   ' 每隔 10 个单位划尺寸标记
                CurrentX = CurrentX - 9
                CurrentY = CurrentY + 1.5   ' 移动光标位置
                Print i   ' Print scale  value on left
                II = II + 1
            End If
        Next i
        MaxY = i - StartValueY + 100
        
        If ItemType = 1 Or ItemType = 2 Then
            'Amp Chart
            Line (10, 10)-(10, 115), RGB(0, 0, 255), BF
            II = 1
            
            If LastAmp > ScaleAmp * 20 Then
                LastAmp = ScaleAmp * 20
            ElseIf (LastAmp < ScaleAmp * 20) And (LastAmp > Int(LastAmp / ScaleAmp) * ScaleAmp) Then
                LastAmp = LastAmp + ScaleAmp
            End If
            
            For i = 0 To LastAmp Step ScaleAmp
                If i <> 0 Then
                    Line (10, 10 + IndexY * II)-(12, 10 + IndexY * II), RGB(0, 0, 255), BF ' 每隔 10 个单位划尺寸标记
                    CurrentX = CurrentX - 9
                    CurrentY = CurrentY + 1.5   ' 移动光标位置
                    Print i   ' Print scale  value on left
                    II = II + 1
                End If
            Next i
            CurrentX = 4: CurrentY = 118
            Print "(mA)"
        End If    'X轴与刻度线
        II = 1
        Line (20, 10)-(125, 10), RGB(0, 0, 255), BF
        For i = StartValueX To MaxValueX Step ScaleX
            If i <> 0 Then
                Line (20 + IndexX * II, 10)-(20 + IndexX * II, 12), RGB(0, 0, 255), BF
                CurrentY = CurrentY - 3   ' 移动光标位置
                CurrentX = CurrentX - 3
                Print i  ' 将尺寸标记值打印在下边
                II = II + 1
            End If
        Next i
        
        CurrentX = 15.5: CurrentY = 10.5
        Print 0
        
        CurrentX = 14: CurrentY = 118
        If ItemType = 3 Then
            Print "(mA)"
        ElseIf ItemType = 1 Or ItemType = 2 Then
            Print "(mV)"
        End If
        
    '    CurrentX = 1: CurrentY = 111
    '    Print "△V"
        CurrentX = 121: CurrentY = 9.5
        Print "(mSec)"
    '    CurrentX = 115: CurrentY = 7.5
    '    Print "△T"
        
        NumY = CInt((MaxValueY - StartValueY) / ScaleY)
        NumX = CInt((MaxValueX - StartValueX) / ScaleX)
        
        If ItemType = 2 Then  'UVP Test
            II = 0
            For i = MaxValueY To StartValueY Step -ScaleY
                If CInt(CurrentX) < NumX * IndexX + 20 Then
                    Line (20 + II * IndexX, 10 + (NumY - II + 1) * IndexY)-(20 + (II + 1) * IndexX, 10 + (NumY - II + 1) * IndexY), RGB(255, 0, 0), BF ' 第一个蓝色条
                Else
                    Exit Sub
                End If
                
                If CInt(CurrentY) > 10 + IndexY And CInt(CurrentX) <> NumX * IndexX + 20 Then
                    Line (20 + (II + 1) * IndexX, 10 + (NumY - II + 1) * IndexY)-(20 + (II + 1) * IndexX, 10 + (NumY - II) * IndexY), RGB(255, 0, 0), BF    ' 第一个红色条
                Else
                    Exit For
                End If
                II = II + 1
            Next i
            
            'Amp Line
            Line (10, 10 + StartAmp / ScaleAmp * IndexY)-(20 + (NumX - 1) * IndexX, 10 + StartAmp / ScaleAmp * IndexY), RGB(0, 255, 0), BF
            Line (20 + (NumX - 1) * IndexX, 10 + StartAmp / ScaleAmp * IndexY)-(20 + (NumX - 1) * IndexX, 10), RGB(0, 255, 0), BF
            
            CurrentX = 20 + 0.5: CurrentY = 13 + (NumY + 1) * IndexY
            Print "〉T"
            CurrentX = 20 + 5.5: CurrentY = 9 + (NumY + 1) * IndexY
            Print "〉V"
            
            CurrentX = 50: CurrentY = 5
            Me.Font.Size = 10
            Print "筁溃玂臔代刚"
            
        ElseIf ItemType = 1 Or ItemType = 3 Then    'OVP Test Or SCP Test
        
            II = 0
            For i = MaxValueY To StartValueY Step -ScaleY
                If CInt(CurrentX) < NumX * IndexX + 20 And CInt(CurrentY) <= NumY * IndexY + 15 Then
                    Line (20 + II * IndexX, 10 + IndexY * (II + 1))-(20 + (II + 1) * IndexX, 10 + IndexY * (II + 1)), RGB(255, 0, 0), BF ' 第一个蓝色条
                Else
                    Exit For
                End If
                
                If CInt(CurrentY) < (NumY + 1) * IndexY + 10 And CInt(CurrentX) < NumX * IndexX + 20 Then
                    Line (20 + (II + 1) * IndexX, 10 + IndexY * (II + 1))-(20 + (II + 1) * IndexX, 10 + IndexY * (II + 2)), RGB(255, 0, 0), BF ' 第一个红色条
                Else
                    Exit For
                End If
                II = II + 1
            Next i
            
            If ItemType = 1 Then
                'Amp Line
                Line (10, 10 + StartAmp / ScaleAmp * IndexY)-(20 + (NumX - 1) * IndexX, 10 + StartAmp / ScaleAmp * IndexY), RGB(0, 255, 0), BF
                Line (20 + (NumX - 1) * IndexX, 10 + StartAmp / ScaleAmp * IndexY)-(20 + (NumX - 1) * IndexX, 10 + EndAmp / ScaleAmp * IndexY), RGB(0, 255, 0), BF
                Line (20 + (NumX - 1) * IndexX, 10 + EndAmp / ScaleAmp * IndexY)-(100, 10 + EndAmp / ScaleAmp * IndexY), RGB(0, 255, 0), BF
            End If
            
            CurrentX = 20 + 0.5: CurrentY = 10 + IndexY
            Print "〉T"        
            CurrentX = 50: CurrentY = 5
            Me.Font.Size = 10
            If ItemType = 1 Then
                Print "筁蔼溃玂臔代刚"
                CurrentX = 20 + IndexX: CurrentY = 10 + IndexY + 3.5
                Print "〉V"
            ElseIf ItemType = 3 Then
                Print "筁筿瑈玂臔代刚"
                CurrentX = 20 + IndexX: CurrentY = 10 + IndexY + 3.5
                Print "〉A"
            End If
            
        End If
        
    End Sub Private Sub Form_Load()
        Call PictureLine(, , , 600, 1200, 2700, 300, 300, 3500, OVPFlag)
    End Sub