求VB PictureBox坐标与GDI坐标的转换关系
图片解释补充:红色十字线是X、Y轴,右上角两条线形状相似,但不重合;GDI划线函数忽略Picture1定义的坐标,仍按左上角0,0坐标系统划线。存在坐标转换问题。求:gd...
图片解释补充:红色十字线是X、Y轴,右上角两条线形状相似,但不重合;GDI划线函数忽略Picture1定义的坐标,仍按左上角0,0坐标系统划线。存在坐标转换问题。
求:gdi 绘图坐标与picturebox自定义坐标的转换关系。
主要源码:
窗体控件代码:
Option Explicit
Dim gdL As New gdiDrawLine
Dim X() As Double
Dim Y() As Double
Dim nCount As Long
Dim key As Integer
Private Sub cmd1_Click()
''【GDI划线】
Me.Picture1.ForeColor = QBColor(7)
Me.Picture1.DrawWidth = 2
''Me.Picture1.AutoRedraw = True
If gdL.DrawLine(Me.Picture1.hwnd, X, Y, nCount) = 0 Then
MsgBox gdL.ErrInfo, vbOKOnly + vbInformation, "错误"
End If
End Sub
Private Sub cmd2_Click()
''【退出】
Unload Me
End Sub
Private Sub cmd3_Click()
''【刷新】
Me.Picture1.Cls
End Sub
Private Sub cmd4_Click()
''【坐标重置】
If key = 1 Then
Me.Picture1.Scale (-200, 200)-(200, -200)
Picture1.DrawWidth = 1
Picture1.ForeColor = QBColor(12)
Picture1.Line (-90, 0)-(90, 0), QBColor(12)
Picture1.Line (0, -190)-(0, 190), QBColor(12)
key = 0
Else
''Me.Picture1.Cls
key = 1
Me.Picture1.Scale (0, 0)-(400, 400)
End If
End Sub
Private Sub cmd5_Click()
''【Pic划线】
Dim i As Long
Picture1.DrawWidth = 5
Picture1.PSet (X(0), Y(0)), QBColor(12)
Picture1.DrawWidth = 1
For i = 1 To nCount - 1
Picture1.Line -(X(i), Y(i)), QBColor(10)
Next i
End Sub
Private Sub Form_Load()
Dim V1 As Variant, V2 As Variant
Dim i As Integer
V1 = Array(12, 25, 34, 2, 9, 50, 40, 60, 25, 34)
V2 = Array(20, 35, 38, 44, 49, 60, 65, 70, 72, 75)
nCount = UBound(V1) + 1
ReDim X(0 To nCount - 1)
ReDim Y(0 To nCount - 1)
For i = 0 To nCount - 1
''初始化点数组
X(i) = V1(i)
Y(i) = V2(i)
Next i
Erase V1, V2
Me.Picture1.Scale (0, 0)-(400, 400)
key = 1
End Sub
Private Sub Form_Unload(Cancel As Integer)
Erase X, Y
Set gdL = Nothing
End Sub
''gdi划线的类代码(略)
''gdi划线的类代码
Option Explicit
''gdiDrawLine.cls
Public Function DrawLine(ByVal hwnd As Long, X As Variant, Y As Variant, ByVal nCount As Long) As Long
Dim dc As Long
Dim i As Long
dc = GetDC(hwnd)
MoveToEx dc, X(0), Y(0), 0
For i = 1 To nCount - 1
LineTo dc, X(i), Y(i)
Next i
DrawLine = -1
End Function 展开
求:gdi 绘图坐标与picturebox自定义坐标的转换关系。
主要源码:
窗体控件代码:
Option Explicit
Dim gdL As New gdiDrawLine
Dim X() As Double
Dim Y() As Double
Dim nCount As Long
Dim key As Integer
Private Sub cmd1_Click()
''【GDI划线】
Me.Picture1.ForeColor = QBColor(7)
Me.Picture1.DrawWidth = 2
''Me.Picture1.AutoRedraw = True
If gdL.DrawLine(Me.Picture1.hwnd, X, Y, nCount) = 0 Then
MsgBox gdL.ErrInfo, vbOKOnly + vbInformation, "错误"
End If
End Sub
Private Sub cmd2_Click()
''【退出】
Unload Me
End Sub
Private Sub cmd3_Click()
''【刷新】
Me.Picture1.Cls
End Sub
Private Sub cmd4_Click()
''【坐标重置】
If key = 1 Then
Me.Picture1.Scale (-200, 200)-(200, -200)
Picture1.DrawWidth = 1
Picture1.ForeColor = QBColor(12)
Picture1.Line (-90, 0)-(90, 0), QBColor(12)
Picture1.Line (0, -190)-(0, 190), QBColor(12)
key = 0
Else
''Me.Picture1.Cls
key = 1
Me.Picture1.Scale (0, 0)-(400, 400)
End If
End Sub
Private Sub cmd5_Click()
''【Pic划线】
Dim i As Long
Picture1.DrawWidth = 5
Picture1.PSet (X(0), Y(0)), QBColor(12)
Picture1.DrawWidth = 1
For i = 1 To nCount - 1
Picture1.Line -(X(i), Y(i)), QBColor(10)
Next i
End Sub
Private Sub Form_Load()
Dim V1 As Variant, V2 As Variant
Dim i As Integer
V1 = Array(12, 25, 34, 2, 9, 50, 40, 60, 25, 34)
V2 = Array(20, 35, 38, 44, 49, 60, 65, 70, 72, 75)
nCount = UBound(V1) + 1
ReDim X(0 To nCount - 1)
ReDim Y(0 To nCount - 1)
For i = 0 To nCount - 1
''初始化点数组
X(i) = V1(i)
Y(i) = V2(i)
Next i
Erase V1, V2
Me.Picture1.Scale (0, 0)-(400, 400)
key = 1
End Sub
Private Sub Form_Unload(Cancel As Integer)
Erase X, Y
Set gdL = Nothing
End Sub
''gdi划线的类代码(略)
''gdi划线的类代码
Option Explicit
''gdiDrawLine.cls
Public Function DrawLine(ByVal hwnd As Long, X As Variant, Y As Variant, ByVal nCount As Long) As Long
Dim dc As Long
Dim i As Long
dc = GetDC(hwnd)
MoveToEx dc, X(0), Y(0), 0
For i = 1 To nCount - 1
LineTo dc, X(i), Y(i)
Next i
DrawLine = -1
End Function 展开
1个回答
展开全部
Dim picX, picY, x1, y1, 滑贺x2, y2, gdiX, gdiY
picX = 0 '要转换的Picture1坐标X
picY = 0 '要转换芦凳的Picture1坐标Y
x1 = Picture1.ScaleLeft
y1 = Picture1.ScaleTop
x2 = Picture1.ScaleLeft + Picture1.ScaleWidth
y2 = Picture1.ScaleTop + Picture1.ScaleHeight '记录原先的坐标系统
Picture1.ScaleMode = 3 '坐标系统设为像素
gdiX = Round((picX - x1) / (x2 - x1) * Picture1.ScaleWidth)
gdiY = Round((picY - y1) / (y2 - y1) * Picture1.ScaleHeight)
MsgBox "Picture1的坐标(" & picX & "," & picY & ")转为GDI坐标是(" & gdiX & "," & gdiY & ")"
Picture1.Scale (x1, y1)-(x2, y2) '陪让旅改回原先的坐标系统
更多追问追答
追问
gdiX = (picX - scaleLeft) / (x2 - scaleLeft) * scaleWidth ‘’()/ 400*400
gdiY = (picY - scaleTop) / (y2 - scaleTop) * scaleHeight ‘’()/ -400* -400
经测试:貌似不行
追答
你这样写当然不行,这两句代码与我写的代码含义完全不一样。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询