我遵循了常用的将应用程序减少到系统托盘的技巧:http://www.developer.com/net/csharp/article.php/3336751现在它可以工作了,但仍然有一个问题:我的应用程序在启动时显示;我希望它直接在系统托盘中启动。我试图最小化并将其隐藏在Load事件中,但它什么也不起作用。
编辑:我可以,就像一个发帖者建议的那样,修改快捷键属性,但我更喜欢使用代码:我不能完全控制安装软件的每一台计算机。
我不想把它从除了系统托盘之外的任何地方完全移除,我只想让它开始最小化。
有什么想法吗?
谢谢
发布于 2008-11-12 11:21:45
这就是你怎么做的
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();
}
}
发布于 2008-11-12 11:13:47
如果您使用的是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);
发布于 2008-11-12 12:58:03
由于这是用vb.net标记的,下面是我在刚刚完成的Windows Service and Controller应用程序中所做的事情,将代码模块添加到项目中,在Sub Main()中设置NotifyIcon及其关联的上下文菜单,然后将应用程序的Startup对象设置为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
https://stackoverflow.com/questions/283632
复制相似问题