设为首页设为首页
 添加收藏添加收藏
 进入音乐版音 乐 版
  汉南在线程序设计VB程序

怎样限制鼠标移动
作  者:匿名
关键字:VisualBasic实例



本文介绍如何限制鼠标在窗口的指定范围内移动。这个技术在需要防止用户鼠标在指定区域内活动时非常
有用。例如在一个射击游戏中,需要限制鼠标在射击区内移动。
操作步骤
1、建立一个新工程项目,缺省建立窗体FORM1
2、添加一个新模体
3、粘贴下面代码到新模体

OptionExplicitDeclareFunctionClipCursorLib"user32"(lpRectAsAny)AsLong
DeclareFunctionClipCursorClearLib"user32"Alias"ClipCursor"(ByVallpRectAsLong)AsLong
DeclareFunctionClientToScreenLib"user32"(ByValhwndAsLong,lpPointAsPOINTAPI)AsLong
TypeRECT
LeftAsLong
TopAsLong
RightAsLong
BottomAsLong
EndType
TypePOINTAPI
XAsLong
YAsLong
EndType
PublicRetValueAsLong
PublicClipModeAsBoolean

PublicSubSetCursor(ClipObjectAsObject,SettingAsBoolean)
'usedtoclipthecursorintotheviewportand
'turnoffthedefaultwindowscursor

DimCurrentPointAsPOINTAPI
DimClipRectAsRECT

IfSetting=FalseThen
'setclipstatebacktonormal
RetValue=ClipCursorClear(0)
ExitSub
EndIf

'setcurrentposition
WithCurrentPoint
.X=0
.Y=0
EndWith
'findpositiononthescreen(notthewindow)
RetValue=ClientToScreen(ClipObject.hwnd,CurrentPoint)
'designatecliparea
WithClipRect
.Top=CurrentPoint.Y
.Left=CurrentPoint.X
.Right=.Left ClipObject.ScaleWidth
.Bottom=.Top ClipObject.ScaleHeight
EndWith'clipit
RetValue=ClipCursor(ClipRect)
EndSub

4、添加一个图片框控件(PICTURE1)到窗体(FORM1)
5、设置PICTURE1的尺寸和FORM1的一样大
6、在PICTURE1的CLICK事件中添加以下代码:

PrivateSubPicture1_Click()
ClipMode=NotClipMode
SetCursorPicture1,ClipMode
EndSub

7、保存工程项目
8、运行程序。在图片框单击鼠标,鼠标将被包含在图片框控件的区域内。要释放限制状态只需再次单击鼠标。
注意:如果释放限制状态失败,鼠标将被永久限制,只能用重新启动机器来解决。
另一个限制鼠标活动范围的方法是关闭鼠标,用其他图象代替光标,例如手枪。->



来源:网络
阅读:602
日期:2008-1-2

【 双击滚屏 】 【 收藏 】 【 打印 】 【 关闭 】 【 字体: 】 
上一篇:VisualBasic菜单在应用程序中的使用
下一篇:解除网虫心病VB做定时断线程序

  >> 相关文章
 
  ·VisualBasic菜单在应用程序中的使用
  ·利用API函数实现隐藏和显示桌面图标
  ·用VB实现“ICQ”式的启动欢迎画面
  ·再谈VB程序的加密
  ·多用户应用程序中应注意的其它问题(1)--多客户环境下VB数据库编程之(8)
  ·VB5实现窗口图像缩放、滚动技巧
  ·用VisualBasic自编小游戏
  ·用VB学做“黑客”程序
授权使用:汉南在线 http://www.hzwz.net/(2008-2009)   
Copyright (c) 2002-2007 汉南在线. All Rights Reserved . 
经营许可证:陕ICP备05000109号 Powered by:汉南在线