Option ExplicitPrivate Sub Form_Load() Dim i As Long Picture1.AutoRedraw = True Picture1.Scale (0, 0)-(19, 19) For i = 0 To 19 Picture1.Line (0, i)-(19, i) Picture1.Line (i, 0)-(i, 19) Next End SubPrivate Sub Picture1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single) Dim iX As Long, iY As Long '<-像素坐标
Label1 = "(" & X & ", " & Y & ")" iX = Picture1.ScaleX(X, Picture1.ScaleMode, vbPixels) iY = Picture1.ScaleY(Y, Picture1.ScaleMode, vbPixels) Label2 = "(" & iX & ", " & iY & ")" End Sub
Option ExplicitPrivate Type POINTAPI x As Long y As Long End TypePrivate Type RECT Left As Long Top As Long Right As Long Bottom As Long End TypePrivate Declare Function ClientToScreen Lib "user32.dll" (ByVal hwnd As Long, ByRef lpPoint As POINTAPI) As Long Private Declare Function GetClientRect Lib "user32.dll" (ByVal hwnd As Long, ByRef lpRect As RECT) As Long Private Declare Function GetCursorPos Lib "user32.dll" (ByRef lpPoint As POINTAPI) As Long Private Declare Function PtInRect Lib "user32.dll" (ByRef lpRect As RECT, ByVal x As Long, ByVal y As Long) As Long Private Declare Function ScreenToClient Lib "user32.dll" (ByVal hwnd As Long, ByRef lpPoint As POINTAPI) As Long Private Declare Function SetCursorPos Lib "user32.dll" (ByVal x As Long, ByVal y As Long) As Long Private Sub Form_Load() Dim i As Long Picture1.AutoRedraw = True Picture1.Scale (0, 0)-(19, 19) For i = 0 To 19 Picture1.Line (0, i)-(19, i) Picture1.Line (i, 0)-(i, 19) Next End SubPrivate Sub Picture1_KeyDown(KeyCode As Integer, Shift As Integer) Dim pt As POINTAPI, rc As RECT Dim fUserX As Single, fUserY As Single
If Shift = 0 Then GetCursorPos pt ScreenToClient Picture1.hwnd, pt GetClientRect Picture1.hwnd, rc If PtInRect(rc, pt.x, pt.y) Then fUserX = Int(Picture1.ScaleX(pt.x, vbPixels, Picture1.ScaleMode)) + 0.5 fUserY = Int(Picture1.ScaleY(pt.y, vbPixels, Picture1.ScaleMode)) + 0.5 Select Case KeyCode Case vbKeyUp: fUserY = fUserY - 1 Case vbKeyDown: fUserY = fUserY + 1 Case vbKeyLeft: fUserX = fUserX - 1 Case vbKeyRight: fUserX = fUserX + 1 Case Else Exit Sub End Select
If PtInRect(rc, pt.x, pt.y) Then ClientToScreen Picture1.hwnd, pt SetCursorPos pt.x, pt.y End If End If End If End SubPrivate Sub Picture1_MouseMove(Button As Integer, Shift As Integer, x As Single, y As Single) Dim iX As Long, iY As Long
Label1 = "(" & x & ", " & y & ")" iX = Picture1.ScaleX(x, Picture1.ScaleMode, vbPixels) iY = Picture1.ScaleY(y, Picture1.ScaleMode, vbPixels) Label2 = "(" & iX & ", " & iY & ")" End Sub
PS:
俺VB的API浏览器不见了,怎么可以把它查出来?
刚才试了下,SetCursorPos应该也是相对于屏幕而言吧
Dim i As Long
Picture1.AutoRedraw = True
Picture1.Scale (0, 0)-(19, 19)
For i = 0 To 19
Picture1.Line (0, i)-(19, i)
Picture1.Line (i, 0)-(i, 19)
Next
End SubPrivate Sub Picture1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
Dim iX As Long, iY As Long '<-像素坐标
Label1 = "(" & X & ", " & Y & ")"
iX = Picture1.ScaleX(X, Picture1.ScaleMode, vbPixels)
iY = Picture1.ScaleY(Y, Picture1.ScaleMode, vbPixels)
Label2 = "(" & iX & ", " & iY & ")"
End Sub
x As Long
y As Long
End TypePrivate Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End TypePrivate Declare Function ClientToScreen Lib "user32.dll" (ByVal hwnd As Long, ByRef lpPoint As POINTAPI) As Long
Private Declare Function GetClientRect Lib "user32.dll" (ByVal hwnd As Long, ByRef lpRect As RECT) As Long
Private Declare Function GetCursorPos Lib "user32.dll" (ByRef lpPoint As POINTAPI) As Long
Private Declare Function PtInRect Lib "user32.dll" (ByRef lpRect As RECT, ByVal x As Long, ByVal y As Long) As Long
Private Declare Function ScreenToClient Lib "user32.dll" (ByVal hwnd As Long, ByRef lpPoint As POINTAPI) As Long
Private Declare Function SetCursorPos Lib "user32.dll" (ByVal x As Long, ByVal y As Long) As Long
Private Sub Form_Load()
Dim i As Long
Picture1.AutoRedraw = True
Picture1.Scale (0, 0)-(19, 19)
For i = 0 To 19
Picture1.Line (0, i)-(19, i)
Picture1.Line (i, 0)-(i, 19)
Next
End SubPrivate Sub Picture1_KeyDown(KeyCode As Integer, Shift As Integer)
Dim pt As POINTAPI, rc As RECT
Dim fUserX As Single, fUserY As Single
If Shift = 0 Then
GetCursorPos pt
ScreenToClient Picture1.hwnd, pt
GetClientRect Picture1.hwnd, rc
If PtInRect(rc, pt.x, pt.y) Then
fUserX = Int(Picture1.ScaleX(pt.x, vbPixels, Picture1.ScaleMode)) + 0.5
fUserY = Int(Picture1.ScaleY(pt.y, vbPixels, Picture1.ScaleMode)) + 0.5
Select Case KeyCode
Case vbKeyUp: fUserY = fUserY - 1
Case vbKeyDown: fUserY = fUserY + 1
Case vbKeyLeft: fUserX = fUserX - 1
Case vbKeyRight: fUserX = fUserX + 1
Case Else
Exit Sub
End Select
pt.x = Picture1.ScaleX(fUserX, Picture1.ScaleMode, vbPixels)
pt.y = Picture1.ScaleY(fUserY, Picture1.ScaleMode, vbPixels)
If PtInRect(rc, pt.x, pt.y) Then
ClientToScreen Picture1.hwnd, pt
SetCursorPos pt.x, pt.y
End If
End If
End If
End SubPrivate Sub Picture1_MouseMove(Button As Integer, Shift As Integer, x As Single, y As Single)
Dim iX As Long, iY As Long
Label1 = "(" & x & ", " & y & ")"
iX = Picture1.ScaleX(x, Picture1.ScaleMode, vbPixels)
iY = Picture1.ScaleY(y, Picture1.ScaleMode, vbPixels)
Label2 = "(" & iX & ", " & iY & ")"
End Sub