您的位置:逆风者 VB 正文
原作者:www.upwinder.com 添加时间:2007-09-02 原文发表:2007-08-31 人气:3 来源:未知

一.通过鼠标在屏幕上的移动来控件程序界面

本例通过鼠标在屏幕上的移动来控制程序窗体的显示与隐藏:当鼠标移动到窗体所在区域时窗体显示,反之隐藏起来。仅需一条API函数:GetCursorPos。注意:如果需要将API函数置于模块中请对代码作相应修改。要尝试本例,需给标准EXE工程缺省添加一个Timer控件。
逆风编程技术

Private Type POINTAPI
x As Long
y As Long
End Type

Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long

Private Sub Form_Load()
Me.Visible = False
Timer1.Enabled = True
Timer1.Interval = 100
End Sub

Private Sub Timer1_Timer()
Dim lResult As Long
Dim lpPoint As POINTAPI
Dim iCounter As Integer
lResult = GetCursorPos(lpPoint)
If lpPoint.x < Me.Left \ Screen.TwipsPerPixelX Or lpPoint.x > (Me.Left _
Me.Width) \ Screen.TwipsPerPixelX Or lpPoint.y < Me.Top \ _
Screen.TwipsPerPixelY Or lpPoint.y - 10 > (Me.Top Me.Height) \ _
Screen.TwipsPerPixelY Then
Me.Visible = False '鼠标在窗体区域之外时
Else
Me.Visible = True '鼠标在窗体区域之内时
End If
End Sub

二.获得Mouse_Exit事件

所谓Mouse_Exit事件,是指鼠标指针离开某一控件所应发生的事件。本例是通过Form_MouseMove事件来判断鼠标指针是在窗体之内还是窗体之外的,你可根据需要作相应改动。请给窗体缺省创建一个按钮(用于观察效果)。

Private Declare Function SetCapture Lib "user32" (ByVal hWnd As Long) As Long
Private Declare Function ReleaseCapture Lib "user32" () As Long

Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
Dim MouseExit As Boolean
MouseExit = (0 <= X) And (X <= Me.Width) And (0 <= Y) And (Y <= Me.Height)
If MouseExit Then
Me.Caption = "鼠标指针在窗体范围内"
Command1.Enabled = True
SetCapture Me.hWnd
Else
Me.Caption = "鼠标指针在窗体范围外"
Command1.Enabled = False
ReleaseCapture
End If
End Sub

相关文章

使用Shell指令具有Wait的功能
用RND()函数加密
利用VB6.0实现五线谱作曲工具
在VB5中生成统计图形
如何使用文件复制对话框
VB技巧-工具栏使用技巧
用类来编写数据库程序
VB从图片框控件取得颜色
在VB6中将XML传入一个TreeView控件
移除字串中不要的字符
如何在Windows操作系统中改变文件打开方式
怎样存取注册表信息
Visual baisc中Byval与Byref的区别
用VB实现屏幕滚屏保护效果
如何用TextBox打开和保存文件
用SendMessage实现剪贴板操作
VB“变态”用法之有用技术和没用指针
让一个 App 永远保持在最上层
在VB中用文件映射来进行进程通讯
用API函数定义热键又一法

相关评论


本文章所属分类:首页 VB
 

  热门关键字: