返回首页

{A}{S0}简介
工具条是一个很好的资产,因为他们为用户提供了一个整洁和友好的用户界面。它可以将图像添加到许多不同的工具条项目。它甚至有可能迫使这些项目出现较大的调整财产ImageScalingSize,然而在pixelisation成本。
通常是可取尽可能使我们的应用程序访问。较大的工具条项与那些较贫穷的视线,甚至那些只是单纯地享受较大的图标的质量梦幻。不幸的是,标准ToolStrip类不提供图标支持。当指定一个图标,它被转换成一个图像实例。因此,使用一个图标的利益都将丢失。
所以我决定,这将是一个不错的主意,支持多图像添加到ToolStrip控件。虽然我的首要重点是添加一些图标的支持,我想,它更理想的推广使用两个接口IImageProvider和IMultipleImageProvider。设计
正在更新MultipleImageToolStrip时,它要求获得所需的大小的图像时可用IImageProvider。
IImageProvider公开两种方法IsImageSupported和GetImage。当多个图标提供商提供,它是可以访问所需的图标,使用的关键。在本文附带的代码,关键代表的ToolStripItem。下图提供了一个使用UML类图设计的概述。
当没有IImageProvider可用的,原始的ToolStripItem保持,并在必要时,以配合整体的工具条大小捉襟见肘。
我的扩展,我决定,4个谨慎的大小,将缓解自定义图像提供商的执行情况。可用尺寸:小(16 × 16),中(24 × 24),大(32 × 32),和超大(48 × 48)。当要求的图像大小是不可用,可以自动取代默认的图标,如果MultipleImageToolStrip.UseUnknownImageSizeIcon设置为true。
图片选择如下:如果图像供应商可从工具条内:如果需要的图像大小是支持的,该图像获得。如果所需要的图像大小是不支持UseUnknownImageSizeIcon设置,默认的图像提供商利用。当没有图像可以获取原始图像使用这些标准,简直是捉襟见肘匹配缩放按钮。如果上面的失败,该项目本身就是一个IImageProvider的,那么它是用来获得所需的图像。最后,如​​果项目ToolStripItem.Tag IImageProvider,那么它是用来获得所需的图像。使用代码
之前备份您的项目,尝试新的东西,它始终是一个好主意!
要使用简单的附加代码:将源文件添加到项目中。重新编译项目。从工具箱拖到窗体或UserControl MultipleImageToolStrip。添加一些按钮。你现在应该有一个ToolStrip的行为和以前一样。
如果你想转换成一个MultipleImageToolStrip现有的ToolStrip:quot;。designer.csquot;这是您的表单或控制相关的文件。打开它,并取代ToolStrip的实例与MultipleImageToolStrip。
为了使项目自动调整大小的,添加一个表单(或控制)的Load事件的处理程序。看看下面看看如何做到这一点的一个例子:

private void Form1_Load(object sender, EventArgs e)

{

    // Begin updating tool strip images. This is important because it prevents

    // the tool strip from refreshing after each image assignment.

    this.iconToolStrip.BeginUpdateImages();



    // Here it is possible to provide an 'IImageProvider' instance.

    this.iconToolStrip.AssignImage(toolStripBack, new IconImageProvider(

        Resources.arrow_left));



    // Or just provide icons themselves.

    this.iconToolStrip.AssignImage(toolStripForward, Resources.arrow_right);

    this.iconToolStrip.AssignImage(toolStripHome, Resources.home);

    this.iconToolStrip.AssignImage(toolStripStop, Resources.stop);



    // Finalize updating.

    this.iconToolStrip.EndUpdateImages();



    // Then to select an initial icon size.

    this.iconToolStrip.ImageSize = ImageSize.Medium;

}

当一个工具条项是永久删除,相关的IImageProvider还必须手动删除。这一决定是因为一个项目往往只是暂时删除,然后重新插入。
如果你总是希望删除相关IImageProvider当一个工具项被删除,添加MultipleImageToolStrip.ItemRemove事件的处理程序:{C}
此外,相关的供应商可以删除与该项目一起:
public void RemoveToolStripItem(ToolStripItem item)

{

    // Remove item itself.

    this.iconToolStrip.Items.Remove(item);

    

    // Remove any associated provider.

    this.iconToolStrip.RemoveImage(item);

}
兴趣点
{A2}工具演示应用程序中使用了意想不到的图标请提供。历史六月十六日,2008年:原始版本发布2009年8月3日,演示和源文件的更新会员盖伊..大号改变序列中的多个工具图标时,发现性能问题。现在应该已经解决了这个问题。

