这个是我编写的用来取得窗口位置信息的程序
你把鼠标位置减去窗口位置 得到相对位置
'
'该程序可以通过鼠标指针的位置判断鼠标所指的窗口的参数
'辽宁大学。物理学院 yoya0303
'
Private Declare Function GetWindowPlacement Lib "user32" (ByVal hwnd As Long, lpwndpl As WINDOWPLACEMENT) As Long
Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
Private Type POINTAPI
x As Long
y As Long
End Type
Private Declare Function WindowFromPoint Lib "user32" (ByVal xPoint As Long, ByVal yPoint As Long) As Long
Private Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type
Private Type WINDOWPLACEMENT
Length As Long
flags As Long
showCmd As Long
ptMinPosition As POINTAPI
ptMaxPosition As POINTAPI
rcNormalPosition As RECT
End Type
Private Sub Form_Load()
Timer1Interval = 100
End Sub
Private Sub Timer1_Timer()
Dim pt As POINTAPI
Dim wfp As WINDOWPLACEMENT
Dim x As Long
Dim ret As Long
GetCursorPos pt
x = WindowFromPoint(ptx, pty)
ret = GetWindowPlacement(x, wfp)
Label1Caption = wfprcNormalPositionLeft '窗口距屏幕左边距离
Label2Caption = wfprcNormalPositionRight '窗口距屏幕右边距离
Label3Caption = wfprcNormalPositionTop '窗口距屏幕顶距离
Label4Caption = wfprcNormalPositionBottom '窗口距屏幕底距离
End Sub
用API函数GetCursorPos来获取位置;;
鼠标x位置,
Shift
As
Integer;
&
P,
GetCursorPos
P
"
Y
End
If
End
Sub
方法二,
"
(ByVal
vKey
As
Long)
As
Integer
Private
Declare
Function
GetCursorPos
Lib
",
X
As
Single:"右键按下",哪怕鼠标不在应用程序内也行
在窗体上添加一个计时器;
&
P,最高位为1,用GetAsyncKeyState来获取是否按下左键
这种方法在任何时候都可以获取X
&
",其坐标值为所在容器的相对坐标值)
Private
Sub
Form_MouseDown(Button
As
Integer;
(lpPoint
As
POINTAPI)
As
Long
Private
Type
POINTAPI
X
As
Long
Y
As
Long
End
Type
Private
Sub
Timer1_Timer()
Dim
P
As
POINTAPI
X
=
GetAsyncKeyState(1)
If
X
=
-32767
Then
':"
X;
&
P:":
直接用
MouseDown事件
举例;;
鼠标y位置;,是屏幕中的位置(不是相对窗体的)
Private
Declare
Function
GetAsyncKeyState
Lib
"Y
End
If
X
=
GetAsyncKeyState(2)
If
X
=
-32767
Then
Cls
":",设置Interval属性为10
获取的位置的数字,
GetCursorPos
P
":(这种方法获取的是点的位置在窗体中;user32"
&
P;x返回的是16位整数:"user32";Y:",却该对象有MouseDown事件才行;
鼠标y位置;
鼠标x位置,
Y
As
Single)
If
Button
=
1
Then
Cls
",表明按下
Cls
"X
&
"X;左键按下"方法一
Private Sub Form_Load()
ScaleMode = 3 '像素模式
End Sub
Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
MsgBox X & "," & Y
End Sub
获取的是相对窗体区域左上角(不含标题栏)
Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
Text1Text = X
Text2Text = Y
End Sub
Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
MeCaption = X & "," & Y
End Sub
VBA讲的是逻辑,那么从逻辑上来说
单元格的位置可以作为坐标来反馈,故可以使用Cells(SelectionRow, SelectionColumn)来确定当前光标所处的坐标,但这仅仅局限于一个单元格,cells(r,c)分别为行号和列号,通过坐标来判断光标所处位置。同样的,如果用cells(r,c+1)则可以 *** 作光标所处单元格的右数1个单元格的属性,并且在 *** 作右侧单元格时光标依然在当前的单元格上,所以如果下一条命令是cells(r+1,c)则可 *** 作下方相邻单元格,举个例子就是如果cells(r,c)是A1,cells(r,c+1)就是B1,cells(r+1,c)是A2
那么只要让textbox的值 = Cells(SelectionRow, SelectionColumn)Value 即=cells(r,c)的值,就可以让textbox显示出当前光标所处单元格的值。也就是无论你光标移动到哪里,窗体都能显示出光标所处的单元格的值
最后,你提到的“右击”,VBA无法判断左击还是右击,但无论你怎么击都属于“选中 or 激活”这个 *** 作,所以 无论你左击还是右击某个单元格,Cells(SelectionRow, SelectionColumn)都能指出当前激活单元格的坐标。
看到你的问题补充就不知道你想要写成什么样子的了。
给你两段代码把。
一、获取鼠标在屏幕上的坐标
'坐标显示在Text1,Text2
'建一个Timer1,Text1,Text2
'代码如下。
'============
Private Type POINTAPI
x As Long
y As Long
End Type
Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
Dim p As POINTAPI
Private Sub Form_Load()
Timer1Interval = 10
End Sub
Private Sub Timer1_Timer()
GetCursorPos p
Text1Text = px
Text2Text = py
End Sub
二、获取鼠标在窗体内的坐标
'坐标显示在标题上
'运行此程序需要再窗体添加控件Timer
Option Explicit
Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
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 Form_Load()
Timer1Interval = 10 '设置获取鼠标坐标间隔
End Sub
Private Sub Timer1_Timer()
Dim P As POINTAPI
GetCursorPos P '获取鼠标在屏幕中的位置
ScreenToClient Mehwnd, P '转换为本窗体的坐标
Dim t As Boolean
t = Px >= 0 And Py >= 0 And Px < MeWidth / ScreenTwipsPerPixelX And Py <= MeHeight / ScreenTwipsPerPixelY
If t Then MeCaption = "x=" & Px & "y=" & Py '按像素显示坐标
'If t Then MeCaption = "x=" & Px ScreenTwipsPerPixelX & "y=" & Py ScreenTwipsPerPixelY '按缇显示坐标
End Sub
在你的窗体代码最顶上(那就第一行吧,其实就是“声明”那里),加入以下代码:
Private Type POINTAPI
x As Long
y As Long
End Type
Public Declare Function ClientToScreen Lib "user32" Alias "ClientToScreen" (ByVal hWnd As Long, lpPoint As POINTAPI) As Long
Public Declare Function GetCursorPos Lib "user32" Alias "GetCursorPos" (lpPoint As POINTAPI) As Long
然后呢,我告诉你取鼠标相对控件位置的方法(注意注意:这里取得的坐标是以像素为单位的!!!而VB默认单位是“缇”即Twips。1像素=15缇):
Dim MousePos As POINTAPI
GetCursorPos MousePos
ClientToScreen 控件名hWnd, MousePos
完了以后,MousePosx和MousePosy就是鼠标相对控件的位置的横纵坐标。去掉“ClientToScreen 控件名hWnd, MousePos”这句,就是取鼠标在屏幕上的位置。
你原来的代码里有
Private Sub Picture2_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
这个过程。实际上,这里的X和Y是鼠标在容器(控件)内的位置,以“缇”为单位。要转化成像素(Pixel),你得把原坐标的横纵值除以15
把鼠标移到像素坐标不是整数的位置,是不可能的事,所以获得的像素坐标是没误差的,转换成的窗体缇坐标也没误差。
默认设置下,缇和像素的关系是15,如果你没改过设置,最好确认一下是不是12。
以上就是关于vb 如何获取指定程序的窗口内的鼠标坐标全部的内容,包括:vb 如何获取指定程序的窗口内的鼠标坐标、vb中如何获得鼠标位置 要在timer事件下获得、vb 里面 鼠标按下 获取点的坐标等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)