设为首页收藏本站|繁體中文

Project1

 找回密码
 注册会员
搜索
查看: 1385|回复: 5
打印 上一主题 下一主题

[已经过期] 如何显示光标

 关闭 [复制链接]

Lv1.梦旅人

梦石
0
星屑
50
在线时间
27 小时
注册时间
2010-7-21
帖子
27
跳转到指定楼层
1
发表于 2011-6-18 20:42:01 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
如何在游戏中显示光标(鼠标)?
搜索竟然没有啊

Lv1.梦旅人

反伸手党斗士

梦石
0
星屑
91
在线时间
1128 小时
注册时间
2009-9-10
帖子
2513

贵宾

2
发表于 2011-6-18 20:43:22 | 只看该作者
搜索“鼠标脚本”

点评

不是 是鼠标一进入游戏界面就没了 搞不清鼠标在哪里  发表于 2011-6-18 20:58
回复

使用道具 举报

Lv2.观梦者

虚構歪曲

梦石
0
星屑
364
在线时间
1198 小时
注册时间
2010-12-18
帖子
3928

贵宾

3
发表于 2011-6-18 21:00:33 | 只看该作者
RMXP没有内置Mouse Module。
  1. #==============================================================================
  2. # ** Mouse Input Module (Revised)
  3. #------------------------------------------------------------------------------
  4. #   by DerVVulfman
  5. #   version 1.2
  6. #   08-18-2007
  7. #------------------------------------------------------------------------------
  8. #   Based on...
  9. #   Mouse Input Module
  10. #   by Near Fantastica
  11. #------------------------------------------------------------------------------
  12. #   Set_Pos feature by
  13. #   Freakboy
  14. #------------------------------------------------------------------------------
  15. #
  16. #   THE CALLS:
  17. #
  18. #   Mouse.click?
  19. #   This returns a true/false value  when you test whether a button is clicked.
  20. #   The values you pass are 1 (for the left mouse button), 2 (for the right) or
  21. #   3 (for the middle button).
  22. #
  23. #   Mouse.press?
  24. #   This returns a true/false value  when you test  whether a button is pressed
  25. #   and kept depressed.  The values you pass are 1 (for the left mouse button),
  26. #   2 (for the right mouse button), or 3 (for the middle).
  27. #
  28. #   Mouse.pixels
  29. #   This returns the  mouse's screen coordinates  in pixels.  Based on a screen
  30. #   with a 640x480 dimension,  this returns an array of the mouse's position in
  31. #   index values.  Calling Mouse.pixels returns both x & y positions  in a sin-
  32. #   gle string,  but calling Mouse.pixels[0] returns the x position (0-639) and
  33. #   calling Mouse.pixels[1]  returns  the y position (0-439).   If the mouse is
  34. #   outside of the game's window region, this call returns nil.
  35. #
  36. #   Mouse.tiles
  37. #   This returns  the mouse's screen  coordinates  in map tiles.   Based on the
  38. #   system's 20x15 tile size,  this returns it in index values  (a 0-19 width &
  39. #   a 0-14 height).  This functions the same manner as Mouse.pixels.
  40. #
  41. #   Mouse.set_pos
  42. #   This allows you  to forcefully position the mouse at an x/y position within
  43. #   the game screen by pixel coordinates.  Given the game's normal screen width
  44. #   of 640x480, adding:  Mouse.set_pos(320,240)  should position the mouse dead
  45. #   center of the gaming window.
  46. #
  47. #   Mouse.update
  48. #   Add this routine  into your update routines  to update  the mouse position.
  49. #   It must be called otherwise you won't get valid mouse coordinates.
  50. #==============================================================================

  51. module Mouse
  52.   #--------------------------------------------------------------------------
  53.   # * Mouse in_area
  54.   #--------------------------------------------------------------------------
  55.   def Mouse.area?(x, y, width=32, height=32)
  56.     return false if @pos == nil
  57.     return true if @pos[0] >= x and @pos[0] <= (x+width) and @pos[1] >= y and @pos[1] <= (y+height)
  58.     return false
  59.   end
  60.   #--------------------------------------------------------------------------
  61.   # * Mouse Pixel Position
  62.   #--------------------------------------------------------------------------
  63.   def Mouse.pixels
  64.     return @pos == nil ? [0, 0] : @pos
  65.   end
  66.   #--------------------------------------------------------------------------
  67.   # * Mouse Tile Position
  68.   #--------------------------------------------------------------------------
  69.   def Mouse.tiles
  70.     return nil if @pos == nil
  71.     x = @pos[0] / 32
  72.     y = @pos[1] / 32
  73.     return [x, y]
  74.   end
  75.   #--------------------------------------------------------------------------
  76.   # * Mouse Update
  77.   #--------------------------------------------------------------------------
  78.   def Mouse.update
  79.     @pos = Mouse.pos
  80.     ## 清空
  81.     @press,@toggled = 0,0
  82.     ## 状态记录
  83.     @press += LKEY   if GetKeyState.call(LKEY) & 0x8000 > 0
  84.     @press += RKEY   if GetKeyState.call(RKEY) & 0x8000 > 0
  85.     @press += MKEY   if GetKeyState.call(MKEY) & 0x8000 > 0
  86.     ## 触发记录
  87.     @toggled += LKEY if GetKeyState.call(LKEY) & 0x01 > 0
  88.     @toggled += RKEY if GetKeyState.call(RKEY) & 0x01 > 0
  89.     @toggled += MKEY if GetKeyState.call(MKEY) & 0x01 > 0
  90.     ## 单击记录
  91.     @clicks.shift if @clicks.size >=2
  92.     @clicks.push @press
  93.     ## 双击记录
  94.     if @dblc.size >= 2
  95.       @dblc.clear
  96.       @count = 0
  97.     end
  98.     if @dblc.size == 1 and @count - @dblc[0][1] > DBLCTIME
  99.       @dblc.clear
  100.       @count = 0
  101.     end
  102.     if @clicks[0] == 0 and @clicks[1] != 0
  103.       if @dblc.size == 1 and @dblc[0][0] != @clicks[1]
  104.         @dblc.clear
  105.         @count = 0
  106.       end
  107.       @dblc.push [@press,@count,@pos]
  108.     end
  109.     @count += 1 unless @dblc.empty?
  110.     ## 移动记录
  111.     @move_pos.shift if @move_pos.size >= 2
  112.     @move_pos.push @pos
  113.     ## 控制鼠标单击效果
  114.     $click_abled = true
  115.   end
  116.   #--------------------------------------------------------------------------
  117.   # * Automatic functions below
  118.   #--------------------------------------------------------------------------
  119.   #
  120.   #--------------------------------------------------------------------------
  121.   # * Obtain Mouse position in screen
  122.   #--------------------------------------------------------------------------
  123.   def Mouse.global_pos
  124.     pos = [0, 0].pack('ll')
  125.     ## 屏幕坐标
  126.     return GetCursorPos.call(pos) != 0 ? pos.unpack('ll') : nil
  127.   end
  128.   #--------------------------------------------------------------------------
  129.   # * Return Screen mouse position within game window
  130.   #--------------------------------------------------------------------------
  131.   def Mouse.pos
  132.     x, y = Mouse.screen_to_client(*Mouse.global_pos)
  133.     width, height = Mouse.client_size
  134.     begin
  135.       if (x >= 0 and y >= 0 and x < width and y < height)
  136.         return x, y
  137.       else
  138.         return nil
  139.       end
  140.     rescue
  141.       return nil
  142.     end
  143.   end
  144.   #--------------------------------------------------------------------------
  145.   #  * Pass Screen to Game System
  146.   #--------------------------------------------------------------------------
  147.   def Mouse.screen_to_client(x, y)
  148.     return nil unless x and y
  149.     pos = [x, y].pack('ll')
  150.     if ScreenToClient.call(HWND, pos) != 0
  151.       return pos.unpack('ll')
  152.     else
  153.       return nil
  154.     end
  155.   end
  156.   #--------------------------------------------------------------------------
  157.   # * Get Game Window Size
  158.   #--------------------------------------------------------------------------
  159.   def Mouse.client_size
  160.     rect = [0, 0, 0, 0].pack('l4')
  161.     GetClientRect.call(HWND, rect)
  162.     right, bottom = rect.unpack('l4')[2..3]
  163.     return right, bottom
  164.   end
  165.   #--------------------------------------------------------------------------
  166.   # * Get Window Position
  167.   #--------------------------------------------------------------------------
  168.   def Mouse.client_pos
  169.     rect = [0, 0, 0, 0].pack('l4')
  170.     GetWindowRect.call(HWND, rect)
  171.     left, upper = rect.unpack('l4')[0..1]
  172.     return left+4, upper+30
  173.   end
  174.   #--------------------------------------------------------------------------
  175.   # ## 设定鼠标坐标
  176.   #--------------------------------------------------------------------------
  177.   def self.set_pos(x = 0, y = 0)
  178.     width, height = Mouse.client_size
  179.     if (x.between?(0, width) && y.between?(0, height))
  180.       px = Mouse.client_pos[0] + x
  181.       py = Mouse.client_pos[1] + y
  182.       Win32API.new('user32', 'SetCursorPos', 'ii', 'l').call(px, py)
  183.     end
  184.   end
  185.   #--------------------------------------------------------------------------
  186.   # ## 触发?
  187.   #--------------------------------------------------------------------------
  188.   def self.toggled?(key)
  189.     return @toggled & key != 0
  190.   end
  191.   #--------------------------------------------------------------------------
  192.   # ## 按下?
  193.   #--------------------------------------------------------------------------
  194.   def Mouse.press?(key)
  195.     return @press & key != 0
  196.   end
  197.   #--------------------------------------------------------------------------
  198.   # ## 单击?
  199.   #--------------------------------------------------------------------------
  200.   def self.click?(key)
  201.     return false if key == LKEY and !$click_abled
  202.     return @clicks[0] & key == 0 && @clicks[1] & key != 0
  203.   end
  204.   #--------------------------------------------------------------------------
  205.   # ## 双击?
  206.   #--------------------------------------------------------------------------
  207.   def self.dbl_click?(key)
  208.     return false if @dblc.size < 2
  209.     ## 键值有效
  210.     if @dblc[1][0] & key != 0 and @dblc[0][0] & key != 0
  211.       ## 间隔有效
  212.       if @dblc[1][1] - @dblc[0][1] < DBLCTIME
  213.         ## 坐标有效
  214.         if @dblc[1][2] == @dblc[0][2]
  215.           return @clicks[0] & key == 0 && @clicks[1] & key != 0
  216.         end
  217.       end
  218.     end
  219.     return false
  220.   end
  221.   #--------------------------------------------------------------------------
  222.   # ## 经过?
  223.   #     obj       : 对象实例
  224.   #                 **支持Sprite,Window,Viewport,Rect,Plane,可补充
  225.   #--------------------------------------------------------------------------
  226.   def Mouse.over?(obj)
  227.     case obj
  228.     when Sprite
  229.       x = obj.x-obj.ox; y = obj.y-obj.oy
  230.       unless obj.viewport.nil?
  231.         x += obj.viewport.rect.x
  232.         y += obj.viewport.rect.y
  233.       end
  234.       return Mouse.area?(x, y, obj.width, obj.height)
  235.     when Window
  236.       x = obj.x;y = obj.y
  237.       unless obj.viewport.nil?
  238.         x += obj.viewport.rect.x
  239.         y += obj.viewport.rect.y
  240.       end
  241.       return Mouse.area?(x, y, obj.width, obj.height)
  242.     when Viewport
  243.       return Mouse.area?(obj.rect.x, obj.rect.y, obj.rect.width, obj.rect.height)
  244.     when Rect
  245.       return Mouse.area?(obj.x, obj.y, obj.width, obj.height)
  246.     when Plane
  247.       x = obj.x;y = obj.y
  248.       unless obj.viewport.nil?
  249.         x += obj.viewport.rect.x
  250.         y += obj.viewport.rect.y
  251.       end
  252.       return Mouse.area?(x, y, obj.width, obj.height)
  253.     end
  254.   end
  255.   #--------------------------------------------------------------------------
  256.   # ## 移动?
  257.   #--------------------------------------------------------------------------
  258.   def self.move?
  259.     return false if @move_pos.size < 2
  260.     return @move_pos[1] != @move_pos[0]
  261.   end
  262.   #--------------------------------------------------------------------------
  263.   # ## 滚轮
  264.   #--------------------------------------------------------------------------
  265.   def self.scroll
  266.     msg = "\0"*32
  267.     PeekMessage.call(msg,0,0,0,PM_NOREMOVE)
  268.     r = unpack_msg(msg)
  269.     return unless r[0] == WM_MOUSEWHEEL
  270.     return word2signed_short(hiword(r[1]))
  271.   end
  272.   #--------------------------------------------------------------------------
  273.   # ## 窗体信息
  274.   #--------------------------------------------------------------------------
  275.   def self.unpack_msg(buffer)
  276.     msg = []
  277.     msg << unpack_dword(buffer, 4*1)
  278.     msg << unpack_dword(buffer, 4*2)
  279.     return msg
  280.   end
  281.   #--------------------------------------------------------------------------
  282.   # ## 转带符号整型
  283.   #--------------------------------------------------------------------------
  284.   def self.word2signed_short(value)
  285.     return value if (value & 0x8000)==0
  286.     return -1*((~value&0x7fff) + 1)
  287.   end
  288.   #--------------------------------------------------------------------------
  289.   # ## 低位
  290.   #--------------------------------------------------------------------------
  291.   def self.loword(dword)
  292.     return dword & 0x0000ffff
  293.   end
  294.   #--------------------------------------------------------------------------
  295.   # ## 高位
  296.   #--------------------------------------------------------------------------
  297.   def self.hiword(dword)
  298.     return (dword & 0xffff0000) / 0x10000
  299.   end
  300.   #--------------------------------------------------------------------------
  301.   # ## 双字节
  302.   #--------------------------------------------------------------------------
  303.   def self.unpack_dword(buffer, offset=0)
  304.     ret=buffer[offset+0]&0x000000ff
  305.     ret|=(buffer[offset+1]<<(8*1)) & 0x0000ff00
  306.     ret|=(buffer[offset+2]<<(8*2)) & 0x00ff0000
  307.     ret|=(buffer[offset+3]<<(8*3)) & 0xff000000
  308.     return ret
  309.   end
  310.   #--------------------------------------------------------------------------
  311.   # ## 常量
  312.   #--------------------------------------------------------------------------
  313.   GetKeyState      = Win32API.new('user32','GetKeyState',['i'],'i')
  314.   ScreenToClient   = Win32API.new('user32', 'ScreenToClient', 'lp', 'i')
  315.   GetCursorPos     = Win32API.new('user32', 'GetCursorPos', 'p', 'i')
  316.   GetWindowRect    = Win32API.new('user32', 'GetWindowRect', 'lp', 'i')
  317.   GetClientRect    = Win32API.new('user32', 'GetClientRect', 'lp', 'i')
  318.   GetDblClickTime  = Win32API.new('user32','GetDoubleClickTime','','l')
  319.   PeekMessage      = Win32API.new('user32','PeekMessage','pllll','l')
  320.   
  321.   WM_MOUSEWHEEL    = 0x20A
  322.   PM_NOREMOVE      = 0x0
  323.   
  324.   HWND             = get_hwnd
  325.   LKEY,RKEY,MKEY   = 1,2,4
  326.   DBLCTIME         = (GetDblClickTime.call * 60.0 / 1000).round
  327.   #--------------------------------------------------------------------------
  328.   # ## 内部变量
  329.   #--------------------------------------------------------------------------
  330.   @count    = 0     ## 双击时间计数
  331.   @dblc     = []    ## 双击记录[key,wait,[mouse_x,mouse_y]]
  332.   @move_pos = []    ## 移动记录
  333.   @clicks   = []    ## 单击记录
  334. end