回答

评论会员:another11 时间:2011/12/18
虽然简单,但我这个很长一段时间。感谢
评论会员:。nmichalo 时间:2011/12/18
我有增加2班从你在我的项目的源代码,但是当我试图建立的项目,我得到这个错误

错误1类型或命名空间名称"属性"不存在IconToolStrip"命名空间"(是否缺少程序集引用?)C:\ Visual Studio 2008中\ \测试\测试\ IconToolStrip.cs 8月21日测试

任何建议
评论会员:?lhayes00 时间:2011/12/18
示范项目"属性"命名空间是一种资源的自定义命名空间。我建议您看看"演示"拉链,而不是因为一切已经成立有

请让我知道,如果你仍然有问题。

非常感谢,
LEA海斯
评论会员:GoodLucy 时间:2011/12/18
,未找到"Prooerties"IconToolStrip命名空间
。有人能帮助我
评论会员:?lhayes00 时间:2011/12/18
?你的意思是"Prooerties"或"属性"

你能提供的步骤,我可以按照同样的问题重现吗?



第1步。下载"示范项目"
第2步。 ...


LEA海斯
评论会员:lhayes00 时间:2011/12/18
我惊奇地发现,MultipleImageToolStrip作品ToolStripManager一种

你以为是一种"扩展"ToolStripManager存储MultipleImageToolStrip.ImageSize设置以及

盖伊.. L
评论会员:lhayes00 时间:2011/12/18
这不是一个功能,我想添加到控件本身,但这里有一些说明添加此功能,您可以按照:

第1步。 "IconToolStrip.cs"使用下面的版本:
第2步。在你的窗体设计器中设置的"SaveSettings"属性"真"。
第3步。建立运行。

此代码不使用"ToolStripManager"类,我不认为有任何简单的方法延长。相反,下面使用的应用程序设置文件。请注意两种形式,如果有相同的名称的ToolStrip,他们会发生冲突。

让我知道你是怎么得到!

IconToolStrip.cs:
================={ BR}
using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Configuration;

using System.Drawing;

using System.Runtime.Serialization;

using System.Windows.Forms;

 

using IconToolStrip.Properties;

 

namespace IconToolStrip

{

    public enum ImageSize

    {

        /// <summary>

        /// An image of 16x16 pixels.

        /// </summary>

        Small,

        /// <summary>

        /// An image of 24x24 pixels.

        /// </summary>

        Medium,

        /// <summary>

        /// An image of 32x32 pixels.

        /// </summary>

        Large,

        /// <summary>

        /// An image of 48x48 pixels.

        /// </summary>

        ExtraLarge,

    }

 

    /// <summary>

    /// Implementations of this interface must indicate whether or not the

    /// requested image size is supported. A default image override could

    /// be enforced should the provider report with no support.

    /// </summary>

    public interface IImageProvider

    {

        #region Methods

 

        /// <summary>

        /// Queries the image provider for support of a specific size.

        /// </summary>

        /// <param name="size">Indicated image size.</param>

        /// <returns>Returns true when the requested size is supported.</returns>

        bool IsImageSupported(ImageSize size);

 

        /// <summary>

        /// Fetches an image of the requested size.

        /// </summary>

        /// <param name="size">Size of image to obtain.</param>

        /// <returns>If supported, returns requested image. A value of null

        /// indicates that the requested size is not supported.</returns>

        Image GetImage(ImageSize size);

 

        #endregion

    }

 

    /// <summary>

    /// Implementations of this interface can provide access to multiple

    /// different images of multiple different sizes. A default image override

    /// could be enforced should the provider report with no support.

    /// </summary>

    public interface IMultipleImageProvider

    {

        #region Methods

 

        /// <summary>

        /// Queries the image provider for support of a specific size.

        /// </summary>

        /// <param name="key">Key used to identify an image.</param>

        /// <param name="size">Indicated image size</param>

        /// <returns>Returns true when the requested size is supported.</returns>

        bool IsImageSupported(object key, ImageSize size);

 

        /// <summary>

        /// Fetches an image of the requested size.

        /// </summary>

        /// <param name="key">Key used to identify an image.</param>

        /// <param name="size">Size of image to obtain.</param>

        /// <returns>If supported, returns requested image. A value of null

        /// indicates that the requested size is not supported.</returns>

        Image GetImage(object key, ImageSize size);

 

