vb.net 程序右下角消息弹窗怎么实现,具体代码
时间: 2024-02-18 12:00:18 浏览: 41
在 VB.NET 中实现右下角消息弹窗,可以使用 NotifyIcon 控件和 ContextMenuStrip 控件。
首先,在窗体中添加一个 NotifyIcon 控件和一个 ContextMenuStrip 控件。在 ContextMenuStrip 控件中添加一个 ToolStripMenuItem,用于显示弹窗消息。
然后,在窗体的 Load 事件中添加如下代码:
```
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
NotifyIcon1.Icon = SystemIcons.Application
NotifyIcon1.Visible = True
ContextMenuStrip1.Items(0).Text = "显示消息"
End Sub
```
这段代码设置了 NotifyIcon 控件的图标和可见性,并且设置了 ContextMenuStrip 控件中第一个菜单项的文本。
接下来,在 ToolStripMenuItem 的 Click 事件中添加如下代码:
```
Private Sub ToolStripMenuItem1_Click(sender As Object, e As EventArgs) Handles ToolStripMenuItem1.Click
Dim notify As New NotifyIcon
notify.Icon = SystemIcons.Application
notify.BalloonTipIcon = ToolTipIcon.Info
notify.BalloonTipTitle = "消息标题"
notify.BalloonTipText = "消息内容"
notify.Visible = True
notify.ShowBalloonTip(3000)
End Sub
```
这段代码创建了一个新的 NotifyIcon 控件,并设置了弹窗消息的图标、标题和内容,并且显示了弹窗消息。最后,记得在窗体的 FormClosing 事件中将 NotifyIcon 控件的可见性设置为 False,以确保程序退出时 NotifyIcon 控件被正常关闭。
完整的代码如下:
```
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
NotifyIcon1.Icon = SystemIcons.Application
NotifyIcon1.Visible = True
ContextMenuStrip1.Items(0).Text = "显示消息"
End Sub
Private Sub ToolStripMenuItem1_Click(sender As Object, e As EventArgs) Handles ToolStripMenuItem1.Click
Dim notify As New NotifyIcon
notify.Icon = SystemIcons.Application
notify.BalloonTipIcon = ToolTipIcon.Info
notify.BalloonTipTitle = "消息标题"
notify.BalloonTipText = "消息内容"
notify.Visible = True
notify.ShowBalloonTip(3000)
End Sub
Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing
NotifyIcon1.Visible = False
End Sub
End Class
```
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)