史萊姆論壇

史萊姆論壇 (http://forum.slime.com.tw/)
-   程式語言討論區 (http://forum.slime.com.tw/f76.html)
-   -   [使用版本VB2008]使用NotifyIcon(TrayIcon)偵測滑鼠有無移動 (http://forum.slime.com.tw/thread275422.html)

魔術王子 2014-02-05 07:32 PM

[使用版本VB2008]使用NotifyIcon(TrayIcon)偵測滑鼠有無移動
 
將程式置於(常駐)NotifyIcon(TrayIcon)偵側滑鼠狀態
若滑鼠3秒沒動,就會出現
https://imagizer.imageshack.us/v2/720x576q90/163/6xbc.jpg
置於(常駐)NotifyIcon(TrayIcon)的圖示
https://imagizer.imageshack.us/v2/720x529q90/543/epda.jpg
引用:

作者: 元件
Private Sub InitializeComponent()
Me.components = New System.ComponentModel.Container
Me.Timer1 = New System.Windows.Forms.Timer(Me.components)
Me.NotifyIcon1 = New System.Windows.Forms.NotifyIcon(Me.components)
Me.Label1 = New System.Windows.Forms.Label
Me.ContextMenuStrip1 = New System.Windows.Forms.ContextMenuStrip(Me.components)
Me.ToolStripMenuItem1 = New System.Windows.Forms.ToolStripMenuItem
Me.ContextMenuStrip1.SuspendLayout()
Me.SuspendLayout()
'
'Timer1
'
Me.Timer1.Enabled = True
'
'NotifyIcon1
'
Me.NotifyIcon1.Text = "NotifyIcon1"
Me.NotifyIcon1.Visible = True
'
'Label1
'
Me.Label1.AutoSize = True
Me.Label1.Font = New System.Drawing.Font("標楷體", 50.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(136, Byte))
Me.Label1.Location = New System.Drawing.Point(12, 9)
Me.Label1.Name = "Label1"
Me.Label1.Size = New System.Drawing.Size(233, 67)
Me.Label1.TabIndex = 0
Me.Label1.Text = "Label1"
'
'ContextMenuStrip1
'
Me.ContextMenuStrip1.Items.AddRange(New System.Windows.Forms.ToolStripItem() {Me.ToolStripMenuItem1})
Me.ContextMenuStrip1.Name = "ContextMenuStrip1"
Me.ContextMenuStrip1.Size = New System.Drawing.Size(101, 26)
'
'ToolStripMenuItem1
'
Me.ToolStripMenuItem1.Name = "ToolStripMenuItem1"
Me.ToolStripMenuItem1.Size = New System.Drawing.Size(100, 22)
Me.ToolStripMenuItem1.Text = "離開"
'
'Form1
'
Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 12.0!)
Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
Me.ClientSize = New System.Drawing.Size(501, 189)
Me.Controls.Add(Me.Label1)
Me.Name = "Form1"
Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
Me.Text = "Form1"
Me.ContextMenuStrip1.ResumeLayout(False)
Me.ResumeLayout(False)
Me.PerformLayout()

End Sub
Friend WithEvents Timer1 As System.Windows.Forms.Timer
Friend WithEvents NotifyIcon1 As System.Windows.Forms.NotifyIcon
Friend WithEvents Label1 As System.Windows.Forms.Label
Friend WithEvents ContextMenuStrip1 As System.Windows.Forms.ContextMenuStrip
Friend WithEvents ToolStripMenuItem1 As System.Windows.Forms.ToolStripMenuItem

引用:

作者: 程式碼
Public Class Form1
Private Declare Function GetCursorPos Lib "user32" Alias "GetCursorPos" (ByRef lpPoint As POINTAPI) As Long
Private Structure POINTAPI
Dim x As Integer
Dim y As Integer
End Structure

Dim p As POINTAPI, p1 As POINTAPI
Dim dt As Double = My.Computer.Clock.TickCount

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
NotifyIcon1.Icon = Icon
NotifyIcon1.ContextMenuStrip = ContextMenuStrip1
GetCursorPos(p)
End Sub

Private Sub ToolStripMenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripMenuItem1.Click
Close()
End Sub

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
p1 = p
NotifyIcon1.Text = Now().ToLongTimeString
GetCursorPos(p)
If p.x = p1.x And p.y = p1.y Then
If My.Computer.Clock.TickCount - dt > 3000 Then
If Not Me.Visible Then Me.Visible = True
Label1.Text = Now().ToLongDateString & vbCrLf & Now().ToLongTimeString
End If
Else
If Me.Visible Then Me.Visible = False
dt = My.Computer.Clock.TickCount
End If
End Sub

Private Sub Form1_FormClosed(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles MyBase.FormClosed
NotifyIcon1.Visible = False
End Sub

Private Sub NotifyIcon1_MouseDoubleClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles NotifyIcon1.MouseDoubleClick
Timer1.Enabled = False
Me.Visible = True
End Sub

Private Sub NotifyIcon1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NotifyIcon1.Click
GetCursorPos(p)
ContextMenuStrip1.Show(p.x, p.y)
End Sub
End Class

此段是在檢查,滑鼠有無移動,若滑鼠沒有移動,程式就會開始計數
引用:

If p.x = p1.x And p.y = p1.y Then
If My.Computer.Clock.TickCount - dt > 3000 Then
If Not Me.Visible Then Me.Visible = True
Label1.Text = Now().ToLongDateString & vbCrLf & Now().ToLongTimeString
End If
Else
If Me.Visible Then Me.Visible = False
dt = My.Computer.Clock.TickCount
End If

若滑鼠超過3000沒移動就會顯示表單,若你覺得3秒不夠或太多,可自行更改
引用:

If My.Computer.Clock.TickCount - dt > 3000 Then
If Not Me.Visible Then Me.Visible = True
Label1.Text = Now().ToLongDateString & vbCrLf & Now().ToLongTimeString
End If



所有時間均為台北時間。現在的時間是 08:28 AM

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

『服務條款』

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


SEO by vBSEO 3.6.1