        #endregion

 

        #region Properties

 

        /// <summary>

        /// Gets count of registered image providers.

        /// </summary>

        int ImageProviderCount { get; }

 

        #endregion

    }

 

    /// <summary>

    /// Provides a collection which pairs image providers with a key object.

    /// The collection can be used to provide access to different images of

    /// different sizes.

    /// </summary>

    public class ImageProviderCollection : Dictionary<object, IImageProvider>, IMultipleImageProvider

    {

        #region Construction and destruction

 

        public ImageProviderCollection()

            : base()

        {

        }

 

        public ImageProviderCollection(int capacity)

            : base(capacity)

        {

        }

 

        public ImageProviderCollection(ImageProviderCollection collection)

            : base(collection)

        {

        }

 

        public ImageProviderCollection(SerializationInfo info, StreamingContext context)

            : base(info, context)

        {

        }

 

        #endregion

 

        #region IMultipleImageProvider Members

 

        /// <summary>

        /// Queries the image provider for support of a specific size.

        /// </summary>

        /// <param name="key">Key used to identify an image.</param>

        /// <param name="size">Indicated image size</param>

        /// <returns>Returns true when the requested size is supported.</returns>

        public bool IsImageSupported(object key, ImageSize size)

        {

            if (!this.ContainsKey(key))

                return false;

            return this[key].IsImageSupported(size);

        }

 

        /// <summary>

        /// Fetches an image of the requested size.

        /// </summary>

        /// <param name="key">Key used to identify an image.</param>

        /// <param name="size">Size of image to obtain.</param>

        /// <returns>If supported, returns requested image. A value of null

        /// indicates that the requested size is not supported.</returns>

        public Image GetImage(object key, ImageSize size)

        {

            if (!this.ContainsKey(key))

                throw new NullReferenceException();

            return this[key].GetImage(size);

        }

 

        /// <summary>

        /// Gets count of registered image providers.

        /// </summary>

        int IMultipleImageProvider.ImageProviderCount

        {

            get { return Count; }

        }

 

        #endregion

    }

 

    /// <summary>

    /// Allows an icon to be used to provide images of different sizes.

    /// </summary>

    public class IconImageProvider : IImageProvider

    {

        #region Construction and destruction

 

        public IconImageProvider(Icon icon)

        {

            this.m_sourceIcon = icon;

        }

 

        public IconImageProvider(System.IO.Stream stream)

        {

            this.m_sourceIcon = new Icon(stream);

        }

 

        public IconImageProvider(string fileName)

        {

            this.m_sourceIcon = new Icon(fileName);

        }

 

        public IconImageProvider(Type type, string resource)

        {

            this.m_sourceIcon = new Icon(type, resource);

        }

 

        #endregion

 

        #region Global Utility Methods

 

        /// <summary>

        /// A utility method which transforms an enumerated image value into

        /// a two-dimensional size.

        /// </summary>

        /// <param name="size">Requested image size.</param>

        /// <returns>Returns a two-dimensional size.</returns>

        public static Size GetIconSize(ImageSize size)

        {

            switch (size)

            {

                case ImageSize.Small:

                    return new Size(16, 16);

                case ImageSize.Medium:

                    return new Size(24, 24);

                case ImageSize.Large:

                    return new Size(32, 32);

                case ImageSize.ExtraLarge:

                    return new Size(48, 48);

                default:

                    throw new NotSupportedException("Invalid image size requested.");

            }

        }

 

        #endregion

 

        #region Events

 

        /// <summary>

        /// Raised when the icon property is changed.

        /// </summary>

        public OldNewEventHandler<Icon> IconChanged;

 

        /// <summary>

        /// Invoked the 'IconChanged' event.

        /// </summary>

        /// <param name="e">Provides access to old and new icons.</param>

        public virtual void OnIconChanged(OldNewEventArgs<Icon> e)

        {

            if (this.IconChanged != null)

                this.IconChanged(this, e);

        }

 

        #endregion

 

        #region IImageProvider Members

 

        /// <summary>

        /// Queries the image provider for support of a specific size.

        /// </summary>

        /// <param name="size">Indicated image size.</param>

        /// <returns>Returns true when the requested size is supported.</returns>

        public bool IsImageSupported(ImageSize size)

        {

            return true;

        }

 

        /// <summary>

        /// Fetches an image of the requested size.

        /// </summary>

        /// <param name="size">Size of image to obtain.</param>

        /// <returns>If supported, returns requested image. A value of null

