''程序说明:
'本例演示了怎样移动无标题栏的窗体的方法,程序中涉
'及到mousedown、mousemove、mouseup三个鼠标事件的
'使用,我们可以从中学习到它们的用法。
'---------------------------------------------
option explicit
'变量声明
'movescreen,布尔型变量,标示窗体是否处于被移动状态
dim movescreen as boolean
'鼠标位置
dim mousx as integer
dim mousy as integer
'窗体位置
dim currx as integer
dim curry as integer
'“退出”按钮
private sub cmdexit_click()
end
end sub
'当鼠标在窗体上按下时
private sub form_mousedown(button as integer, shift as integer, x as single, y as single)
'---------------------------------------------
'参数说明:
'---------------------------------------------
'button 返回一个整数,用来标识按下或释放的是哪一
'个按钮。button 参数的值为相应于左按钮(1)右按钮
'(2),以及中间按钮(4)。
'---------------------------------------------
'shift 返回一个整数,在鼠标按钮被按下或者被释放
'的同时,shift,ctrl,和 alt 键的状态,返回的shift
'参数值分别为1,2,和 4。指示这些键的状态。
'---------------------------------------------
'x, y 返回一个指定鼠标指针当前位置的数。
'---------------------------------------------
'如果是鼠标左键按下
if button = 1 then
'标示为移动状态
movescreen = true
'得到鼠标在窗体上的位置(相对与窗体内部坐标)
mousx = x
mousy = y
end if
end sub
'当鼠标在窗体上移过时
private sub form_mousemove(button as integer, shift as integer, x as single, y as single)
'如果处于鼠标左键按下的状态,即movescreen = true时
if movescreen then
'计算新的窗体坐标值
'仔细想一下,看看是不是这样
currx = form1.left - mousx + x
curry = form1.top - mousy + y
'移动窗体到新的位置
form1.move currx, curry
end if
'把新的窗体坐标显示出来,是相对于屏幕的坐标
label1.caption = currx
label2.caption = curry
'把鼠标点击的位置显示出来,是相对与窗体的坐标
label3.caption = mousx
label4.caption = mousy
end sub
'如果鼠标松开,则停止拖动
private sub form_mouseup(button as integer, shift as integer, x as single, y as single)
movescreen = false
end sub