看到你的问题补充就不知道你想要写成什么样子的了。
给你两段代码把。
'坐标显示在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
通过窗体的MouseDown事件的eX和eY属性获取。通过eLocation属性获取Point类型的坐标。
下面的代码示例使用 Location 属性跟踪鼠标左键单击,并绘制一系列直线段作为对用户输入的响应。如果隐藏窗体然后重新显示它,此示例不保留已绘制的线段,该代码为简单起见而被省略了。
Dim FirstPoint As Point
Dim HaveFirstPoint As Boolean = False
Sub Form1_MouseDownDrawing(ByVal sender As Object, ByVal e As SystemWindowsFormsMouseEventArgs) Handles MeMouseDown
If HaveFirstPoint Then
Dim g As Graphics = MeCreateGraphics()
gDrawLine(PensBlack, FirstPoint, eLocation)
HaveFirstPoint = False
Else
FirstPoint = eLocation
HaveFirstPoint = True
End If
End Sub
private
sub
command1_mousemove(button
as
integer,
shift
as
integer,
x
as
single,
y
as
single)
command1mousepointer
=
vbcross
end
sub
private
sub
command1_mouseup(button
as
integer,
shift
as
integer,
x
as
single,
y
as
single)
if
button
=
vbrightbutton
then
msgbox
"现在鼠标光标位于命令按钮中"
end
if
end
sub
private
sub
form_load()
'picture1mouseicon
=
loadpicture("c:\handcur")'光标文件的路径
end
sub
private
sub
form_mousemove(button
as
integer,
shift
as
integer,
x
as
single,
y
as
single)
memousepointer
=
vbdefault
end
sub
private
sub
form_mouseup(button
as
integer,
shift
as
integer,
x
as
single,
y
as
single)
if
button
=
vbrightbutton
then
msgbox
"现在鼠标光标位于窗体上"
end
if
end
sub
private
sub
picture1_mousemove(button
as
integer,
shift
as
integer,
x
as
single,
y
as
single)
picture1mousepointer
=
vbcustom
end
sub
private
sub
picture1_mouseup(button
as
integer,
shift
as
integer,
x
as
single,
y
as
single)
if
button
=
vbrightbutton
then
msgbox
"现在鼠标光标位于框中"
end
if
end
sub
private
sub
text1_mousemove(button
as
integer,
shift
as
integer,
x
as
single,
y
as
single)
text1mousepointer
=
vbhourglass
end
sub
private
sub
text1_mouseup(button
as
integer,
shift
as
integer,
x
as
single,
y
as
single)
if
button
=
vbrightbutton
then
msgbox
"现在鼠标光标位于文本框中"
end
if
end
sub
Form有个MousePointer的属性可设置和返回当前窗体鼠标的形状。
ScreenMousePointer 可设置和返回当前荧屏鼠标的形状。
你要全局的应该就是ScreenMousePointer 吧?
以上就是关于vb 获取鼠标在窗口内的屏幕坐标全部的内容,包括:vb 获取鼠标在窗口内的屏幕坐标、vb.net 怎样获取鼠标单击点的坐标、VB编程如何获取鼠标双击事件等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)