        /// indicates that the requested size is not supported.</returns>

        public Image GetImage(ImageSize size)

        {

            Icon desiredSize = new Icon(SourceIcon, IconImageProvider.GetIconSize(size));

            return desiredSize.ToBitmap();

        }

 

        #endregion

 

        #region Properties

 

        /// <summary>

        /// Gets or sets the source icon.

        /// </summary>

        public Icon SourceIcon

        {

            get { return this.m_sourceIcon; }

            set

            {

                if (value != this.m_sourceIcon)

                {

                    Icon oldIcon = this.m_sourceIcon;

                    this.m_sourceIcon = value;

                    OnIconChanged(new OldNewEventArgs<Icon>(oldIcon, value));

                }

            }

        }

 

        #endregion

 

        #region Attributes

 

        private Icon m_sourceIcon;

 

        #endregion

    }

 

    /// <summary>

    /// Provides event handlers with the current and proposed image sizes.

    /// Event handlers can decide whether or not to cancel this procedure.

    /// </summary>

    public class ImageSizeChangingEventArgs : CancelEventArgs

    {

        #region Construction and destruction

 

        public ImageSizeChangingEventArgs(ImageSize oldValue, ImageSize newValue)

            : base(false)

        {

            this.m_currentValue = oldValue;

            this.m_newValue = newValue;

        }

 

        #endregion

 

        #region Properties

 

        /// <summary>

        /// Gets the current image size.

        /// </summary>

        public ImageSize CurrentValue

        {

            get { return this.m_currentValue; }

        }

 

        /// <summary>

        /// Gets the proposed image size.

        /// </summary>

        public ImageSize NewValue

        {

            get { return this.m_newValue; }

        }

 

        #endregion

 

        #region Attributes

 

        private ImageSize m_currentValue;

        private ImageSize m_newValue;

 

        #endregion

    }

 

    public delegate void ImageSizeChangingEventHandler(object sender, ImageSizeChangingEventArgs e);

 

    /// <summary>

    /// This toolstrip implements the multiple image provider interface and thus

    /// provides support for automatic image changes based upon the selected

    /// image size.

    /// </summary>

    public class MultipleImageToolStrip : ToolStrip, IMultipleImageProvider, IImageProvider, IPersistComponentSettings

    {

        #region Construction and destruction

 

        public MultipleImageToolStrip()

        {

            this.m_imageProvider = new ImageProviderCollection();

            this.m_defaultProvider = this;

 

            this.m_settings = new PersistentIconToolStripSettings((this as IPersistComponentSettings).SettingsKey);

        }

 

        #endregion

 

        #region Constants

 

        public const ImageSize DefaultImageSize = ImageSize.Small;

 

        #endregion

 

        #region Events

 

        /// <summary>

        /// Raised when image size is about to be changed.

        /// </summary>

        public event ImageSizeChangingEventHandler ImageSizeChanging;

        /// <summary>

        /// Raised when image size is changed.

        /// </summary>

        public event OldNewEventHandler<ImageSize> ImageSizeChanged;

        /// <summary>

        /// Raised when the property 'UseUnknownImageSizeIcon' is changed.

        /// </summary>

        public event EventHandler UseUnknownImageSizeIconChanged;

 

        /// <summary>

        /// Invokes the 'ImageSizeChanging' event handler.

        /// </summary>

        /// <param name="e">Event arguments.</param>

        /// <returns>Returns true when proposed change is accepted.</returns>

        protected virtual bool OnImageSizeChanging(ImageSizeChangingEventArgs e)

        {

            if (e.Cancel)

                return false;

 

            ImageSizeChangingEventHandler handler = this.ImageSizeChanging;

            if (handler != null)

            {

                handler(this, e);

                return !e.Cancel;

            }

 

            return true;

        }

 

        /// <summary>

        /// Invokes the 'ImageSizeChanged' event handler.

        /// </summary>

        /// <param name="e">Event arguments.</param>

        protected virtual void OnImageSizeChanged(OldNewEventArgs<ImageSize> e)

        {

            OldNewEventHandler<ImageSize> handler = this.ImageSizeChanged;

            if (handler != null)

                this.ImageSizeChanged(this, e);

        }

 

        /// <summary>

        /// Invokes the 'UseUnknownImageSizeIconChanged' event handler.

        /// </summary>

        /// <param name="e"></param>

        protected virtual void OnUseUnknownImageSizeIconChanged(EventArgs e)

