c# - 如何显示 C# 窗口服务的系统托盘图标?

标签 c# windows

如何为 C# 窗口服务显示系统托盘图标?

最佳答案

服务在与登录用户不同的窗口站中运行,因此您不能为它们设置系统托盘图标。来自 https://learn.microsoft.com/en-us/dotnet/framework/windows-services/introduction-to-windows-service-applications :

Windows Service applications run in a different window station than the interactive station of the logged-on user. A window station is a secure object that contains a Clipboard, a set of global atoms, and a group of desktop objects. Because the station of the Windows service is not an interactive station, dialog boxes raised from within a Windows service application will not be seen and may cause your program to stop responding. Similarly, error messages should be logged in the Windows event log rather than raised in the user interface.

The Windows service classes supported by the .NET Framework do not support interaction with interactive stations, that is, the logged-on user. The .NET Framework also does not include classes that represent stations and desktops. If your Windows service must interact with other stations, you will need to access the unmanaged Windows API. For more information, see the Windows SDK documentation.

The interaction of the Windows service with the user or other stations must be carefully designed to include scenarios such as there being no logged on user, or the user having an unexpected set of desktop objects. In some cases, it may be more appropriate to write a Windows application that runs under the control of the user.

这里有几个关于如何写入系统托盘的链接。您将需要另一个应用程序来与该服务交互,因为该服务不能直接在系统托盘中显示图标。

How can I make a .NET Windows Forms application that only runs in the System Tray?

http://msdotnetsupport.blogspot.com/2008/02/cnet-application-windows-system-tray.html

关于c# - 如何显示 C# 窗口服务的系统托盘图标?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2652254/

相关文章:

c# - JSonConverter 没有在 C# WebAPI 中触发我的模型的属性

c# - 当枚举名称与类名称冲突时该怎么办?

c# - 如何在绑定(bind)到 DataTable 的 ComboBox 中插入 'Empty' 字段

windows - 在docker容器中创建D盘

python - 使用pyw文件的Windows启动不会关闭cmd

windows - 在PE的导入表中添加一个条目

windows - .Net Compact Framework,如何在windows下 "use"(测试目的)

c# - token 已过期或撤销 - Google Ads

c# - 如何在按钮控件上使用动画 GIF?

c++ - MFC slider +编辑+微调模式