25

コードでC#コンソールアプリケーションのアイコンを設定する方法を知っている人はいますか(Visual Studioのプロジェクトプロパティを使用していません)?

4

3 に答える 3

27

プロジェクトのプロパティで変更できます。

このスタック オーバーフローの記事を参照してください: Is it possible to change a console window's icon from .net?

要約するには、Visual Studio でプロジェクト (ソリューションではない) を右クリックし、プロパティを選択します。[アプリケーション] タブの下部には、アイコンを変更できる [アイコンとマニフェスト] のセクションがあります。

于 2011-05-11T12:08:57.783 に答える
24

コードで実行可能ファイルのアイコンを指定することはできません。これは、バイナリファイル自体の一部です。

それが助けになる場合はコマンドラインから使用/win32icon:<file>しますが、アプリケーションのコード内で指定することはできません。ほとんどの場合、アプリケーションのアイコンが表示されていることを忘れないでください。アプリはまったく実行されていません。

これは、エクスプローラーでファイル自体のアイコンを意味していることを前提としています。ファイルをダブルクリックするだけで実行中のアプリケーションのアイコンを意味する場合、それは常にコンソール自体のアイコンになると思います。

于 2009-07-23T08:20:36.287 に答える
6

コードでアイコンを変更するソリューションは次のとおりです。

class IconChanger
{
    public static void SetConsoleIcon(string iconFilePath)
    {
        if (Environment.OSVersion.Platform == PlatformID.Win32NT)
        {
            if (!string.IsNullOrEmpty(iconFilePath))
            {
                System.Drawing.Icon icon = new System.Drawing.Icon(iconFilePath);
                SetWindowIcon(icon);
            }
        }
    }
    public enum WinMessages : uint
    {
        /// <summary>
        /// An application sends the WM_SETICON message to associate a new large or small icon with a window. 
        /// The system displays the large icon in the ALT+TAB dialog box, and the small icon in the window caption. 
        /// </summary>
        SETICON = 0x0080,
    }

    [System.Runtime.InteropServices.DllImport("user32.dll", CharSet = System.Runtime.InteropServices.CharSet.Auto)]
    private static extern IntPtr SendMessage(IntPtr hWnd, int Msg, int wParam, IntPtr lParam);


    private static void SetWindowIcon(System.Drawing.Icon icon)
    {
        IntPtr mwHandle = System.Diagnostics.Process.GetCurrentProcess().MainWindowHandle;
        IntPtr result01 = SendMessage(mwHandle, (int)WinMessages.SETICON, 0, icon.Handle);
        IntPtr result02 = SendMessage(mwHandle, (int)WinMessages.SETICON, 1, icon.Handle);
    }// SetWindowIcon()
}
于 2020-01-24T13:33:11.963 に答える