        {

            // Refresh toolstrip images from providers?

            if (!IsUpdatingImages)

                RefreshItemImages();

 

            // Raise the associated event handler.

            EventHandler handler = this.UseUnknownImageSizeIconChanged;

            if (handler != null)

                this.UseUnknownImageSizeIconChanged(this, e);

        }

 

        #endregion

 

        #region IMultipleImageProvider Members

 

        /// <summary>

        /// Queries the image provider for support of a specific size.

        /// </summary>

        /// <param name="key">Key used to identify an image.</param>

        /// <param name="size">Indicated image size</param>

        /// <returns>Returns true when the requested size is supported.</returns>

        public virtual bool IsImageSupported(object key, ImageSize size)

        {

            return ImageProvider.IsImageSupported(key, size);

        }

 

        /// <summary>

        /// Fetches an image of the requested size.

        /// </summary>

        /// <param name="key">Key used to identify an image.</param>

        /// <param name="size">Size of image to obtain.</param>

        /// <returns>If supported, returns requested image. A value of null

        /// indicates that the requested size is not supported.</returns>

        public virtual Image GetImage(object key, ImageSize size)

        {

            return ImageProvider.GetImage(key, size);

        }

 

        /// <summary>

        /// Gets count of registered image providers.

        /// </summary>

        public virtual int ImageProviderCount

        {

            get { return ImageProvider.Count; }

        }

 

        #endregion

 

        #region IImageProvider Members

 

        /// <summary>

        /// Queries the image provider for support of a specific size.

        /// </summary>

        /// <param name="size">Indicated image size.</param>

        /// <returns>Returns true when the requested size is supported.</returns>

        public virtual bool IsImageSupported(ImageSize size)

        {

            if (DefaultImageProvider == null)

                return false;

            if (DefaultImageProvider == this)

                return true;

 

            return DefaultImageProvider.IsImageSupported(size);

        }

 

        /// <summary>

        /// Fetches an image of the requested size.

        /// </summary>

        /// <param name="size">Size of image to obtain.</param>

        /// <returns>If supported, returns requested image. A value of null

        /// indicates that the requested size is not supported.</returns>

        public virtual Image GetImage(ImageSize size)

        {

            if (DefaultImageProvider == null)

                throw new NullReferenceException();

 

            if (DefaultImageProvider == this)

            {

                Size iconSize = IconImageProvider.GetIconSize(size);

                Icon iconResult = new Icon(Resources.stop, iconSize);

                return iconResult.ToBitmap();

            }

 

            return DefaultImageProvider.GetImage(size);

        }

 

        #endregion

 

        #region IPersistComponentSettings Members

 

        public void LoadComponentSettings()

        {

            // Read settings from the settings provider.

            Settings.Reload();

            ApplySettings();

        }

 

        public void ResetComponentSettings()

        {

            // Reset to the default image size.

            Settings.Reset();

            ApplySettings();

        }

 

        public void SaveComponentSettings()

        {

            // Save settings to the settings provider.

            RetrieveSettings();

            Settings.Save();

        }

 

        protected virtual void ApplySettings()

        {

            this.ImageSize = Settings.ImageSize;

        }

 

        protected virtual void RetrieveSettings()

        {

            Settings.ImageSize = this.ImageSize;

        }

 

        [DefaultValue(false)]

        public bool SaveSettings

        {

            get; set;

        }

 

        string IPersistComponentSettings.SettingsKey

        {

            get { return this.Name; }

            set { throw new NotSupportedException(); }

        }

 

        #endregion

 

        #region ToolStrip Overrides

 

        protected override void Dispose(bool disposing)

        {

            if (disposing)

            {

                try

                {

                    SaveComponentSettings();

                }

                finally

                {

                }

            }

 

            base.Dispose(disposing);

        }

 

        #endregion

 

        #region Methods

 

        /// <summary>

        /// Call to begin a batch image provider update more efficiently.

        /// Each 'BeginImageProviderUpdate' call <b>MUST</b> be paired with

        /// an 'EndImageProviderUpdate' call.

        /// </summary>

        public virtual void BeginUpdateImages()

        {

            IsUpdatingImages = true;

        }

 

        /// <summary>

        /// Call to end a batch image provider update. Please note that any

        /// image refreshements only occur when all nested updates are ended.

        /// </summary>

        /// <param name="refresh">Indicates if image sizes are to be refreshed.</param>

        public virtual void EndUpdateImages(bool refresh)

