/// <summary> /// Edits the specified object's value using the editor style indicated by the <see cref="M:System.Drawing.Design.UITypeEditor.GetEditStyle" /> method. /// </summary> /// <param name="context">An <see cref="T:System.ComponentModel.ITypeDescriptorContext" /> that can be used to gain additional context information.</param> /// <param name="provider">An <see cref="T:System.IServiceProvider" /> that this editor can use to obtain services.</param> /// <param name="value">The object to edit.</param> /// <returns>The new value of the object. If the value of the object has not changed, this should return the same object it was passed.</returns> public override object EditValue(System.ComponentModel.ITypeDescriptorContext context, IServiceProvider provider, object value) { if ((provider == null) || (provider.GetService(typeof(IWindowsFormsEditorService)) == null)) { return(value); } if (_filedialog == null) { _filedialog = new OpenFileDialog(); _filedialog.Filter = "Icons(*.ico)|*.ico"; } if (_filedialog.ShowDialog() == DialogResult.OK) { try { using (FileStream stream = new FileStream(_filedialog.FileName, FileMode.Open, FileAccess.Read, FileShare.Read)) { byte[] buffer = new byte[stream.Length]; stream.Read(buffer, 0, buffer.Length); using (MemoryStream mstr = new MemoryStream(buffer)) { value = new IconEncoder(mstr); } } } catch {} } return(value); }
/// <summary> /// adds the specified icon to the collection /// </summary> /// <param name="value">The value.</param> public void Add(IconEncoder value) { if (value == null) { return; } this.List.Add(value); }
/// <summary> /// constructs an icon from a dll resource /// </summary> /// <param name="hlibrary">The hlibrary.</param> /// <param name="resourceid">The resourceid.</param> /// <returns>IconEncoder.</returns> /// <exception cref="System.ArgumentException">resourceid is invalid type - resourceid</exception> /// <exception cref="System.Exception"> /// this is not an icon file /// or /// no iconimages contained /// </exception> private static IconEncoder IconFromLibrary(IntPtr hlibrary, object resourceid) { IntPtr hicon; //is_intresource if (resourceid is int) { hicon = Kernel32.FindResource(hlibrary, (int)resourceid, Kernel32.RT_GROUP_ICON); } else if (resourceid is string) { hicon = Kernel32.FindResource(hlibrary, (string)resourceid, Kernel32.RT_GROUP_ICON); } else { throw new ArgumentException("resourceid is invalid type", "resourceid"); } //open stream MEMICONDIRENTRY[] entries; using (Stream str = Kernel32.GetStreamFromResource(hlibrary, hicon)) { ICONDIR header = new ICONDIR(str); if (header.idType != 1) { throw new Exception("this is not an icon file"); } if (header.idCount < 1) { throw new Exception("no iconimages contained"); } //read headers entries = new MEMICONDIRENTRY[header.idCount]; for (int i = 0; i < entries.Length; i++) { entries[i] = new MEMICONDIRENTRY(str); } } IconEncoder ret = new IconEncoder(); //read images for (int i = 0; i < entries.Length; i++) { //stream for single image using (Stream str = Kernel32.GetStreamFromResource(hlibrary, Kernel32.FindResource(hlibrary, entries[i].ID, Kernel32.RT_ICON))) { ret.Images.Add(IconImage.FromStream(str)); } } return(ret); }
/// <summary> /// Paints a representation of the value of an object using the specified <see cref="T:System.Drawing.Design.PaintValueEventArgs" />. /// </summary> /// <param name="e">A <see cref="T:System.Drawing.Design.PaintValueEventArgs" /> that indicates what to paint and where to paint it.</param> public override void PaintValue(PaintValueEventArgs e) { if (e.Value is IconEncoder) { IconEncoder icn = (IconEncoder)e.Value; Rectangle bounds = e.Bounds; bounds.Width--; bounds.Height--; if (icn.Images.Count > 0) { e.Graphics.DrawImage(icn.Images[0].Bitmap, bounds); } e.Graphics.DrawRectangle(SystemPens.WindowFrame, bounds); } }