簡體   English   中英

更改 Windows Forms 應用程序中的默認圖標

[英]Changing the default icon in a Windows Forms application

我需要更改我正在處理的應用程序中的圖標。 但只需從項目屬性選項卡 ->應用程序->圖標瀏覽其他圖標,它不會將圖標存儲在桌面上。

正確的做法是什么?

您在桌面上看到的圖標不是圖標文件。 它們是可執行文件.exe或任何應用程序的快捷方式.lnk 所以只能設置帶有.ico擴展名的圖標。

轉到項目菜單 -> Your_Project_Name 屬性 -> 應用程序選項卡 -> 資源 -> 圖標

瀏覽你的圖標,記住它必須有.ico擴展名

你可以在 Visual Studio 中制作你的圖標

轉到項目菜單 -> 添加新項目 -> 圖標文件

任務欄和窗口標題中顯示的圖標是主窗體的圖標。 通過更改其圖標,您還可以設置任務欄中顯示的圖標,當已 包含在 *.resx 中時

System.ComponentModel.ComponentResourceManager resources = 
    new System.ComponentModel.ComponentResourceManager(typeof(MyForm));
this.Icon = ((System.Drawing.Icon)(resources.GetObject("statusnormal.Icon")));

或者,直接從您的資源中閱讀:

this.Icon = new Icon("Resources/statusnormal.ico");

如果您不能立即找到Form的代碼,請在您的整個項目( CTRL+SHIFT+F )中搜索顯示的 Window-Title (假設文本是靜態的)

添加您的圖標作為資源(項目> yourprojectname 屬性> 資源> 從下拉菜單中選擇圖標> 添加資源(如果您已經擁有.ico,則從下拉菜單中選擇添加現有文件)

然后:

this.Icon = Properties.Resources.youriconname;

您可以在項目屬性下更改應用程序圖標。 表單屬性下的單個表單圖標。

一旦圖標在 Visual Studio 中采用 .ICO 格式,我就使用

//This uses the file u give it to make an icon. 

Icon icon = Icon.ExtractAssociatedIcon(String);//pulls icon from .ico and makes it then icon object.

//Assign icon to the icon property of the form

this.Icon = icon;

所以簡而言之

Icon icon = Icon.ExtractAssociatedIcon("FILE/Path");

this.Icon = icon; 

每次都有效。

我將 .ico 文件添加到我的項目中,將 Build Action 設置為Embedded Resource 我在項目設置中指定了該文件的路徑作為項目的圖標,然后我在表單的構造函數中使用了下面的代碼來共享它。 這樣,我不需要在任何地方使用圖標副本維護資源文件。 更新它所需要做的就是替換文件。

var exe = System.Reflection.Assembly.GetExecutingAssembly();
var iconStream = exe.GetManifestResourceStream("Namespace.IconName.ico");
if (iconStream != null) Icon = new Icon(iconStream);

在解決方案資源管理器中,右鍵單擊項目標題並在上下文菜單中選擇“屬性”以打開“項目屬性”表單。 在“應用程序”選項卡中的“資源”組框中,有一個輸入字段,您可以在其中為應用程序選擇所需的圖標文件。

我發現最簡單的方法是:

  1. 將圖標文件添加到您的 WinForms 項目中。
  2. 將圖標文件的構建操作更改為嵌入式資源
  3. 在主窗體加載功能中:

    Icon = LoadIcon("<那個圖標文件的文件名>");

這是給所有forms設置相同圖標的方法,不用一一更改。 這就是我在我的應用程序中編寫的代碼。

FormUtils.SetDefaultIcon();

這里有一個完整的例子可以使用。

static class Program
{
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main(string[] args)
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);

        //Here it is.
        FormUtils.SetDefaultIcon();

        Application.Run(new Form());
    }
}

這是 class FormUtils:

using System.Drawing;
using System.Windows.Forms;

public static class FormUtils
{
    public static void SetDefaultIcon()
    {
        var icon = Icon.ExtractAssociatedIcon(EntryAssemblyInfo.ExecutablePath);
        typeof(Form)
            .GetField("defaultIcon", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static)
            .SetValue(null, icon);
    }
}

這里是 class EntryAssemblyInfo。 此示例已將其截斷。 這是我從 System.Winforms.Application 中獲取的自定義編碼 class。

using System.Security;
using System.Security.Permissions;
using System.Reflection;
using System.Diagnostics;

public static class EntryAssemblyInfo
{
    private static string _executablePath;

    public static string ExecutablePath
    {
        get
        {
            if (_executablePath == null)
            {
                PermissionSet permissionSets = new PermissionSet(PermissionState.None);
                permissionSets.AddPermission(new FileIOPermission(PermissionState.Unrestricted));
                permissionSets.AddPermission(new SecurityPermission(SecurityPermissionFlag.UnmanagedCode));
                permissionSets.Assert();

                string uriString = null;
                var entryAssembly = Assembly.GetEntryAssembly();

                if (entryAssembly == null)
                    uriString = Process.GetCurrentProcess().MainModule.FileName;
                else
                    uriString = entryAssembly.CodeBase;

                PermissionSet.RevertAssert();

                if (string.IsNullOrWhiteSpace(uriString))
                    throw new Exception("Can not Get EntryAssembly or Process MainModule FileName");
                else
                {
                    var uri = new Uri(uriString);
                    if (uri.IsFile)
                        _executablePath = string.Concat(uri.LocalPath, Uri.UnescapeDataString(uri.Fragment));
                    else
                        _executablePath = uri.ToString();
                }
            }

            return _executablePath;
        }
    }
}

我按照 Csmotor 的回答在我的項目中設置圖標,但我還需要一個步驟才能將Icon轉換為ImageSource

我為關心的人分享。

this.Icon = ExportXML.Properties.Resources.IDR_MAINFRAME.ToImageSource();

public static class Helper{
        public static ImageSource ToImageSource(this Icon icon)
        {
            ImageSource imageSource = Imaging.CreateBitmapSourceFromHIcon(
                icon.Handle,
                Int32Rect.Empty,
                BitmapSizeOptions.FromEmptyOptions());

            return imageSource;
        }
    }

最簡單的解決方案在這里:如果您使用的是 Visual Studio,請在Solution Explorer中右鍵單擊您的項目文件。 選擇Properties 選擇Icon and manifest然后瀏覽您的.ico文件。

從項目選項卡中選擇您的項目屬性然后應用程序->資源->圖標和清單->更改默認圖標

這在 Visual Studio 2019 中很好用 注意:只能將 .ico 格式的文件添加為圖標

選擇主窗體 -> 屬性 -> Windows 樣式 -> 圖標 -> 瀏覽你的 ico

this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));

將以下行放在表單的構造函數中:

Icon = Icon.ExtractAssociatedIcon(System.Reflection.Assembly.GetExecutingAssembly().Location);

試試這個,例如在表單構造函數中:

public Form1()
{
  InitializeComponent();

  string imagePath = @"" + AppDomain.CurrentDomain.BaseDirectory + "path-for-icon";
  using (var stream = File.OpenRead(imagePath))
  {
     this.Icon = new Icon(stream);
  }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM