Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/github/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 启动时将程序放入系统托盘_C#_.net_Vb.net - Fatal编程技术网

C# 启动时将程序放入系统托盘

C# 启动时将程序放入系统托盘,c#,.net,vb.net,C#,.net,Vb.net,我遵循了将应用程序缩减到系统托盘的常见链接提示:现在它可以工作了,但仍然存在一个问题:我的应用程序在启动时显示;我希望它直接从systray中启动。我试图在Load事件中最小化并隐藏它,但它没有任何作用 编辑:正如海报所建议的,我可以修改快捷方式属性,但我更喜欢使用代码:我不能完全控制安装软件的每台计算机 我不想把它从除systray之外的任何地方完全移除,我只想让它开始最小化 有什么想法吗 谢谢如果您使用的是通知图标,请尝试将ShowInTaskbar更改为false 要将其从Alt+Tab屏

我遵循了将应用程序缩减到系统托盘的常见链接提示:现在它可以工作了,但仍然存在一个问题:我的应用程序在启动时显示;我希望它直接从systray中启动。我试图在Load事件中最小化并隐藏它,但它没有任何作用

编辑:正如海报所建议的,我可以修改快捷方式属性,但我更喜欢使用代码:我不能完全控制安装软件的每台计算机

我不想把它从除systray之外的任何地方完全移除,我只想让它开始最小化

有什么想法吗


谢谢

如果您使用的是
通知图标
,请尝试将ShowInTaskbar更改为false

要将其从Alt+Tab屏幕中删除,请尝试更改窗口边框样式;我相信一些工具窗口样式不会出现

比如:

using System;
using System.Windows.Forms;
class MyForm : Form
{
    NotifyIcon sysTray;

    MyForm()
    {
        sysTray = new NotifyIcon();
        sysTray.Icon = System.Drawing.SystemIcons.Asterisk;
        sysTray.Visible = true;
        sysTray.Text = "Hi there";
        sysTray.MouseClick += delegate { MessageBox.Show("Boo!"); };

        ShowInTaskbar = false;
        FormBorderStyle = FormBorderStyle.SizableToolWindow;
        Opacity = 0;
        WindowState = FormWindowState.Minimized;
    }

    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.Run(new MyForm());
    }
}
如果它仍然出现在Alt+选项卡中,您可以通过p/invoke(稍微有点黑)更改窗口样式:


作为一种权宜之计,您可以将启动应用程序的快捷方式配置为“最小化运行”?那可能会给你你所需要的

像这样:(图片只是谷歌的一个例子)


(来源:)

你就是这样做的

static class Program
{
    [STAThread]
    static void Main()
    {
        NotifyIcon icon = new NotifyIcon();
        icon.Icon = System.Drawing.SystemIcons.Application;
        icon.Click += delegate { MessageBox.Show("Bye!"); icon.Visible = false; Application.Exit(); };
        icon.Visible = true;
        Application.Run();
    }
}

在您的主程序中,您可能有以下一行:

Application.Run(new Form1());
这将强制显示表单。您需要创建表单,但不能将其传递给
应用程序。运行

Form1 form = new Form1();
Application.Run();
请注意,在调用
Application.ExitThread()
之前,程序现在不会终止。最好从
FormClosed
事件的处理程序执行此操作

private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
    Application.ExitThread();
}

由于这是用vb.net标记的,下面是我在刚刚完成的Windows服务和控制器应用程序中所做的,将代码模块添加到项目中,在Sub Main()中设置NotifyIcon及其关联的上下文菜单,然后将应用程序的启动对象设置为Sub Main()而不是表单

Public mobNotifyIcon As NotifyIcon
Public WithEvents mobContextMenu As ContextMenu

Public Sub Main()

    mobContextMenu = New ContextMenu
    SetupMenu()
    mobNotifyIcon = New NotifyIcon()
    With mobNotifyIcon
        .Icon = My.Resources.NotifyIcon
        .ContextMenu = mobContextMenu
        .BalloonTipText = String.Concat("Monitor the EDS Transfer Service", vbCrLf, "Right click icon for menu")
        .BalloonTipIcon = ToolTipIcon.Info
        .BalloonTipTitle = "EDS Transfer Monitor"
        .Text = "EDS Transfer Service Monitor"
        AddHandler .MouseClick, AddressOf showBalloon
        .Visible = True
    End With
    Application.Run()
End Sub

Private Sub SetupMenu()
    With mobContextMenu

        .MenuItems.Add(New MenuItem("Configure", New EventHandler(AddressOf Config)))
        .MenuItems.Add("-")
        .MenuItems.Add(New MenuItem("Start", New EventHandler(AddressOf StartService)))
        .MenuItems.Add(New MenuItem("Stop", New EventHandler(AddressOf StopService)))
        .MenuItems.Add("-")
        .MenuItems.Add(New MenuItem("Exit", New EventHandler(AddressOf ExitController)))
    End With
    GetServiceStatus()
End Sub
在Config()中,我创建表单的一个实例并显示它

