实用技巧:使用notifyicondata在VB.NET中创建系统托盘图标

作者:吉林麻将开发公司 阅读:48 次 发布时间:2023-05-01 13:04:02

摘要:在VB.NET中,为了让我们的应用程序变得更加优雅和简约,我们经常需要使用系统托盘图标来显示通知和消息。然而,创建系统托盘图标并不是一件简单的事情,我们需要使用NotifyIcon控件,并使用notifyicondata结构体来配置图标的各种属性。本文将向你展示如何使用notifyicondata在...

在VB.NET中,为了让我们的应用程序变得更加优雅和简约,我们经常需要使用系统托盘图标来显示通知和消息。然而,创建系统托盘图标并不是一件简单的事情,我们需要使用NotifyIcon控件,并使用notifyicondata结构体来配置图标的各种属性。本文将向你展示如何使用notifyicondata在VB.NET中创建系统托盘图标,同时提供一些实用技巧来帮助你更好地掌握这一功能。

实用技巧:使用notifyicondata在VB.NET中创建系统托盘图标

第一步:创建空的Windows窗体

首先,我们需要创建一个空的Windows窗体,并将其属性FormBorderStyle设置为None,这样我们才能创建整洁的托盘图标。

第二步:添加NotifyIcon控件

接下来,我们需要在窗体上添加NotifyIcon控件。在工具箱中找到NotifyIcon控件并将其拖拽到窗体中。然后在属性窗口中设置NotifyIcon的各种属性,例如图标、文本、提示等等。

第三步:使用notifyicondata结构体来配置NotifyIcon

就像在C#中一样,VB.NET中也可以使用notifyicondata结构体来配置NotifyIcon。notifyicondata结构体包含了在图标上显示的信息和图标的行为。下面是一些重要的属性和方法:

1. Icon:图标必须是ico文件。如果没有图标,可以使用以下代码创建:

Dim icon As Icon = SystemIcons.Application

Dim ms As New MemoryStream()

icon.Save(ms)

Dim bmp As New Bitmap(ms)

2. Text:文本是显示在NotifyIcon的气泡提示中的文字。

3. Visible:指示是否可见。

4. BalloonTipIcon:指定气泡提示的图标。值为None、Info、Warning和Error。

5. BalloonTipTitle:指定气泡提示的标题。

6. BalloonTipText:指定气泡提示的文本。

7. ShowBalloonTip():显示气泡提示。

完整的代码如下:

Imports System.Runtime.InteropServices

Imports System.IO

Public Class Form1

'notifyicondata

_

Public Structure NotifyIconData

Public cbSize As Integer

Public hWnd As IntPtr

Public uID As Integer

Public uFlags As UInteger

Public uCallbackMessage As Integer

Public hIcon As IntPtr

_

Public szTip As Char()

Public dwState As Integer

Public dwStateMask As Integer

_

Public szInfo As Char()

Public uTimeoutOrVersion As Integer

_

Public szInfoTitle As Char()

Public dwInfoFlags As Integer

Public guidItem As Guid

End Structure

Private Const NIM_ADD As UInteger = &H0

Private Const NIM_MODIFY As UInteger = &H1

Private Const NIM_DELETE As UInteger = &H2

Private Const NIF_MESSAGE As UInteger = &H1

Private Const NIF_ICON As UInteger = &H2

Private Const NIF_TIP As UInteger = &H4

Private Const NIF_INFO As UInteger = &H10

Private Const NIIF_NONE As UInteger = &H0

Private Const NIIF_INFO As UInteger = &H1

Private Const NIIF_WARNING As UInteger = &H2

Private Const NIIF_ERROR As UInteger = &H3

Private Declare Function Shell_NotifyIcon Lib "shell32.dll" Alias "Shell_NotifyIconA" (ByVal dwMessage As UInteger, ByRef pnid As NotifyIconData) As Boolean

Private notifyIconData As NotifyIconData

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

notifyIconData.cbSize = Marshal.SizeOf(notifyIconData)

notifyIconData.hWnd = Me.Handle

notifyIconData.uID = &H1

notifyIconData.uFlags = NIF_MESSAGE Or NIF_ICON Or NIF_TIP

notifyIconData.uCallbackMessage = &H8000

notifyIconData.hIcon = CType(IconToBitmap(SystemIcons.Application), IntPtr)

notifyIconData.szTip = "NotifyIconTest".PadRight(128, Convert.ToChar(0))

Shell_NotifyIcon(NIM_ADD, notifyIconData)

notifyIconData.uTimeoutOrVersion = 4000

End Sub

Private Sub Form1_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed

Shell_NotifyIcon(NIM_DELETE, notifyIconData)

End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

notifyIconData.szInfo = "This is a test.".PadRight(256, Convert.ToChar(0))

notifyIconData.szInfoTitle = "Test Title".PadRight(64, Convert.ToChar(0))

notifyIconData.dwInfoFlags = NIIF_INFO

Shell_NotifyIcon(NIM_MODIFY, notifyIconData)

End Sub

Private Function IconToBitmap(ByVal icon As Icon) As Bitmap

Dim ms As New MemoryStream()

icon.Save(ms)

Dim bmp As New Bitmap(ms)

Return bmp

End Function

End Class

当我们运行应用程序时,我们会发现系统托盘区已经显示了一个普通的托盘图标。然后,我们通过点击图标来触发显示一个气泡提示的按钮。此时,我们的NotifyIcon就已经基本完成了。

最后,我们要记得在应用程序关闭的时候将NotifyIcon从系统托盘区删除。

总结

在VB.NET中创建系统托盘图标并不是一件难事,我们只需要在系统托盘区创建一个NotifyIcon控件,然后通过notifyicondata结构体来配置NotifyIcon的各种属性。此外,我们还可以使用一些实用技巧来优化我们的托盘图标,例如气泡提示、图标传输等等。希望这篇文章可以帮助你掌握在VB.NET中创建系统托盘图标的方法。

  • 原标题:实用技巧:使用notifyicondata在VB.NET中创建系统托盘图标

  • 本文链接:https:////qpzx/3257.html

  • 本文由吉林麻将开发公司飞扬众网小编,整理排版发布,转载请注明出处。部分文章图片来源于网络,如有侵权,请与飞扬众网联系删除。
  • 微信二维码

    CTAPP999

    长按复制微信号,添加好友

    微信联系

    在线咨询

    点击这里给我发消息QQ客服专员


    点击这里给我发消息电话客服专员


    在线咨询

    免费通话


    24h咨询☎️:166-2096-5058


    🔺🔺 棋牌游戏开发24H咨询电话 🔺🔺

    免费通话
    返回顶部