好象有点难

cenlmmx 2005-08-16 12:25:24
要求很简单: 就是写代码把鼠标设定在界面的正中.
代码怎么写?
...全文
117 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
3q2008Com 2005-08-16
  • 打赏
  • 举报
回复
gz
cenlmmx 2005-08-16
  • 打赏
  • 举报
回复
谢谢,已经搞定
ljg963419 2005-08-16
  • 打赏
  • 举报
回复
Option Explicit

'API:设定鼠标的位置
Private Declare Function SetCursorPos Lib "user32" (ByVal x As Long, ByVal y As Long) As Long

Private Sub Command1_Click()

'取得鼠标在界面中间位置的Twip值(相对于Screen)
'并把Twip转换为Pixel
Dim x As Long
Dim y As Long
x = (Form1.Left + Form1.Width / 2) \ Screen.TwipsPerPixelX
y = (Form1.Top + Form1.Height / 2) \ Screen.TwipsPerPixelY

Dim ret As Long
ret = SetCursorPos(x, y) ' (X, Y) 为欲设定的鼠标座标,座标单位是 Pixel(像素)
End Sub
jxgzay 2005-08-16
  • 打赏
  • 举报
回复
经典,学习
3q2008Com 2005-08-16
  • 打赏
  • 举报
回复
经典的代码 学习
leolan 2005-08-16
  • 打赏
  • 举报
回复
Option Explicit

Private Type POINTAPI
x As Long
y As Long
End Type
Private Type RECT
left As Long
top As Long
right As Long
bottom As Long
End Type
Private Declare Function ClientToScreen Lib "user32.dll" (ByVal hwnd As Long, ByRef lpPoint As POINTAPI) As Long
Private Declare Function SetCursorPos Lib "user32.dll" (ByVal x As Long, ByVal y As Long) As Long
Private Declare Function ClipCursor Lib "user32.dll" (ByRef lpRect As Any) As Long
Private Declare Function GetWindowRect Lib "user32" (ByVal hwnd As Long, lpRect As RECT) As Long

Private Sub Command1_Click()
Dim pt As POINTAPI

pt.x = Me.ScaleWidth / Screen.TwipsPerPixelX / 2
pt.y = Me.ScaleHeight / Screen.TwipsPerPixelY / 2

ClientToScreen Me.hwnd, pt

'Set Cursor position
SetCursorPos pt.x, pt.y

End Sub

Private Sub Command2_Click()
Dim rt As RECT
Static flag As Boolean

If flag = False Then
'Limits the Cursor movement to within the Command2.
GetWindowRect Command2.hwnd, rt
ClipCursor rt
Else
'Releases the cursor limits
ClipCursor ByVal 0&
End If
flag = Not flag
End Sub

7,763

社区成员

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

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