        {

            if (!IsUpdatingImages)

                throw new NotSupportedException();

 

            IsUpdatingImages = false;

 

            // Only apply updates when image providers have been changed.

            if (HasImagesChanged)

            {

                HasImagesChanged = false;

 

                // If no longer updating image providers (i.e. no nested calls), then

                // refresh the image sizes.

                if (!IsUpdatingImages && refresh)

                    RefreshItemImages();

            }

        }

 

        /// <summary>

        /// Call to end a batch image provider update. Please note that

        /// image refreshements only occur when all nested updates are ended.

        /// </summary>

        public void EndUpdateImages()

        {

            EndUpdateImages(true);

        }

 

        /// <summary>

        /// Assigns an image provider for the specified item.

        /// </summary>

        /// <param name="item">Associated toolstrip item.</param>

        /// <param name="provider">Image provider.</param>

        /// <returns>Returns true when successful.</returns>

        public bool AssignImage(ToolStripItem item, IImageProvider provider)

        {

            if (item == null || provider == null)

                throw new ArgumentException("One or more arguments were null references.");

            if (ContainsImage(item))

                return false;

 

            ImageProvider.Add(item, provider);

            HasImagesChanged = true;

 

            if (!IsUpdatingImages)

                RefreshItemImages();

 

            return true;

        }

 

        /// <summary>

        /// Assigns an image provider for the specified item.

        /// </summary>

        /// <param name="item">Associated toolstrip item.</param>

        /// <param name="item">Associated multi-icon.</param>

        /// <returns>Returns true when successful.</returns>

        public bool AssignImage(ToolStripItem item, Icon icon)

        {

            return AssignImage(item, new IconImageProvider(icon));

        }

 

        /// <summary>

        /// Unregisters an image provider.

        /// </summary>

        /// <param name="item">Associated toolstrip item.</param>

        /// <returns>Returns true when successful.</returns>

        public bool RemoveImage(ToolStripItem item)

        {

            if (ImageProvider.Remove(item))

            {

                HasImagesChanged = true;

 

                if (!IsUpdatingImages)

                    RefreshItemImages();

                return true;

            }

            return false;

        }

 

        /// <summary>

        /// Remove image providers which are not referenced with a <c>ToolStripItem</c>.

        /// </summary>

        /// <returns>Returns count of items removed.</returns>

        public int RemoveUnusedImages()

        {

            List<ToolStripItem> removeList = new List<ToolStripItem>();

            int count = 0;

 

            // Compile a list of all items which are to be removed.

            foreach (ToolStripItem key in ImageProvider.Keys)

                if (!Items.Contains(key as ToolStripItem))

                    removeList.Add(key);

            count = removeList.Count;

 

            // Remove each item from provider collection.

            foreach (ToolStripItem item in removeList)

                RemoveImage(item);

 

            // Make sure that the removal list is disposed of.

            removeList = null;

            return count;

        }

 

        /// <summary>

        /// Searches for the a provider which is associated with a toolstrip item.

        /// </summary>

        /// <param name="item">Toolstrip item.</param>

        /// <returns>Returns true when an associated provider is found.</returns>

        public bool ContainsImage(ToolStripItem item)

        {

            return ImageProvider.ContainsKey(item);

        }

 

        /// <summary>

        /// Forces all images sizes to be refreshed from the respective providers.

        /// </summary>

        protected void RefreshItemImages()

        {

            Size imageSize = IconImageProvider.GetIconSize(ImageSize);

            ImageScalingSize = imageSize;

 

            bool changesMade = false;

            IImageProvider imageProvider = null;

            

            SuspendLayout();

 

            foreach (ToolStripItem item in Items)

            {

                if (item.Size != imageSize)

                {

                    imageProvider = null;

 

                    // If an image provider was registered with the toolstrip then...

                    if (ContainsImage(item))

                    {

                        if (IsImageSupported(item, ImageSize))

                            item.Image = GetImage(item, ImageSize);

                        else if (UseUnknownImageSizeIcon && IsImageSupported(ImageSize))

                            item.Image = GetImage(ImageSize);

 

                        changesMade = true;

                    }

                    else if (item is IImageProvider)

                    {

                        imageProvider = item as IImageProvider;

                    }

                    else if (item.Tag is IImageProvider)

                    {

                        imageProvider = item.Tag as IImageProvider;

                    }

 

                    // If an alternative image provider was found, attempt to use that.

                    if (!changesMade && imageProvider != null)

                    {

                        if (imageProvider.IsImageSupported(ImageSize))

                        {

                            item.Image = imageProvider.GetImage(ImageSize);

                            changesMade = true;

                        }

                    }

 

                    // Were changes made?

                    if (changesMade)

                    {

                        // Automatically adjust the image scaling mode.

                        if (item.Image != null && item.Image.Size == imageSize)

                            item.ImageScaling = ToolStripItemImageScaling.None;

                        else

                            item.ImageScaling = ToolStripItemImageScaling.SizeToFit;

                    }

                }

            }

 

            ResumeLayout();

        }

 

