/// <summary> /// Paint a preview of the <see cref="Image" /> specified by /// the image index provided by the <see cref="PaintValueEventArgs" /> /// </summary> /// <param name="pe">The PaintValue event args</param> public override void PaintValue(PaintValueEventArgs pe) { int imageIndex = -1; // value is the image index if (pe.Value != null) { try { imageIndex = (int)Convert.ToUInt16(pe.Value.ToString()); } catch { } } // no instance, or the instance represents an undefined image if ((pe.Context.Instance == null) || (imageIndex < 0)) { return; } // get the image set ImageSet imageSet = GetImageSet(pe.Context.Instance); // make sure everything is valid if ((imageSet == null) || (imageSet.Count == 0) || (imageIndex >= imageSet.Count)) { return; } // Draw the preview image pe.Graphics.DrawImage(imageSet.Images[imageIndex], pe.Bounds); }
/// <summary> /// Handle a conversion /// </summary> /// <param name="context">designer context</param> /// <param name="culture">globalization info</param> /// <param name="value">The value to be converted</param> /// <param name="destinationType">Target type</param> /// <returns>An instance of the <c>destinationType</c></returns> /// <remarks>This code specifically handles conversion to a <see cref="InstanceDescriptor" /> so that /// the <see cref="ImageSet" /> can be created using a constructor of the form: /// <code> /// new ImageSet(<see cref="System.Drawing.Size" />,<see cref="Color" />) ; /// </code><para> /// Note, the <see cref="InstanceDescriptor" /> is told that the instance may need further /// initialization beyond the values provided to the constructor /// </para></remarks> public override object ConvertTo( ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType ) { if ((destinationType == typeof(InstanceDescriptor)) && (value is ImageSet)) { ImageSet imageSet = value as ImageSet; ConstructorInfo ctorInfo = typeof(ImageSet).GetConstructor(new Type[] { typeof(Size), typeof(Color) }); if (ctorInfo != null) { return(new InstanceDescriptor(ctorInfo, new object[] { imageSet.Size, imageSet.TransparentColor }, false)); } } return(base.ConvertTo(context, culture, value, destinationType)); }
/// <summary> /// When editing an image index value, let the user choose an image from /// a popup that displays all the images in the associated <see cref="ImageSet" /> /// </summary> /// <param name="context">designer context</param> /// <param name="provider">designer service provider</param> /// <param name="value">image index item</param> /// <returns>An image index (selected from the popup) or -1 if the user canceled the /// selection</returns> public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value) { wfes = (IWindowsFormsEditorService) provider.GetService(typeof(IWindowsFormsEditorService)); if ((wfes == null) || (context == null)) { return(null); } // Get the image set ImageSet imageSet = GetImageSet(context.Instance); // anything to show? if ((imageSet == null) || (imageSet.Count == 0)) { return(-1); } // Create an image panel that is close to square Size dims = ImagePanel.CalculateBestDimensions(imageSet.Count, ImagePanel.PanelSizeHints.MinimizeBoth); imagePanel = new ImagePanel((Bitmap)imageSet.Preview, imageSet.Count, dims.Height, dims.Width); // set the current image index value as the default selection imagePanel.DefaultImage = (int)value; // no grid imagePanel.GridColor = Color.Empty; // listen for an image to be selected imagePanel.ImageSelected += new EventHandler(imagePanel_ImageSelected); // show the popup as a drop-down wfes.DropDownControl(imagePanel); // return the selection (or the original value if none selected) return((selectedIndex != -1) ? selectedIndex : (int)value); }