1)Pridejte do Vasi konzolove aplikace reference na tyto 4 Assembly: PresentationCore, System.Drawing, System.Xaml a WindowsBase.
Takhle se pridava assembly PresentationCore, staci stisknout OK!
2)Do souboru(u), kde budeme mit tyto tridy musime mit tyto using directivy. Jedna se o ty co jsou v Program.cs po vytvoreni konzolove aplikace a mnou pridane skrze Resolve:
1 2 3 4 5 6 7 8 9 |
using System; using System.Collections.Generic; using System.Drawing; using System.IO; using System.Linq; using System.Runtime.InteropServices; using System.Text; using System.Threading.Tasks; using System.Windows.Media.Imaging; |
3)Vytvorime si nekolik trid. Jsou to vytazky z mych pomocnych trid, jsou tam ponechany pouze metody, ktere se v teto ukazce pouzivaji:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
class BitmapImageHelper { public static BitmapImage MsAppx(string relPath) { BitmapImage bs = new BitmapImage(new Uri(ImageHelper.protocol + relPath, UriKind.Absolute)); return bs; } } class ImageHelper { public static string protocol = "pack://application:,,,"; public static void SetAssemblyNameForWpfApps(string assemblyName) { protocol += "/" + assemblyName + ";component/"; } public static void RegisterPackProtocol() { if (!UriParser.IsKnownScheme("pack")) new System.Windows.Application(); } } class Pictures { public static Bitmap BitmapImage2Bitmap(BitmapImage bitmapImage) { // BitmapImage bitmapImage = new BitmapImage(new Uri("../Images/test.png", UriKind.Relative)); using (MemoryStream outStream = new MemoryStream()) { BitmapEncoder enc = new BmpBitmapEncoder(); enc.Frames.Add(BitmapFrame.Create(bitmapImage)); enc.Save(outStream); System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(outStream); // return bitmap; <-- leads to problems, stream is closed/closing ... return new Bitmap(bitmap); } } } class W32 { [DllImport("kernel32.dll", SetLastError = true)] public static extern bool SetConsoleIcon(IntPtr hIcon); } |
4)Na konec napiseme do Main() kod, ktery ikonku ke konzolove aplikaci prida:
1 2 3 4 5 6 7 8 9 |
Console.Title = "Konzolova aplikace s logem MS"; //-tempConsoleWithoutSwf je jmenem assembly volajiciho kodu(pokud zdejsi pomocne tridy budou v jinem sestaveni)! ImageHelper.SetAssemblyNameForWpfApps("tempConsoleWithoutSwf"); ImageHelper.RegisterPackProtocol(); BitmapImage bi = BitmapImageHelper.MsAppx("LogoMicrosoft.png"); Bitmap bmp = Pictures.BitmapImage2Bitmap(bi); W32.SetConsoleIcon(bmp.GetHicon()); Console.WriteLine("Program dokoncil svou cinnost, kliknutim na enter jej ukoncite."); Console.ReadLine(); |
5)Nyni uz jen zbyva do rootu projektu pridat obrazkovy soubor LogoMicrosoft.png, idealne ve ctvercove velikosti(zde bylo 50×50) a nastavit mu Build action z Content na Resource. Vysledek vypada pak nejak takto: