如何在 C# 中做一个没有表单的应用程序?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1012113/
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-08-06 05:30:45  来源:igfitidea点击:

How to do an application without form in C#?

c#windowless

提问by Alfre2

Possible Duplicate:
Writing a Windows system tray application with .NET

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

Hi,
I am programming an application in .NET and I don't want to put it a windows interface.
I remember some time ago I did it with inheriting from the ApplicationContext class but now I can't achieve it.

嗨,
我正在用 .NET 编写一个应用程序,但我不想把它放在一个 Windows 界面上。
我记得前段时间我是通过继承 ApplicationContext 类来实现的,但现在我无法实现它。

How I could do it?

我怎么能做到?

Thanks!

谢谢!

Edit:It's an application that is managed by a notify icon. It must appear the icon near the system clock but not a form. I'm doing this:

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

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

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

回答by Aamir

You can write a Console Application or a Service or something of that sort. What exactly do you want to do?

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

回答by Mitch Wheat

回答by Marc Gravell

(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 Mainmethod (usually in Program.csin the VS templates) to see what I mean.

如果您想要一个没有任何 UI(甚至不是控制台)的 exe,但又不是服务等,那么请编写一个 Windows 窗体应用程序,但不要显示任何表单!它是一个 winform 应用程序这一事实仅意味着您没有获得控制台窗口 - 但除了您编写的内容外,没有显示任何 UI。查看Main方法(通常在Program.csVS 模板中)以了解我的意思。

回答by rein

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

见这里: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);
    }
}