一.通过鼠标在屏幕上的移动来控件程序界面
本例通过鼠标在屏幕上的移动来控制程序窗体的显示与隐藏:当鼠标移动到窗体所在区域时窗体显示,反之隐藏起来。仅需一条API函数:GetCursorPos。注意:如果需要将API函数置于模块中请对代码作相应修改。要尝试本例,需给标准EXE工程缺省添加一个Timer控件。
PrivateTypePOINTAPI xAsLong yAsLong EndType
PrivateDeclareFunctionGetCursorPosLib"user32"(lpPointAsPOINTAPI)AsLong
PrivateSubForm_Load() Me.Visible=False Timer1.Enabled=True Timer1.Interval=100 EndSub
PrivateSubTimer1_Timer() DimlResultAsLong DimlpPointAsPOINTAPI DimiCounterAsInteger lResult=GetCursorPos(lpPoint) IflpPoint.x<Me.Left\Screen.TwipsPerPixelXOrlpPoint.x>(Me.Left _ Me.Width)\Screen.TwipsPerPixelXOrlpPoint.y<Me.Top\_ Screen.TwipsPerPixelYOrlpPoint.y-10>(Me.Top Me.Height)\_ Screen.TwipsPerPixelYThen Me.Visible=False'鼠标在窗体区域之外时 Else Me.Visible=True'鼠标在窗体区域之内时 EndIf EndSub
二.获得Mouse_Exit事件
所谓Mouse_Exit事件,是指鼠标指针离开某一控件所应发生的事件。本例是通过Form_MouseMove事件来判断鼠标指针是在窗体之内还是窗体之外的,你可根据需要作相应改动。请给窗体缺省创建一个按钮(用于观察效果)。
PrivateDeclareFunctionSetCaptureLib"user32"(ByValhWndAsLong)AsLong PrivateDeclareFunctionReleaseCaptureLib"user32"()AsLong
PrivateSubForm_MouseMove(ButtonAsInteger,ShiftAsInteger,XAsSingle,YAsSingle) DimMouseExitAsBoolean MouseExit=(0<=X)And(X<=Me.Width)And(0<=Y)And(Y<=Me.Height) IfMouseExitThen Me.Caption="鼠标指针在窗体范围内" Command1.Enabled=True SetCaptureMe.hWnd Else Me.Caption="鼠标指针在窗体范围外" Command1.Enabled=False ReleaseCapture EndIf EndSub->
|