我按照常用链接提示将应用程序减少到系统托盘:http://www.developer.com/net/csharp/article.php/3336751现在它可以正常工作,但仍然存在问题:我的应用程序在启动时显示;我希望它直接在系统托盘中启动。我试图在Load事件中最小化并隐藏它,但它什么也没做。
编辑:我可以像海报建议的那样修改快捷方式属性,但我宁愿使用代码:我无法完全控制安装软件的每台计算机。
我不想从除了systray之外的任何地方完全删除它,我只是想让它开始最小化。
有什么想法吗?
由于
答案 0 :(得分:26)
在你的主程序中,你可能有一行表格:
Application.Run(new Form1());
这将强制显示表单。您需要创建表单,但不将其传递给Application.Run
:
Form1 form = new Form1();
Application.Run();
请注意,在您致电Application.ExitThread()
之前,该程序现在不会终止。最好从FormClosed
事件的处理程序执行此操作。
private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
Application.ExitThread();
}
答案 1 :(得分:13)
这是你怎么做的
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();
}
}
答案 2 :(得分:3)
如果您使用的是NotifyIcon
,请尝试将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 + Tab中,你可以通过p / invoke更改窗口样式(有点粗糙):
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
IntPtr handle = this.Handle;
int currentStyle = GetWindowLong(handle, GWL_EXSTYLE);
SetWindowLong(handle, GWL_EXSTYLE, currentStyle | WS_EX_TOOLWINDOW);
}
private const int GWL_EXSTYLE = -20, WS_EX_TOOLWINDOW = 0x00000080;
[System.Runtime.InteropServices.DllImport("user32.dll")]
private static extern int SetWindowLong(IntPtr window, int index, int value);
[System.Runtime.InteropServices.DllImport("user32.dll")]
private static extern int GetWindowLong(IntPtr window, int index);
答案 3 :(得分:2)
答案 4 :(得分:1)
由于这是用vb.net标记的,这就是我在刚完成的Windows服务和控制器应用程序中所做的,在项目中添加代码模块,在Sub Main()中设置NotifyIcon及其相关的上下文菜单,以及然后将应用程序的启动对象设置为Sub Main()而不是Form。
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
答案 5 :(得分:0)
你走了:
创建2个类,1个继承自ApplicationContext。另一个只包含一个Main例程。我做了一个有一个表单和一个通知图标的例子,当双击时会显示该表单然后再返回。
请记住在My Project设置中将“Sub Main”设置为启动对象,并指向真实的* .ico文件,而不是f:\ TP.ico .. :)
代码当然应该包含适当的错误处理代码。
的Class1:
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
等级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
答案 6 :(得分:0)
这将向您展示如何使用NotifyIcon将启动控制为最小化或正常以及更多。