当前位置:编程学习 > VB >>

麻烦写一个VB编程

编写一个程序,实现鼠标指针移到窗口上时窗口变大,移出窗口时窗口变小
答案:不是很完善的,帮你写了一个。

Option Explicit

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

Event MouseIn()
Event MouseOut()


Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
    Static isIn As Boolean
    If X >= 0 And X <= Me.Width And Y >= 0 And Y <= Me.Height Then
        SetCapture Me.hwnd
        If Not isIn Then
            isIn = True
            RaiseEvent MouseIn
            Me.Width = Me.Width * 2
            Me.Height = Me.Height * 2
        End If
       
    Else
        ReleaseCapture
        If isIn Then
            isIn = False
            RaiseEvent MouseOut
            Me.Width = Me.Width \ 2
            Me.Height = Me.Height \ 2
        End If
    End If
End Sub

 

'-----------

'event 和 raiseevent 句可以删除。你改其他控件可以用这两个

'如果完善一些,可以用GetCursorPos,Screen.TwipsPerPixelX 来计算并判断 ,但那样是那是拿着鼠标不放,个人认为不是很合理。如果你愿意改,就自行改进了。

用Form 的 Mouse_Move事件来检查鼠标是否在窗口内:
  如果在窗口内就使窗口尺寸变大(Me.Height = 大窗口高度值);
  如果鼠标离开就使窗口尺寸变小(Me.Height = 大窗口高度值);
  但是,最好不要直接对窗口的尺寸进行增减,应该首先判断窗口尺寸是否已经是所需窗口的大小,最后设定窗口始终在最前方。下面附上代码,供参考:
  '首先应设置Form1.BorderStyle = 0
  Option Explicit
  Dim a As Long
  Dim b As Long
  Private Sub Form_Load()
  a = 3500
  b = 1000
  Form1.BackColor = vbMagenta
  Form1.Width = a
  Form1.Height = b
  End Sub
  '鼠标移动到窗口则窗口变大反之则变小
  Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
  Form1.Width = 8200
  Form1.Height = 2450
  If (X > 8000) or (Y > 2000) or (X < 200) or (Y < 200) Then '在理论上应取0   Form1.Width = a
  Form1.Height = b
  End If
  End Sub
多多练习!!!

上一个:C语言,VB编程题
下一个:vb 编程 高手进!!

CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,