Skip to content

GETMOUSE


获取鼠标指针设备的状态

语法

vb
declare function Getmouse ( byref x as long, byref y as long, byref wheel as long = 0, byref buttons as long = 0, byref clip as long = 0 ) as long
declare function Getmouse ( byref x as longint, byref y as longint, byref wheel as longint = 0, byref buttons as longint = 0, byref clip as longint = 0 ) as long

用法

result = Getmouse (x, y [, [ wheel ] [, [ buttons ] [, [ clip ]]]])

参数

x

x坐标值

y

y坐标值

wheel

滚轮值

buttons

按钮状态

clip

裁剪状态

返回值

成功时返回 0,出错时(例如鼠标在图形窗口外)或失败时返回 1(设置运行时错误)

说明

Getmouse 获取鼠标位置和按钮状态;信息通过引用传递给此函数的变量返回。若鼠标不可用,所有变量将包含 -1 值。

在控制台模式下,xy 坐标是鼠标当前所在的字符单元格坐标;屏幕左上角坐标为 0, 0。若鼠标移出控制台窗口,Getmouse 返回鼠标最后停留在窗口上的坐标。在控制台全屏模式下,滚轮值不会被返回。

在图形模式下,xy 始终以像素坐标返回,仍然相对于屏幕左上角(此时为0,0);通过 ViewWindow 设置的自定义坐标系不影响 Getmouse 返回的坐标。

若鼠标离开图形窗口,所有值设为 -1,函数返回值设为 1。若不同时检测函数返回值,可能会对按钮和滚轮值产生误判。

Wheel 是鼠标滚轮计数器;向前滚动滚轮使计数增加,向后滚动使计数减少。程序启动时或通过 Screen (Graphics) 设置新图形模式时,滚轮位置重置为0。FreeBASIC在特定平台上可能并不总是支持鼠标滚轮,此时始终返回0。

Buttons 以位掩码形式存储按钮状态:若鼠标左键按下则第0位置位;若右键按下则第1位置位;若中键/滚轮按下则第2位置位。

Clip 存储鼠标裁剪状态;若为 1,鼠标当前被裁剪到图形窗口内;若为 0,鼠标未被裁剪。

Getmouse 返回的错误码可以在下一行用 Err 检查。函数版本的 Getmouse 直接以32位 Long 返回错误码。

警告:当设置了 GFX_SHAPED_WINDOW 标志时(参见 Screenres),Getmouse 只在彩色区域内有效,即颜色 <> RGBA(255, 0, 255, alpha) 的区域。

示例

start GeSHi

vb
Dim As Long x, y, buttons, res
' Set video mode and enter loop
ScreenRes 640, 480, 8
Do
    ' Get mouse x, y and buttons. Discard wheel position.
    res = GetMouse (x, y, , buttons)
    Locate 1, 1
    If res <> 0 Then '' Failure

#IFDEF __FB_DOS__
        Print "Mouse or mouse driver not available"
#ELSE
        Print "Mouse not available or not on window"
#ENDIF

    Else
        Print Using "Mouse position: ###:###  Buttons: "; x; y;
        If buttons And 1 Then Print "L";
        If buttons And 2 Then Print "R";
        If buttons And 4 Then Print "M";
        Print "   "
    End If
Loop While Inkey = ""
End

end GeSHi

start GeSHi

vb
'Example 2: type-union-type structure
Type mouse
    As Long res
    As Long x, y, wheel, clip
    Union
        buttons As Long
        Type
            Left:1 As Long
            Right:1 As Long
            middle:1 As Long
        End Type
    End Union
End Type
 
Screen 11
Dim As mouse m

Do
    m.res = GetMouse( m.x, m.y, m.wheel, m.buttons, m.clip )
    ScreenLock
    Cls
    Print Using "res = #"; m.res
    Print Using "x = ###; y = ###; wheel = +###; clip = ##"; m.x; m.y; m.wheel; m.clip
    Print Using "buttons = ##; left = #; middle = #; right = #"; m.buttons; m.Left; m.middle; m.Right
    ScreenUnlock
    Sleep 10, 1
Loop While Inkey = ""

end GeSHi

方言差异

  • -lang qb 方言中不可用,除非使用别名 __Getmouse 引用。传递的变量类型也必须为 Long 而非 Integer

平台差异

  • 在 Win32 上,全屏控制台模式下滚轮变化不保证被检测到。WinXP 确认:窗口模式下可检测到鼠标滚轮变化,但全屏控制台模式下不能。
  • 在 DOS 中,"clip"值无意义。此外,除非鼠标驱动程序支持并启用,否则滚轮和中键将不起作用。另参见 FaqDOS

与 QB 的差异

  • FreeBASIC 新增

另请参阅

  • Screenres 通过分辨率设置图形模式
  • Screen (Graphics) QB风格的模式设置
  • Setmouse
  • Multikey
  • Getjoystick
  • Event
  • ScreenEvent

返回 目录

基于 FreeBASIC 官方文档翻译 如有侵权请联系我们删除
FreeBASIC 是开源项目,与微软公司无隶属关系