问,路在何方啊?

jing 2000-03-14 11:45:00
当mouse移出对窗体时,窗体可不可以继续得到Mouse的位置。继续运行窗体中的程序
...全文
130 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
Lin 2000-03-14
  • 打赏
  • 举报
回复
见VC中你的同一个问题。
apollo 2000-03-14
  • 打赏
  • 举报
回复
窗体上加一个time控件

Module1.bas

Option Explicit

Type POINTAPI
X As Long
Y As Long
End Type

Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type

Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
Declare Function GetWindowRect Lib "user32" (ByVal hwnd As Long, lpRect As RECT) As Long


form1.frm

Option Explicit

Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)

Dim B As RECT
Dim A As POINTAPI
GetCursorPos A
GetWindowRect Form1.hwnd, B
'Lblx = X / Screen.TwipsPerPixelX
Lblx = A.X - B.Left
Lbly = A.Y - B.Top

End Sub
929 2000-03-14
  • 打赏
  • 举报
回复
可试试这个API函数,它可捕获鼠标,把鼠标移动事件传给窗口。
HWND SetCapture(

HWND hWnd // handle of window to receive mouse capture
);
如果只想得到鼠标的位置,可在程序中调用下面这个函数。
BOOL GetCursorPos(

LPPOINT lpPoint // address of structure for cursor position
);
jing 2000-03-14
  • 打赏
  • 举报
回复
谢谢各位出手相助,我现在正在做一个类似3D Engine的程序,由VB做前台的interface,调用DX SDK做的DLL,类似网上的VRML的程序,所以时常烦大家。再次感谢。
Janven 2000-03-14
  • 打赏
  • 举报
回复
有幸回答版主问题。
929提供的函数SetCapture是不能监测另一个程序的窗口鼠标位置的。介绍加上三个GetCapture:取得目前拥有鼠标Capture的窗口hWnd,GetCursorPos取得鼠标的位置,WindowFromPoint取得某一位置底下的窗口hWnd。

Type POINTAPI
x As Long
y As Long
End Type

Declare Function SetCapture Lib "user32" (ByVal hWnd As Long) As Long
Declare Function ReleaseCapture Lib "user32" () As Long
Declare Function GetCapture Lib "user32" () As Long

Declare Function WindowFromPoint Lib "user32" (ByVal xPoint As Long, ByVal yPoint As Long) As Long
Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long

接下来在窗体中放一个Timer控件,定时判断鼠标所在位置的窗口hWnd是否等于拥有鼠标Capture的窗口hWnd,如果不是,则表示鼠标移到另外的程序中。这时就可以从利用Timer事件得到GetCursorPos的鼠标数据来继续程序控制了。
Form_Load事件添加

SetCapture Me.hWnd
Form_Unload事件添加
ReleaseCapture
Time1_Timer事件
Dim hCursorWnd As Long, point As POINTAPI

GetCursorPos point
hCursorWnd = WindowFromPoint(point.x, point.y)

If GetCapture <> 0 And GetCapture <> hCursorWnd Then
’添加程序
ReleaseCapture
End If

7,759

社区成员

发帖
与我相关
我的任务
社区描述
VB 基础类
社区管理员
  • VB基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