在 VB.Net 中构建系统托盘应用程序?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20636231/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 16:10:45  来源:igfitidea点击:

Build systray application in VB.Net?

vb.netnotifyicon

提问by Gulbahar

I'm using Visual Studio Express 2013 to write a formless VB.Net application that will start and remain iconized in the systray section.

我正在使用 Visual Studio Express 2013 编写一个无格式的 VB.Net 应用程序,该应用程序将在系统托盘部分启动并保持图标化。

I googled for infos on how to get started but didn't find much.

我在谷歌上搜索了有关如何开始的信息,但没有找到太多。

Would anyone have pointers to get started?

有没有人有开始的指示?

Thank you.

谢谢你。



Edit: If you need to add support for a single left click, add the following code to the NotifyIcon1 object so that the application doesn't also open the pop-up menu:

编辑:如果您需要添加对单击左键的支持,请将以下代码添加到 NotifyIcon1 对象中,以便应用程序不会同时打开弹出菜单:

Private Sub LeftClick(sender As Object, e As EventArgs) Handles NotifyIcon1.Click
    'Work-around to prevent Windows from triggering Click then right-click
    Dim MyButton As System.Windows.Forms.MouseEventArgs = e
    If MyButton.Button = MouseButtons.Left Then
        'Find how to put focus on msgbox
        MessageBox.Show("Left click")
    End If
End Sub

回答by ?s??? ????

Creating an application that lives in the system tray is easy, but it is not entirely obvious how to do so in Visual Studio.

创建位于系统托盘中的应用程序很容易,但如何在 Visual Studio 中执行此操作并不十分明显。

1) First, create a new Windows Application project in Visual Studio.NET. This can be either Visual Basic or C#

1) 首先,在Visual Studio.NET 中新建一个Windows Application 项目。这可以是 Visual Basic 或 C#

2) Drag a Notifyiconcontrol and a ContextMenucontrol from the toolbox onto the form.

2) 将一个Notifyicon控件和一个ContextMenu控件从工具箱中拖到窗体上。

3) Click on the NotifyIconcontrol that you just added, and set the Iconproperty to whatever icon you want your application to have.

3) 单击NotifyIcon您刚刚添加的控件,并将该Icon属性设置为您希望应用程序具有的任何图标。

4) Set the ContextMenuproperty of the Notifyiconto the context menu that you added to your project.

4) 将 的ContextMenu属性设置为Notifyicon您添加到项目中的上下文菜单。

5) Right click on the Context Menucontrol, and select Edit. Since this menu will be the right-click menu for your tray icon, you will want to add the items that the user will see. Make sure to add an Exitmenu item.

5) 右键单击Context Menu控件,然后选择编辑。由于此菜单将是托盘图标的右键单击菜单,因此您需要添加用户将看到的项目。确保添加Exit菜单项。

6) Double click the Exitmenu item, and add the following code:

6) 双击Exit菜单项,添加如下代码:

Me.Close

7) Now for the important settings. Click on the form, and go to the Properties window. Set the following settings:

7) 现在进行重要设置。单击表单,然后转到“属性”窗口。设置以下设置:

FormBorderSize: Fixed Tool Window
WindowState: Minimized
Opacity: 0%
ShowInTaskbar: False

The key thing to remember is that the default form is not to be used for application functionality, it is only used as the hidden background window. If you want to create a new window, you can just add another form to your application.

要记住的关键是默认表单不用于应用程序功能,它仅用作隐藏的背景窗口。如果您想创建一个新窗口,您只需将另一个表单添加到您的应用程序中即可。