8

可能重复:
使用 .NET 编写 Windows 系统托盘应用程序

嗨,
我正在.NET 中编写一个应用程序,我不想把它变成一个 Windows 界面。
我记得前段时间我通过继承 ApplicationContext 类来做到这一点,但现在我无法实现它。

我怎么能做到?

谢谢!

编辑: 这是一个由通知图标管理的应用程序。它必须出现在系统时钟附近的图标而不是表格。我正在这样做:

class Sync : ApplicationContext
{
    public Sync()
    {
        ...
    }
}

[STAThread]
static void Main()
{
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);
    Application.Run(new Sync());
}
4

4 回答 4

23

(obsolete now that you've edited the question to state systray; left for reference only)

If you want an exe without any UI (not even a console), but without it being a service etc - then write a windows forms app, but just don't show any forms! The fact that it is a winform app simply means you don't get a console window - but no UI is shown except that which you write. Look at the Main method (usually in Program.cs in the VS templates) to see what I mean.

于 2009-06-18T11:29:34.990 回答
9

您可以编写控制台应用程序或服务或类似的东西。你到底想做什么?

于 2009-06-18T11:20:45.443 回答
3

使用控制台应用程序Windows 服务怎么样?

于 2009-06-18T11:21:10.700 回答
1

see here: http://bluehouse.wordpress.com/2006/01/24/how-to-create-a-notify-icon-in-c-without-a-form/

Edit: added the code in the link to this answer in case the link dies. Credit goes to the author for this code.

using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.ComponentModel;
using System.Drawing;

class ControlContainer : IContainer
{
    ComponentCollection _components;

    public ControlContainer()
    {
        _components = new ComponentCollection(new IComponent[] { });
    }

    public void Add(IComponent component) { }
    public void Add(IComponent component, string Name) { }
    public void Remove(IComponent component) { }

    public ComponentCollection Components
    {
        get { return _components; }
    }

    public void Dispose()
    {
        _components = null;
    }
}

class MainEntryClass
{
    static void Main(string[] args)
    {
        SomeClass sc = new SomeClass();
        Application.Run();
    }
}

class SomeClass
{
    ControlContainer container = new ControlContainer();
    private System.Windows.Forms.NotifyIcon notifyIcon1;

    public SomeClass()
    {
        this.notifyIcon1 = new NotifyIcon(container);
    }
}
于 2009-06-18T11:37:44.957 回答