史萊姆論壇

史萊姆論壇 (http://forum.slime.com.tw/)
-   程式語言討論區 (http://forum.slime.com.tw/f76.html)
-   -   請問關於vb裡判斷滑鼠的問題 (http://forum.slime.com.tw/thread175143.html)

foxsimon2001 2006-05-22 03:36 PM

請問關於vb裡判斷滑鼠的問題
 
是這樣的
例如一個text1 裡面有東西
當滑鼠4秒沒動作時 text就出現現在系統時間
如果一動作就回覆原本text內的內容
下次又超過4秒沒動作就顯示時間

請問一下這樣的程式 應該如何判斷滑鼠有無動作呢?
PS.我是使用Visual Basic 6.0

mini 2006-05-22 07:25 PM

可以在每個物件的 MouseMove 事件 與 一個Timer 上作組合判斷
(需要一對 gX、gY 變數)

但其有個缺點 滑鼠一離開你的視窗程式 其滑鼠的移動 就不算數
此時就可用系統Hook方法
(但對初學者而言難理解了點...)

語法:

'timeGetTime()
'精度約1ms , 此式適用於大多數應用場合
Private Declare Function timeGetTime Lib "winmm.dll" () As Long

'Dim CTime As Integer
Dim TempT As Long
Dim PreText As String

Dim gX As Single, gY As Single

Private Sub Form_Load()

    PreText = Text1
    'CTime = 10
    TempT = timeGetTime
    Timer1.Interval = 100
    Timer1.Enabled = True
   
End Sub

Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)

    If gX = X And gY = Y Then Exit Sub
    gX = X: gY = Y
   
    'CTime = 40
    TempT = timeGetTime
   
    If Timer1.Enabled = False Then
        Text1 = PreText
        Timer1.Enabled = True
    End If
   
End Sub

Private Sub Timer1_Timer()

    'CTime = CTime - 1
    'Debug.Print CTime
   
    'If CTime < 0 Then
    If (timeGetTime - TempT) > 4000 Then
        PreText = Text1
        Text1 = Time
        Timer1.Enabled = False
    End If
   
End Sub

以上程式為求準確使用 timeGetTime
API函式
也可完全用 Timer 的計數準確度 (把上面的' 註解取消與 timeGetTime交換)
只是 VB 的 Timer 影響因素眾多
很容易不準確

mini 2006-05-22 07:59 PM

但以上程式可發現
系統時間不會動
此時發現原來 Timer 需持續計數
最後的改良版如下
語法:

'timeGetTime()
'精度約1ms , 此式適用於大多數應用場合
Private Declare Function timeGetTime Lib "winmm.dll" () As Long

Dim TempT As Long
Dim PreText As String
Dim gX As Single, gY As Single
Dim blnMove As Boolean

Private Sub Form_Load()

    PreText = Text1
    TempT = timeGetTime
    Timer1.Interval = 100
    Timer1.Enabled = True
   
End Sub

Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)

    If gX = X And gY = Y Then Exit Sub
    gX = X: gY = Y
   
    TempT = timeGetTime
    blnMove = True
    Text1 = PreText
   
End Sub

Private Sub Timer1_Timer()

    If (timeGetTime - TempT) > 1500 Then '表示1.5秒不動的話...
        If blnMove Then
            PreText = Text1
            blnMove = False
        End If
       
        Text1 = Time
    End If
   
End Sub


foxsimon2001 2006-05-23 09:19 PM

謝謝版主幫忙 終於想通了
我知道怎麼做嚕


所有時間均為台北時間。現在的時間是 12:48 AM

Powered by vBulletin® 版本 3.6.8
版權所有 ©2000 - 2025, Jelsoft Enterprises Ltd.

『服務條款』

* 有問題不知道該怎麼解決嗎?請聯絡本站的系統管理員 *


SEO by vBSEO 3.6.1