复制代码
这其实才是好脚本,比那些站上流传的鼠标脚本好多了。
不过……要自己写图标刷新。

点评

第325行发生了Error- -  发表于 2011-6-18 21:02
回复

使用道具 举报

Lv1.梦旅人

梦石
0
星屑
50
在线时间
27 小时
注册时间
2010-7-21
帖子
27
4
 楼主| 发表于 2011-6-18 21:24:13 | 只看该作者
为什么第325行会ERROR啊
55555555555555555555
回复

使用道具 举报

头像被屏蔽

Lv1.梦旅人 (禁止发言)

梦石
0
星屑
50
在线时间
0 小时
注册时间
2011-6-19
帖子
5
5
发表于 2011-6-19 10:20:12 | 只看该作者
提示: 作者被禁止或删除 内容自动屏蔽
签名被屏蔽
回复

使用道具 举报

Lv2.观梦者

虚構歪曲

梦石
0
星屑
364
在线时间
1198 小时
注册时间
2010-12-18
帖子
3928

贵宾

6
发表于 2011-6-20 09:58:33 | 只看该作者
  1. #==============================================================================
  2. # [XP/VX] 精确获取窗口句柄 by 紫苏
  3. #==============================================================================
  4. # ■ Kernel
  5. #==============================================================================
  6. module Kernel
  7.   #--------------------------------------------------------------------------
  8.   # ● 需要的 Windows API 函数
  9.   #--------------------------------------------------------------------------
  10.   GetWindowThreadProcessId = Win32API.new("user32", "GetWindowThreadProcessId", "LP", "L")
  11.   GetWindow = Win32API.new("user32", "GetWindow", "LL", "L")
  12.   GetClassName = Win32API.new("user32", "GetClassName", "LPL", "L")
  13.   GetCurrentThreadId = Win32API.new("kernel32", "GetCurrentThreadId", "V", "L")
  14.   GetForegroundWindow = Win32API.new("user32", "GetForegroundWindow", "V", "L")
  15.   #--------------------------------------------------------------------------
  16.   # ● 获取窗口句柄
  17.   #--------------------------------------------------------------------------
  18.   def get_hwnd
  19.     # 获取调用线程(RM 的主线程)的进程标识
  20.     threadID = GetCurrentThreadId.call
  21.     # 获取 Z 次序中最靠前的窗口
  22.     hWnd = GetWindow.call(GetForegroundWindow.call, 0)
  23.     # 枚举所有窗口
  24.     while hWnd != 0
  25.       # 如果创建该窗口的线程标识匹配本线程标识
  26.       if threadID == GetWindowThreadProcessId.call(hWnd, 0)
  27.         # 分配一个 11 个字节的缓冲区
  28.         className = " " * 11
  29.         # 获取该窗口的类名
  30.         GetClassName.call(hWnd, className, 12)
  31.         # 如果匹配 RGSS Player 则跳出循环
  32.         break if className == "RGSS Player"
  33.       end
  34.       # 获取下一个窗口
  35.       hWnd = GetWindow.call(hWnd, 2)
  36.     end
  37.     return hWnd
  38.   end
  39. end

  40. #==============================================================================
  41. # ** Mouse Input Module (Revised)
  42. #------------------------------------------------------------------------------
  43. #   by DerVVulfman
  44. #   version 1.2
  45. #   08-18-2007
  46. #------------------------------------------------------------------------------
  47. #   Based on...
  48. #   Mouse Input Module
  49. #   by Near Fantastica
  50. #------------------------------------------------------------------------------
  51. #   Set_Pos feature by
  52. #   Freakboy
  53. #------------------------------------------------------------------------------
  54. #
  55. #   THE CALLS:
  56. #
  57. #   Mouse.click?
  58. #   This returns a true/false value  when you test whether a button is clicked.
  59. #   The values you pass are 1 (for the left mouse button), 2 (for the right) or
  60. #   3 (for the middle button).
  61. #
  62. #   Mouse.press?
  63. #   This returns a true/false value  when you test  whether a button is pressed
  64. #   and kept depressed.  The values you pass are 1 (for the left mouse button),
  65. #   2 (for the right mouse button), or 3 (for the middle).
  66. #
  67. #   Mouse.pixels
  68. #   This returns the  mouse's screen coordinates  in pixels.  Based on a screen
  69. #   with a 640x480 dimension,  this returns an array of the mouse's position in
  70. #   index values.  Calling Mouse.pixels returns both x & y positions  in a sin-
  71. #   gle string,  but calling Mouse.pixels[0] returns the x position (0-639) and
  72. #   calling Mouse.pixels[1]  returns  the y position (0-439).   If the mouse is
  73. #   outside of the game's window region, this call returns nil.
  74. #
  75. #   Mouse.tiles
  76. #   This returns  the mouse's screen  coordinates  in map tiles.   Based on the
  77. #   system's 20x15 tile size,  this returns it in index values  (a 0-19 width &
  78. #   a 0-14 height).  This functions the same manner as Mouse.pixels.
  79. #
  80. #   Mouse.set_pos
  81. #   This allows you  to forcefully position the mouse at an x/y position within
  82. #   the game screen by pixel coordinates.  Given the game's normal screen width
  83. #   of 640x480, adding:  Mouse.set_pos(320,240)  should position the mouse dead
  84. #   center of the gaming window.
  85. #
  86. #   Mouse.update
  87. #   Add this routine  into your update routines  to update  the mouse position.
  88. #   It must be called otherwise you won't get valid mouse coordinates.
  89. #==============================================================================

  90. module Mouse
  91.   #--------------------------------------------------------------------------
  92.   # * Mouse in_area
  93.   #--------------------------------------------------------------------------
  94.   def Mouse.area?(x, y, width=32, height=32)
  95.     return false if @pos == nil
  96.     return true if @pos[0] >= x and @pos[0] <= (x+width) and @pos[1] >= y and @pos[1] <= (y+height)
  97.     return false
  98.   end
  99.   #--------------------------------------------------------------------------
  100.   # * Mouse Pixel Position
  101.   #--------------------------------------------------------------------------
  102.   def Mouse.pixels
  103.     return @pos == nil ? [0, 0] : @pos
  104.   end
  105.   #--------------------------------------------------------------------------
  106.   # * Mouse Tile Position
  107.   #--------------------------------------------------------------------------
  108.   def Mouse.tiles
  109.     return nil if @pos == nil
  110.     x = @pos[0] / 32
  111.     y = @pos[1] / 32
  112.     return [x, y]
  113.   end
  114.   #--------------------------------------------------------------------------
  115.   # * Mouse Update
  116.   #--------------------------------------------------------------------------
  117.   def Mouse.update
  118.     @pos = Mouse.pos
  119.     ## 清空
  120.     @press,@toggled = 0,0
  121.     ## 状态记录
  122.     @press += LKEY   if GetKeyState.call(LKEY) & 0x8000 > 0
  123.     @press += RKEY   if GetKeyState.call(RKEY) & 0x8000 > 0
  124.     @press += MKEY   if GetKeyState.call(MKEY) & 0x8000 > 0
  125.     ## 触发记录
  126.     @toggled += LKEY if GetKeyState.call(LKEY) & 0x01 > 0
  127.     @toggled += RKEY if GetKeyState.call(RKEY) & 0x01 > 0
  128.     @toggled += MKEY if GetKeyState.call(MKEY) & 0x01 > 0
  129.     ## 单击记录
  130.     @clicks.shift if @clicks.size >=2
  131.     @clicks.push @press
  132.     ## 双击记录
  133.     if @dblc.size >= 2
  134.       @dblc.clear
  135.       @count = 0
  136.     end
  137.     if @dblc.size == 1 and @count - @dblc[0][1] > DBLCTIME
  138.       @dblc.clear
  139.       @count = 0
  140.     end
  141.     if @clicks[0] == 0 and @clicks[1] != 0
  142.       if @dblc.size == 1 and @dblc[0][0] != @clicks[1]
  143.         @dblc.clear
  144.         @count = 0
  145.       end
  146.       @dblc.push [@press,@count,@pos]
  147.     end
  148.     @count += 1 unless @dblc.empty?
  149.     ## 移动记录
  150.     @move_pos.shift if @move_pos.size >= 2
  151.     @move_pos.push @pos
  152.     ## 控制鼠标单击效果
  153.     $click_abled = true
  154.   end
  155.   #--------------------------------------------------------------------------
  156.   # * Automatic functions below
  157.   #--------------------------------------------------------------------------
  158.   #
  159.   #--------------------------------------------------------------------------
  160.   # * Obtain Mouse position in screen
  161.   #--------------------------------------------------------------------------
  162.   def Mouse.global_pos
  163.     pos = [0, 0].pack('ll')
  164.     ## 屏幕坐标
  165.     return GetCursorPos.call(pos) != 0 ? pos.unpack('ll') : nil
  166.   end
  167.   #--------------------------------------------------------------------------
  168.   # * Return Screen mouse position within game window
  169.   #--------------------------------------------------------------------------
  170.   def Mouse.pos
  171.     x, y = Mouse.screen_to_client(*Mouse.global_pos)
  172.     width, height = Mouse.client_size
  173.     begin
  174.       if (x >= 0 and y >= 0 and x < width and y < height)
  175.         return x, y
  176.       else
  177.         return nil
  178.       end
  179.     rescue
  180.       return nil
  181.     end
  182.   end
  183.   #--------------------------------------------------------------------------
  184.   #  * Pass Screen to Game System
  185.   #--------------------------------------------------------------------------
  186.   def Mouse.screen_to_client(x, y)
  187.     return nil unless x and y
  188.     pos = [x, y].pack('ll')
  189.     if ScreenToClient.call(HWND, pos) != 0
  190.       return pos.unpack('ll')
  191.     else
  192.       return nil
  193.     end
  194.   end
  195.   #--------------------------------------------------------------------------
  196.   # * Get Game Window Size
  197.   #--------------------------------------------------------------------------
  198.   def Mouse.client_size
  199.     rect = [0, 0, 0, 0].pack('l4')
  200.     GetClientRect.call(HWND, rect)
  201.     right, bottom = rect.unpack('l4')[2..3]
  202.     return right, bottom
  203.   end
  204.   #--------------------------------------------------------------------------
  205.   # * Get Window Position
  206.   #--------------------------------------------------------------------------
  207.   def Mouse.client_pos
  208.     rect = [0, 0, 0, 0].pack('l4')
  209.     GetWindowRect.call(HWND, rect)
  210.     left, upper = rect.unpack('l4')[0..1]
  211.     return left+4, upper+30
  212.   end
  213.   #--------------------------------------------------------------------------
  214.   # ## 设定鼠标坐标
  215.   #--------------------------------------------------------------------------
  216.   def self.set_pos(x = 0, y = 0)
  217.     width, height = Mouse.client_size
  218.     if (x.between?(0, width) && y.between?(0, height))
  219.       px = Mouse.client_pos[0] + x
  220.       py = Mouse.client_pos[1] + y
  221.       Win32API.new('user32', 'SetCursorPos', 'ii', 'l').call(px, py)
  222.     end
  223.   end
  224.   #--------------------------------------------------------------------------
  225.   # ## 触发?
  226.   #--------------------------------------------------------------------------
  227.   def self.toggled?(key)
  228.     return @toggled & key != 0
  229.   end
  230.   #--------------------------------------------------------------------------
  231.   # ## 按下?
  232.   #--------------------------------------------------------------------------
  233.   def Mouse.press?(key)
  234.     return @press & key != 0
  235.   end
  236.   #--------------------------------------------------------------------------
  237.   # ## 单击?
  238.   #--------------------------------------------------------------------------
  239.   def self.click?(key)
  240.     return false if key == LKEY and !$click_abled
  241.     return @clicks[0] & key == 0 && @clicks[1] & key != 0
  242.   end
  243.   #--------------------------------------------------------------------------
  244.   # ## 双击?
  245.   #--------------------------------------------------------------------------
  246.   def self.dbl_click?(key)
  247.     return false if @dblc.size < 2
  248.     ## 键值有效
  249.     if @dblc[1][0] & key != 0 and @dblc[0][0] & key != 0
  250.       ## 间隔有效
  251.       if @dblc[1][1] - @dblc[0][1] < DBLCTIME
  252.         ## 坐标有效
  253.         if @dblc[1][2] == @dblc[0][2]
  254.           return @clicks[0] & key == 0 && @clicks[1] & key != 0
  255.         end
  256.       end
  257.     end
  258.     return false
  259.   end
  260.   #--------------------------------------------------------------------------
  261.   # ## 经过?
  262.   #     obj       : 对象实例
  263.   #                 **支持Sprite,Window,Viewport,Rect,Plane,可补充
  264.   #--------------------------------------------------------------------------
  265.   def Mouse.over?(obj)
  266.     case obj
  267.     when Sprite
  268.       x = obj.x-obj.ox; y = obj.y-obj.oy
  269.       unless obj.viewport.nil?
  270.         x += obj.viewport.rect.x
  271.         y += obj.viewport.rect.y
  272.       end
  273.       return Mouse.area?(x, y, obj.width, obj.height)
  274.     when Window
  275.       x = obj.x;y = obj.y
  276.       unless obj.viewport.nil?
  277.         x += obj.viewport.rect.x
  278.         y += obj.viewport.rect.y
  279.       end
  280.       return Mouse.area?(x, y, obj.width, obj.height)
  281.     when Viewport
  282.       return Mouse.area?(obj.rect.x, obj.rect.y, obj.rect.width, obj.rect.height)
  283.     when Rect
  284.       return Mouse.area?(obj.x, obj.y, obj.width, obj.height)
  285.     when Plane
  286.       x = obj.x;y = obj.y
  287.       unless obj.viewport.nil?
  288.         x += obj.viewport.rect.x
  289.         y += obj.viewport.rect.y
  290.       end
  291.       return Mouse.area?(x, y, obj.width, obj.height)
  292.     end
  293.   end
  294.   #--------------------------------------------------------------------------
  295.   # ## 移动?
  296.   #--------------------------------------------------------------------------
  297.   def self.move?
  298.     return false if @move_pos.size < 2
  299.     return @move_pos[1] != @move_pos[0]
  300.   end
  301.   #--------------------------------------------------------------------------
  302.   # ## 滚轮
  303.   #--------------------------------------------------------------------------
  304.   def self.scroll
  305.     msg = "\0"*32
  306.     PeekMessage.call(msg,0,0,0,PM_NOREMOVE)
  307.     r = unpack_msg(msg)
  308.     return unless r[0] == WM_MOUSEWHEEL
  309.     return word2signed_short(hiword(r[1]))
  310.   end
  311.   #--------------------------------------------------------------------------
  312.   # ## 窗体信息
  313.   #--------------------------------------------------------------------------
  314.   def self.unpack_msg(buffer)
  315.     msg = []
  316.     msg << unpack_dword(buffer, 4*1)
  317.     msg << unpack_dword(buffer, 4*2)
  318.     return msg
  319.   end
  320.   #--------------------------------------------------------------------------
  321.   # ## 转带符号整型
  322.   #--------------------------------------------------------------------------
  323.   def self.word2signed_short(value)
  324.     return value if (value & 0x8000)==0
  325.     return -1*((~value&0x7fff) + 1)
  326.   end
  327.   #--------------------------------------------------------------------------
  328.   # ## 低位
  329.   #--------------------------------------------------------------------------
  330.   def self.loword(dword)
  331.     return dword & 0x0000ffff
  332.   end
  333.   #--------------------------------------------------------------------------
  334.   # ## 高位
  335.   #--------------------------------------------------------------------------
  336.   def self.hiword(dword)
  337.     return (dword & 0xffff0000) / 0x10000
  338.   end
  339.   #--------------------------------------------------------------------------
  340.   # ## 双字节
  341.   #--------------------------------------------------------------------------
  342.   def self.unpack_dword(buffer, offset=0)
  343.     ret=buffer[offset+0]&0x000000ff
  344.     ret|=(buffer[offset+1]<<(8*1)) & 0x0000ff00
  345.     ret|=(buffer[offset+2]<<(8*2)) & 0x00ff0000
  346.     ret|=(buffer[offset+3]<<(8*3)) & 0xff000000
  347.     return ret
  348.   end
  349.   #--------------------------------------------------------------------------
  350.   # ## 常量
  351.   #--------------------------------------------------------------------------
  352.   GetKeyState      = Win32API.new('user32','GetKeyState',['i'],'i')
  353.   ScreenToClient   = Win32API.new('user32', 'ScreenToClient', 'lp', 'i')
  354.   GetCursorPos     = Win32API.new('user32', 'GetCursorPos', 'p', 'i')
  355.   GetWindowRect    = Win32API.new('user32', 'GetWindowRect', 'lp', 'i')
  356.   GetClientRect    = Win32API.new('user32', 'GetClientRect', 'lp', 'i')
  357.   GetDblClickTime  = Win32API.new('user32','GetDoubleClickTime','','l')
  358.   PeekMessage      = Win32API.new('user32','PeekMessage','pllll','l')
  359.   
  360.   WM_MOUSEWHEEL    = 0x20A
  361.   PM_NOREMOVE      = 0x0
  362.   
  363.   HWND             = get_hwnd
  364.   LKEY,RKEY,MKEY   = 1,2,4
  365.   DBLCTIME         = (GetDblClickTime.call * 60.0 / 1000).round
  366.   #--------------------------------------------------------------------------
  367.   # ## 内部变量
  368.   #--------------------------------------------------------------------------
  369.   @count    = 0     ## 双击时间计数
  370.   @dblc     = []    ## 双击记录[key,wait,[mouse_x,mouse_y]]
  371.   @move_pos = []    ## 移动记录
  372.   @clicks   = []    ## 单击记录
  373. end
复制代码
- -,原来这货还带着精确获取。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册会员

本版积分规则

拿上你的纸笔,建造一个属于你的梦想世界,加入吧。
 注册会员
找回密码

站长信箱:[email protected]|手机版|小黑屋|无图版|Project1游戏制作

GMT+8, 2024-11-26 05:52

Powered by Discuz! X3.1

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表