首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

使用.NET 4将程序最小化到系统托盘的最简单方法

问题:使用.NET 4将程序最小化到系统托盘的最简单方法是什么?

答案

使用.NET 4将程序最小化到系统托盘的最简单方法是使用Microsoft's Taskbar API。以下是实现此操作的步骤:

  1. 在Visual Studio中创建一个新的Windows应用程序,并选择空白应用程序
  2. 在应用程序的App.xaml.cs文件中,添加以下代码:
代码语言:csharp
复制
using System.Runtime.InteropServices;
using Microsoft.Windows.Shell;

// 导入必要的命名空间
[assembly: ApplicationActivationManager(ActivationType = ApplicationActivationMode.SystemState)]
[assembly: ApplicationShutdownManager]

namespace YourNamespace
{
    class Program : ApplicationActivationManager
    {
        protected override bool OnStartup(EventArgs e)
        {
            // 获取任务栏的ShellFolder
            ShellFolder taskbarFolder = ShellFolder.FromParsingName(ParsingName);

            // 创建一个ShellLink对象,并设置目标路径
            ShellLink shellLink = new ShellLink();
            shellLink.TargetPath = Application.ExecutablePath;

            // 将ShellLink对象添加到任务栏文件夹
            taskbarFolder.Items.Add(shellLink);

            return true;
        }

        protected override void OnShutdown(ShutdownEventArgs e)
        {
            // 从任务栏文件夹中移除ShellLink对象
            ShellFolder taskbarFolder = ShellFolder.FromParsingName(ParsingName);
            taskbarFolder.Items.Remove(shellLink);

            base.OnShutdown(e);
        }
    }
}
  1. App.xaml文件中,设置应用程序的标题和图标:
代码语言:xaml
复制
<Application x:Class="YourNamespace.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="clr-namespace:YourNamespace"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation"
             xmlns:local1="clr-namespace:YourNamespace.Properties"
             xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
             xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions"
             xmlns:converter="clr-namespace:YourNamespace.Converters"
             xsi:schemaLocation="http://schemas.microsoft.com/winfx/2006/xaml/presentation http://schemas.microsoft.com/winfx/2006/xaml/presentation.xsd">
  <Application.Resources>
    <local:LocalizedStringsConverter x:Key="LocalizedStringsConverter"/>
  </Application.Resources>
</Application>
  1. Resources文件夹中创建一个名为LocalizedStringsConverter.cs的类,并添加以下代码:
代码语言:csharp
复制

using System;

using System.Globalization;

using System.Windows.Data;

using System.Windows.Markup;

// 导入必要的命名空间

assembly: XmlnsDefinition("http://schemas.microsoft.com/winfx/2006/xaml/presentation", "YourNamespace")

assembly: XmlnsDefinition("http://schemas.microsoft.com/winfx/2006/xaml", "YourNamespace")

namespace YourNamespace

{

代码语言:txt
复制
public class LocalizedStringsConverter : MarkupExtension, IValueConverter
代码语言:txt
复制
{
代码语言:txt
复制
    public LocalizedStringsConverter()
代码语言:txt
复制
    {
代码语言:txt
复制
        // 设置为True以显示本地化字符串,False以显示原始字符串
代码语言:txt
复制
        IsIncluded = true;
代码语言:txt
复制
    }
代码语言:txt
复制
    public override object ProvideValue(IServiceProvider serviceProvider)
代码语言:txt
复制
    {
代码语言:txt
复制
        return this;
代码语言:txt
复制
    }
代码语言:txt
复制
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
代码语言:txt
复制
    {
代码语言:txt
复制
        string text = value as string;
代码语言:txt
复制
        if (text == null)
代码语言:txt
复制
            return "";
代码语言:txt
复制
        // 从资源字符串中提取本地化字符串
代码语言:txt
复制
        string localizableText = Regex.Match(text, @"^\s*localizable:\s*(true|false)\s*").Groups[1].Value;
代码语言:txt
复制
        if (bool.
页面内容是否对你有帮助?
有帮助
没帮助

相关·内容

没有搜到相关的合辑

领券