        #endregion

 

        #region Properties

 

        /// <summary>

        /// Gets or sets the default image provider.

        /// </summary>

        public IImageProvider DefaultImageProvider

        {

            get { return this.m_defaultProvider; }

            set { this.m_defaultProvider = value; }

        }

 

        /// <summary>

        /// Gets the active multiple image provider.

        /// </summary>

        protected ImageProviderCollection ImageProvider

        {

            get { return this.m_imageProvider; }

        }

 

        /// <summary>

        /// Gets or sets the active toolstrip item images sizes.

        /// </summary>

        public ImageSize ImageSize

        {

            get { return this.m_imageSize; }

            set

            {

                if (value != this.m_imageSize)

                {

                    ImageSizeChangingEventArgs e = new ImageSizeChangingEventArgs(this.m_imageSize, value);

                    if (OnImageSizeChanging(e))

                    {

                        // Adjust image scaling mode.

                        ImageScalingSize = IconImageProvider.GetIconSize(value);

 

                        // Adjust image size as specified.

                        this.m_imageSize = value;

                        RefreshItemImages();

                        OnImageSizeChanged(new OldNewEventArgs<ImageSize>(e.CurrentValue, value));

                    }

                }

            }

        }

 

        /// <summary>

        /// Gets or sets whether a default icon is used to represent unsupported

        /// image sizes.

        /// </summary>

        public bool UseUnknownImageSizeIcon

        {

            get { return this.m_useUnknownIcon; }

            set

            {

                if (value != this.m_useUnknownIcon)

                {

                    this.m_useUnknownIcon = value;

                    OnUseUnknownImageSizeIconChanged(EventArgs.Empty);

                }

            }

        }

 

        /// <summary>

        /// Gets a value indicating if image providers are being updated.

        /// </summary>

        public bool IsUpdatingImages

