【開源項目】花密(Flower Password)VB版之鼠標定位模塊

'*****************************************************************
' Copyright (c) 2011-2012 FlowerPassword.com All rights reserved.
'      Author : xLsDg @ Xiao Lu Software Development Group
'        Blog : http://hi.baidu.com/xlsdg
'          QQ : 4 4 7 4 0 5 7 4 0
'     Version : 1 . 0 . 0 . 0
'        Date : 2 0 1 2 / 0 4 / 0 7
' Description :
'     History :
'*****************************************************************
Option Explicit

Public Password_Hwnd As Long

Private Declare Function GetWindowRect _
                Lib "user32.dll" (ByVal hwnd As Long, _
                                  lpRect As RECT) As Long

Private Type RECT

    Left As Long
    Top As Long
    Right As Long
    Bottom As Long

End Type

Private Declare Function GetCursorPos Lib "user32.dll" (lpPoint As POINTAPI) As Long

Private Type POINTAPI

    X As Long
    Y As Long

End Type

Private Declare Function WindowFromPoint _
                Lib "user32.dll" (ByVal xPoint As Long, _
                                  ByVal yPoint As Long) As Long

'獲得擁有輸入焦點的窗口的句柄
Private Declare Function GetFocus Lib "user32.dll" () As Long

'獲得前臺窗口的句柄。這裏的“前臺窗口”是指前臺應用程序的活動窗口
Private Declare Function GetForegroundWindow Lib "user32.dll" () As Long

'通常,系統內的每個線程都有自己的輸入隊列。本函數(既“連接線程輸入函數”)允許線程和進程共享輸入隊列。連接了線程後,輸入焦點、窗口激活、鼠標捕獲、鍵盤狀態以及輸入隊列狀態都會進入共享狀態
Private Declare Function AttachThreadInput _
                Lib "user32.dll" (ByVal idAttach As Long, _
                                  ByVal idAttachTo As Long, _
                                  ByVal fAttach As Long) As Long

'獲取當前線程一個唯一的線程標識符
Private Declare Function GetCurrentThreadId Lib "kernel32.dll" () As Long

'獲取與指定窗口關聯在一起的一個進程和線程標識符
Private Declare Function GetWindowThreadProcessId _
                Lib "user32.dll" (ByVal hwnd As Long, _
                                  lpdwProcessId As Long) As Long

'判斷插入符的當前位置
Private Declare Function GetCaretPos Lib "user32.dll" (lpPoint As POINTAPI) As Long

'判斷窗口內以客戶區座標表示的一個點的屏幕座標
Private Declare Function ClientToScreen _
                Lib "user32.dll" (ByVal hwnd As Long, _
                                  lpPoint As POINTAPI) As Long

Public Function GetDesktopWindowCaretPos(ByRef Point As POINTAPI) As Long

    Dim foregroundWindowHandle As Long

    foregroundWindowHandle = GetForegroundWindow

    Dim remoteThreadId As Long

    remoteThreadId = GetWindowThreadProcessId(foregroundWindowHandle, 0)

    Dim currentThreadId As Long

    currentThreadId = GetCurrentThreadId()

    Dim result As Long

    result = AttachThreadInput(currentThreadId, remoteThreadId, True)

    If result <> 0 Then

        Dim focused As Long

        focused = GetFocus

        Dim ThisPoint As Long

        ThisPoint = GetCaretPos(Point)
        ClientToScreen focused, Point
        AttachThreadInput currentThreadId, remoteThreadId, False
        GetDesktopWindowCaretPos = focused
    Else
        GetDesktopWindowCaretPos = 0

    End If

End Function

Public Function GetDesktopWindowRect(ByRef rct As RECT, ByRef pos As POINTAPI) As Long
    GetCursorPos pos

    Dim WinHandle As Long

    WinHandle = WindowFromPoint(pos.X, pos.Y)

    Dim execute As Integer

    execute = GetWindowRect(WinHandle, rct)

    If execute = 0 Then
        GetDesktopWindowRect = 0
    Else
        GetDesktopWindowRect = WinHandle

    End If

End Function

Public Sub getLocation(ByRef point_x As Long, ByRef point_y As Long)

    Dim caretpos As POINTAPI, mousepos As POINTAPI, rects As RECT, caretpos_hWnd As Long, rect_hWnd As Long

    caretpos_hWnd = GetDesktopWindowCaretPos(caretpos)
    rect_hWnd = GetDesktopWindowRect(rects, mousepos)

    If caretpos_hWnd <> 0 Then
        Password_Hwnd = caretpos_hWnd

        If (mousepos.Y - caretpos.Y) < 50 Then
            point_x = caretpos.X '* Screen.TwipsPerPixelX
            point_y = (caretpos.Y + 17) '* Screen.TwipsPerPixelY
        Else
            point_x = mousepos.X '* Screen.TwipsPerPixelX
            point_y = (mousepos.Y + 10) '* Screen.TwipsPerPixelY

        End If

    ElseIf rect_hWnd <> 0 Then
        Password_Hwnd = rect_hWnd

        If (rects.Bottom - caretpos.Y) < 50 Then
            point_x = rects.Left '* Screen.TwipsPerPixelX
            point_y = rects.Bottom '* Screen.TwipsPerPixelY
        Else
            point_x = mousepos.X '* Screen.TwipsPerPixelX
            point_y = (mousepos.Y + 10) '* Screen.TwipsPerPixelY

        End If

    Else
        Password_Hwnd = 0
        point_x = mousepos.X '* Screen.TwipsPerPixelX
        point_y = (mousepos.Y + 10) '* Screen.TwipsPerPixelY

    End If

End Sub


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章