我想在 Windows 通知区域显示一个简短的状态文本,不是图标,而是几个字符的文本,其内容定期变化并来自文本文件。
有没有简单的方法可以将这样的文本放入通知区域?
答案1
感谢@LotPings 向我指出了另一个答案,我设法创建了一个 VB 脚本,该脚本定期从文件中读取文本并将其显示在通知区域(图片上的 29)。
目前只有两个字符,但您可以使用较小的字体来显示更多。
请注意,我既不是 Windows 程序员,也不是 VB 程序员,所以代码并不漂亮,我只是从示例中复制粘贴,直到使其正常工作。
以下是可以使用 vbc.exe 编译的代码(例如C:\Windows\Microsoft.NET\Framework64\v4.0.30319\vbc.exe
):
Imports System
Imports System.Drawing
Imports System.Windows.Forms
Imports System.Timers
Public NotInheritable Class Form1
Inherits System.Windows.Forms.Form
Private contextMenu1 As System.Windows.Forms.ContextMenu
Friend WithEvents menuItem1 As System.Windows.Forms.MenuItem
Friend WithEvents notifyIcon1 As System.Windows.Forms.NotifyIcon
Private components As System.ComponentModel.IContainer
Private fontToUse As System.Drawing.Font
Private brushToUse As System.Drawing.Brush
Private bitmapText As System.Drawing.Bitmap
Private g As System.Drawing.Graphics
Private hIcon As IntPtr
Private aTimer As System.Timers.Timer
<System.STAThread()> _
Public Shared Sub Main()
System.Windows.Forms.Application.Run(New Form1)
End Sub 'Main
Public Sub New()
Me.components = New System.ComponentModel.Container
Me.contextMenu1 = New System.Windows.Forms.ContextMenu
Me.menuItem1 = New System.Windows.Forms.MenuItem
' Initialize contextMenu1
Me.contextMenu1.MenuItems.AddRange(New System.Windows.Forms.MenuItem() _
{Me.menuItem1})
' Initialize menuItem1
Me.menuItem1.Index = 0
Me.menuItem1.Text = "E&xit"
' Set up how the form should be displayed.
Me.ClientSize = New System.Drawing.Size(292, 266)
Me.Text = "Notify Icon Example"
' Create the NotifyIcon.
Me.notifyIcon1 = New System.Windows.Forms.NotifyIcon(Me.components)
notifyIcon1.ContextMenu = Me.contextMenu1
aTimer = New System.Timers.Timer(300000)
AddHandler aTimer.Elapsed, AddressOf OnTimedEvent
aTimer.AutoReset = True
aTimer.Enabled = True
UpdateIcon
Me.WindowState = FormWindowState.Minimized
Me.ShowInTaskbar = False
End Sub 'New
Private Sub OnTimedEvent(source As Object, e As ElapsedEventArgs)
'Console.WriteLine("The Elapsed event was raised at {0:HH:mm:ss.fff}",
' e.SignalTime)
UpdateIcon
End Sub
Private Sub UpdateIcon
Dim fileReader As String
fileReader = My.Computer.FileSystem.ReadAllText("c:\temp.txt")
fontToUse = New Font("Microsoft Sans Serif", 16, FontStyle.Regular, GraphicsUnit.Pixel)
brushToUse = New SolidBrush(Color.White)
bitmapText = new Bitmap(16, 16)
g = Drawing.Graphics.FromImage(bitmapText)
g.Clear(Color.Transparent)
g.DrawString(fileReader, fontToUse, brushToUse, -4, -2)
hIcon = (bitmapText.GetHicon)
Me.notifyicon1.Icon = Drawing.Icon.FromHandle(hIcon)
notifyIcon1.Visible = True
End Sub
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
' Clean up any components being used.
If disposing Then
If (components IsNot Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub 'Dispose
Private Sub notifyIcon1_DoubleClick(Sender as object, e as EventArgs) handles notifyIcon1.DoubleClick
' Show the form when the user double clicks on the notify icon.
' Set the WindowState to normal if the form is minimized.
if (me.WindowState = FormWindowState.Minimized) then _
me.WindowState = FormWindowState.Normal
' Activate the form.
me.Activate()
end sub
Private Sub menuItem1_Click(Sender as object, e as EventArgs) handles menuItem1.Click
' Close the form, which closes the application.
me.Close()
end sub
End Class 'Form1