取得視窗位置

使用API

API名稱:ScreenToClient
動態連結程式庫: user32.dll


程式說明

取得程式視窗在螢幕中的X軸及Y軸。
vb6有BUG,取出值為負。


特殊類別

使用類別:POINTAPI

vb6

Private Type POINTAPI
    X As Long
    Y As Long
End Type

vb.net

Structure POINTAPI
    Dim x As Integer
    Dim y As Integer
End Structure

VB6程式碼

Private Declare Function ScreenToClient Lib "user32" (ByVal hWnd As Long, lpPoint As POINTAPI) As Long

Private Type POINTAPI
    X As Long
    Y As Long
End Type

Private Sub Timer1_Timer()
    Dim lpPoint As POINTAPI
        ScreenToClient Me.hWnd, lpPoint
        lblMouseState = "X = " & lpPoint.X & "   Y = " & lpPoint.Y
        Text1.Text = lblMouseState
End Sub

VB.NET程式碼

Imports System.Runtime.InteropServices

Public Class Form1
    Structure POINTAPI
        Dim x As Integer
        Dim y As Integer
    End Structure
    <DllImport("user32.dll", SetLastError:=True)>
    Private Shared Function ScreenToClient(ByVal hWnd As IntPtr, ByRef lpPoint As POINTAPI) As Boolean
    End Function

    ' Display Windows screen coordinates relative to current form.
    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Dim rect As POINTAPI
        ScreenToClient(Handle, rect)
        TextBox1.Text = "x:" & rect.x * -1
        TextBox2.Text = "y:" & rect.y * -1
    End Sub

End Class

下載