        {

            get { return this.m_updatingProviders >

评论会员:iccb1013 时间:2011/12/18
LEA:

我已经实现了你这里有了解,你会不会想搞乱了直接,所以我采取了不同方法的基础上,所有MultipleImageToolStrips(MITS)应为我的应用程序的用户配置的控制的变化。

我只是让他们双点击ToolStripPanel的锁定和解锁立场。它基本上循环通过所有MITS对象和切换紧握的知名度。如果他们双击锁定,那么我调用ToolStripManager摆放好位置。否则,如果应用程序关闭并没有锁定MITS,那么我垫木的位置。

这里的一些源上修改了代码:

        private static List<MultipleImageToolStrip> all;

 

        static MultipleImageToolStrip()

        {

            all = new List<MultipleImageToolStrip>();

        }

 

        public MultipleImageToolStrip()

        {

            this.m_imageProvider = new ImageProviderCollection();

            this.m_defaultProvider = this;

            this.m_settings = new PersistentIconToolStripSettings((this as IPersistComponentSettings).SettingsKey); 

            all.Add(this);

        }

 

...

 

        public static void Lock()

        {

            foreach (MultipleImageToolStrip m in all)

            {

                m.GripStyle = ToolStripGripStyle.Hidden;

            }

        }

 

        public static void Unlock()

        {

            foreach (MultipleImageToolStrip m in all)

            {

                m.GripStyle = ToolStripGripStyle.Visible;

            }

        }

我用这MultipleImageToolStrip.all尝试垫木IMAGESIZE设置以及,因为你的代码似乎中途要带我到一个解决方案。

        public static void SaveAllSettings()

        {

            foreach (MultipleImageToolStrip m in all)

            {

                m.SaveComponentSettings();

            }

        }

 

        public static void LoadAllSettings()

        {

            foreach (MultipleImageToolStrip m in all)

            {

                m.LoadComponentSettings();

            }

        }

然后我从我的应用程序的主要形式有以下代码。

        private void Strips_TopToolStripPanel_DoubleClick(object sender, EventArgs e)

        {

            if (Strips.TopToolStripPanel.Locked)

            {

                Strips.TopToolStripPanel.Locked = false;

                Strips.LeftToolStripPanel.Locked = false;

                Strips.RightToolStripPanel.Locked = false;

                

                MultipleImageToolStrip.Unlock();

            }

            else

            {

                Strips.TopToolStripPanel.Locked = true;

                Strips.LeftToolStripPanel.Locked = true;

                Strips.RightToolStripPanel.Locked = true;

 

                MultipleImageToolStrip.Lock();

                ToolStripManager.SaveSettings(this);

                MultipleImageToolStrip.SaveAllSettings();

            }

        }

 

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)

        {

            if (!Strips.TopToolStripPanel.Locked)

            {

                ToolStripManager.SaveSettings(this);

                MultipleImageToolStrip.SaveAllSettings();

            }

        }

 

        private void Form1_Load(object sender, EventArgs e)

        {

            ToolStripManager.LoadSettings(this);

            MultipleImageToolStrip.LoadAllSettings();

        }

此解决方案不起作用。我看过的user.config项,并不能找到它们。我新的ApplicationSettingsBase,很好奇,如果我失去了一些东西,如果ToolStripManager是具有持久性添加到MultipleImageToolStrip或如果我只是还没有成立一个user.config文件(我需要做吗?)干扰。我假设基础上的属性,您在您的最后发表的文章中添加持久性,而不是应用程序级的用户级别。
再次感谢您的信件。
最好的问候,
盖伊.. L

修改日(星期二),2009年8月4日,日下午11:32
评论会员:Cliffer 时间:2011/12/18
感谢你们出色的后multisized toolstrips
我经常改变一个ToolStrip按钮关联的图标,因为我要指出的状态变化,切换条件,或显示在图像上的一种向导的第一步。
我注意到,当我使用RemoveImage和AssignImage,ToolStrip的变化在两个通话之间的按钮的宽度的长度。在某些情况下,图像改变,每一秒。

是否有任何方式更改,恕不ToolStrip的更新图标?这两个调用更改图标图像的最佳方式?

再次感谢。

最好的问候,
盖伊.. L
评论会员:lhayes00 时间:2011/12/18
您好,

谢谢你的客气话。

我想我已经找到这个问题的根源。以下实施"RefreshItemImages"的方法取代"MultipleImageToolStrip"内有一展身手。下面的版本挂起布局的变化,直到所有的形象已经作了修改。

        /// <summary>

        /// Forces all images sizes to be refreshed from the respective providers.

        /// </summary>

        protected void RefreshItemImages()

        {

            Size imageSize = IconImageProvider.GetIconSize(ImageSize);

            ImageScalingSize = imageSize;

 

            bool changesMade = false;

            IImageProvider imageProvider = null;

            

            SuspendLayout();

 

            foreach (ToolStripItem item in Items)

            {

                if (item.Size != imageSize)

                {

                    imageProvider = null;

 

                    // If an image provider was registered with the toolstrip then...

                    if (ContainsImage(item))

                    {

                        if (IsImageSupported(item, ImageSize))

                            item.Image = GetImage(item, ImageSize);

                        else if (UseUnknownImageSizeIcon && IsImageSupported(ImageSize))

                            item.Image = GetImage(ImageSize);

 

                        changesMade = true;

                    }

                    else if (item is IImageProvider)

                    {

                        imageProvider = item as IImageProvider;

                    }

                    else if (item.Tag is IImageProvider)

                    {

                        imageProvider = item.Tag as IImageProvider;

                    }

 

                    // If an alternative image provider was found, attempt to use that.

                    if (!changesMade && imageProvider != null)

                    {

                        if (imageProvider.IsImageSupported(ImageSize))

                        {

                            item.Image = imageProvider.GetImage(ImageSize);

                            changesMade = true;

                        }

                    }

 

                    // Were changes made?

                    if (changesMade)

                    {

                        // Automatically adjust the image scaling mode.

                        if (item.Image != null && item.Image.Size == imageSize)

                            item.ImageScaling = ToolStripItemImageScaling.None;

                        else

                            item.ImageScaling = ToolStripItemImageScaling.SizeToFit;

                    }

                }

            }

 

            ResumeLayout();

        }
给这一个尝试,让我知道你是怎么得到。如果此修复您的问题,我将提交这篇文章的更新。
报告这个问题,谢谢!

LEA海斯
评论会员:游客 时间:2011/12/18
1016|它。再次感谢