限制鼠标的活动范围可用 ClipCursor 函数。我随意写了一下,经测试可以正常运行,但是只要移动窗口或使窗口失去焦点,限制就会失效,必须再次调用函数。我暂时没想到好的解决方法。
# 取得窗口句柄 HWND = Win32API.new('user32', 'GetActiveWindow', nil, 'l').call # 取得客户区矩形 client_rect = "\0" * 16 Win32API.new('user32', 'GetClientRect', 'lp', 'i').call(HWND, client_rect) ary = client_rect.unpack('llll') point_ul = ary[0..1].pack('ll') point_lr = ary[2..3].pack('ll') CTS = Win32API.new('user32', 'ClientToScreen', 'lp', 'i') CTS.call(HWND, point_ul) CTS.call(HWND, point_lr) client_rect = (point_ul.unpack('ll') + point_lr.unpack('ll')).pack('llll') # 限制鼠标 Win32API.new('user32', 'ClipCursor', 'p', 'l').call(client_rect) # 解除限制 Win32API.new('user32', 'ClipCursor', 'p', 'l').call(nil)
# 取得窗口句柄
HWND = Win32API.new('user32', 'GetActiveWindow', nil, 'l').call
# 取得客户区矩形
client_rect = "\0" * 16
Win32API.new('user32', 'GetClientRect', 'lp', 'i').call(HWND, client_rect)
ary = client_rect.unpack('llll')
point_ul = ary[0..1].pack('ll')
point_lr = ary[2..3].pack('ll')
CTS = Win32API.new('user32', 'ClientToScreen', 'lp', 'i')
CTS.call(HWND, point_ul)
CTS.call(HWND, point_lr)
client_rect = (point_ul.unpack('ll') + point_lr.unpack('ll')).pack('llll')
# 限制鼠标
Win32API.new('user32', 'ClipCursor', 'p', 'l').call(client_rect)
# 解除限制
Win32API.new('user32', 'ClipCursor', 'p', 'l').call(nil)
|