Private Sub Config(ByVal sender As Object, ByVal e As EventArgs)
    Using cs As New ConfigureService
        cs.Show()
    End Using

End Sub
给你:

创建2个类,1个从ApplicationContext继承。另一个只包含一个主例程。我已经做了一个例子,它有一个表单和一个notify图标,当双击该图标时,会显示表单并再次返回

请记住在“我的项目设置”中将“Sub-Main”设置为启动对象,并指向实际的*.ico文件,而不是f:\TP.ico.:)

当然,代码中应该填充正确的错误处理代码

类别1:

Imports System.threading 
Imports System.Runtime.InteropServices 
Imports System.Windows.Forms


Public Class Class1

    <System.STAThread()> _
        Public Shared Sub Main()

        Try
            System.Windows.Forms.Application.EnableVisualStyles()
            System.Windows.Forms.Application.DoEvents()
            System.Windows.Forms.Application.Run(New Class2)
        Catch invEx As Exception

            Application.Exit()

        End Try


    End Sub 'Main End Class 
导入系统线程
导入System.Runtime.InteropServices
导入System.Windows.Forms
公共班级1
_
公共共享子主目录()
尝试
System.Windows.Forms.Application.EnableVisualStyles()
System.Windows.Forms.Application.DoEvents()
System.Windows.Forms.Application.Run(新类2)
捕获invEx作为例外
Application.Exit()
结束尝试
“端子”主端类
类别2:

Imports System.Windows.Forms  
Imports System.drawing

Public Class Class2
    Inherits System.Windows.Forms.ApplicationContext

    Private WithEvents f As New System.Windows.Forms.Form
    Private WithEvents nf As New System.Windows.Forms.NotifyIcon

    Public Sub New()

        f.Size = New Drawing.Size(50, 50)
        f.StartPosition = FormStartPosition.CenterScreen
        f.WindowState = Windows.Forms.FormWindowState.Minimized
        f.ShowInTaskbar = False
        nf.Visible = True
        nf.Icon = New Icon("f:\TP.ico")
    End Sub


    Private Sub nf_DoubleClick(ByVal sender As Object, ByVal e As EventArgs) Handles nf.DoubleClick
        If f.WindowState <> Windows.Forms.FormWindowState.Minimized Then
            f.WindowState = Windows.Forms.FormWindowState.Minimized
            f.Hide()
        Else
            f.WindowState = Windows.Forms.FormWindowState.Normal
            f.Show()
        End If
    End Sub

    Private Sub f_FormClosed(ByVal sender As Object, ByVal e As FormClosedEventArgs) Handles f.FormClosed
        Application.Exit()
    End Sub  End Class
导入System.Windows.Forms
导入System.drawing
公共课2
继承System.Windows.Forms.ApplicationContext
Private with events f As New System.Windows.Forms.Form
Private with events nf As New System.Windows.Forms.NotifyIcon
公共分新()
f、 尺寸=新图纸。尺寸(50,50)
f、 StartPosition=FormStartPosition.CenterScreen
f、 WindowsState=Windows.Forms.FormWindowsState.Minimized
f、 ShowInTaskbar=False
nf.Visible=True
nf.Icon=新图标(“f:\TP.ico”)
端接头
私有子nf_双击(ByVal sender作为对象,ByVal e作为EventArgs)处理nf.DoubleClick
如果f.WindowsState Windows.Forms.FormWindowsState.Minimized,则
f、 WindowsState=Windows.Forms.FormWindowsState.Minimized
f、 隐藏()
其他的
f、 WindowsState=Windows.Forms.FormWindowsState.Normal
f、 Show()
如果结束
端接头
私有子f_FormClosed(ByVal sender作为对象,ByVal e作为FormClosedEventArgs)处理f.FormClosed
Application.Exit()
端子端类

这向您展示了如何使用NotifyIcon控制最小化或正常启动,以及更多

详情如下:

Imports System.Windows.Forms  
Imports System.drawing

Public Class Class2
    Inherits System.Windows.Forms.ApplicationContext

    Private WithEvents f As New System.Windows.Forms.Form
    Private WithEvents nf As New System.Windows.Forms.NotifyIcon

    Public Sub New()

        f.Size = New Drawing.Size(50, 50)
        f.StartPosition = FormStartPosition.CenterScreen
        f.WindowState = Windows.Forms.FormWindowState.Minimized
        f.ShowInTaskbar = False
        nf.Visible = True
        nf.Icon = New Icon("f:\TP.ico")
    End Sub


    Private Sub nf_DoubleClick(ByVal sender As Object, ByVal e As EventArgs) Handles nf.DoubleClick
        If f.WindowState <> Windows.Forms.FormWindowState.Minimized Then
            f.WindowState = Windows.Forms.FormWindowState.Minimized
            f.Hide()
        Else
            f.WindowState = Windows.Forms.FormWindowState.Normal
            f.Show()
        End If
    End Sub

    Private Sub f_FormClosed(ByVal sender As Object, ByVal e As FormClosedEventArgs) Handles f.FormClosed
        Application.Exit()
    End Sub  End Class