VB6中窗体上有个图片框,图片框自定义坐标系,19X19的一个围棋盘,现在的想法是通过方向键控制鼠标在图片框上移动,移动的单位为坐标系的1个单位。用MouseEvent是在相对于电脑屏幕而言,有人说是可以通过ScreenToClient可以实现,可是还是不行!希望大家帮帮俺

解决方案 »

  1.   

    能否再详细点?
    PS:
    俺VB的API浏览器不见了,怎么可以把它查出来?
      

  2.   

    在菜单“外接程序”上选择“外接程序管理器”找到"VB 6 API VIEWER “把它加载
      

  3.   

    TO 2楼
    刚才试了下,SetCursorPos应该也是相对于屏幕而言吧
      

  4.   

    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
      

  5.   

    给你思路.记录一下鼠标的当前位置,然后收到键盘方向键时检查一下当前鼠标位置,看移动后是否到达区域边界.如果没有,那么就移动一个单位.这个单位直接用图片框的当前坐标系统转换为屏幕绝对坐标而来,再使用SetCursorPos设过去即可.
      

  6.   

    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
                
                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