如何将我的 ImageButton 添加到 ToolStrip

发布于 2024-10-17 10:43:33 字数 6065 浏览 5 评论 0 原文

创建 IImageButton 并添加到工具条:

ImageButton imageButton1 = new ImageButton();
toolstrip.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
            imageButton1});

崩溃错误

错误 1 ​​无法隐式转换类型 'lient.userControl.ImageButton' 到 'System.Windows.Forms.ToolStripItem' E:\net_project\trunk\Client\Client\userControl\ToolBox.cs 29 15 客户端

我的 ImageButton^

public partial class ImageButton : PictureBox, IButtonControl
    {
        #region Consturctor
        public ImageButton()
        {
            InitializeComponent();
        }

        public ImageButton(IContainer container)
        {
            container.Add(this);

            InitializeComponent();
        }
        #endregion

        private bool isDefault = false;
        private bool isHover = false;
        private bool isDown = false;


        #region IButtonControl Members

        private DialogResult m_DialogResult;
        public DialogResult DialogResult
        {
            get
            {
                return m_DialogResult;
            }
            set
            {
                m_DialogResult = value;
            }
        }

        public void NotifyDefault(bool value)
        {
            isDefault = value;
        }

        public void PerformClick()
        {
            base.OnClick(EventArgs.Empty);
        }

        #endregion

        #region ImageState
        private Image m_HoverImage;
        public Image HoverImage
        {
            get { return m_HoverImage; }
            set 
            { 
                m_HoverImage = value; 
                if (isHover) Image = value; 
            }
        }

        private Image m_DownImage;
        public Image DownImage
        {
            get { return m_DownImage; }
            set 
            { 
                m_DownImage = value; 
                if (isDown) Image = value; 
            }
        }

        private Image m_NormalImage;
        public Image NormalImage
        {
            get { return m_NormalImage; }
            set 
            { 
                m_NormalImage = value; 
                if (!(isHover || isDown)) Image = value; 
            }
        }

        #endregion

        private const int WM_KEYDOWN = 0x0100;
        private const int WM_KEYUP = 0x0101;
        #region Events
        protected override void OnMouseMove(MouseEventArgs e)
        {
            isHover = true;
            if (isDown)
            {
                if ((m_DownImage != null) && (Image != m_DownImage))
                    Image = m_DownImage;
            }
            else
                if (m_HoverImage != null)
                    Image = m_HoverImage;
                else
                    Image = m_NormalImage;
            base.OnMouseMove(e);
        }

        protected override void OnMouseLeave(EventArgs e)
        {
            isHover = false;
            Image = m_NormalImage;
            base.OnMouseLeave(e);
        }

        protected override void OnMouseDown(MouseEventArgs e)
        {
            base.Focus();
            OnMouseUp(null);
            isDown = true;
            if (m_DownImage != null)
                Image = m_DownImage;
            base.OnMouseDown(e);
        }

        protected override void OnMouseUp(MouseEventArgs e)
        {
            isDown = false;
            if (isHover)
            {
                if (m_HoverImage != null)
                    Image = m_HoverImage;
            }
            else
                Image = m_NormalImage;
            base.OnMouseUp(e);
        }

        private bool holdingSpace = false;

        public override bool PreProcessMessage(ref Message msg)
        {
            if (msg.Msg == WM_KEYUP)
            {
                if (holdingSpace)
                {
                    if ((int)msg.WParam == (int)Keys.Space)
                    {
                        OnMouseUp(null);
                        PerformClick();
                    }
                    else if ((int)msg.WParam == (int)Keys.Escape
                        || (int)msg.WParam == (int)Keys.Tab)
                    {
                        holdingSpace = false;
                        OnMouseUp(null);
                    }
                }
                return true;
            }
            else if (msg.Msg == WM_KEYDOWN)
            {
                if ((int)msg.WParam == (int)Keys.Space)
                {
                    holdingSpace = true;
                    OnMouseDown(null);
                }
                else if ((int)msg.WParam == (int)Keys.Enter)
                {
                    PerformClick();
                }
                return true;
            }
            else
                return base.PreProcessMessage(ref msg);
        }

        protected override void OnLostFocus(EventArgs e)
        {
            holdingSpace = false;
            OnMouseUp(null);
            base.OnLostFocus(e);
        }

        protected override void OnPaint(PaintEventArgs pe)
        {
            base.OnPaint(pe);
            if ((!string.IsNullOrEmpty(Text)) && (pe != null) && (base.Font != null))
            {
                SolidBrush drawBrush = new SolidBrush(base.ForeColor);
                SizeF drawStringSize = pe.Graphics.MeasureString(base.Text, base.Font);
                PointF drawPoint;
                if (base.Image != null)
                    drawPoint = new PointF(base.Image.Width / 2 - drawStringSize.Width / 2, base.Image.Height / 2 - drawStringSize.Height / 2);
                else
                    drawPoint = new PointF(base.Width / 2 - drawStringSize.Width / 2, base.Height / 2 - drawStringSize.Height / 2);
                pe.Graphics.DrawString(base.Text, base.Font, drawBrush, drawPoint);
            }
        }

        protected override void OnTextChanged(EventArgs e)
        {
            Refresh();
            base.OnTextChanged(e);
        }
        #endregion

    }

Create IMageButton and add to toolstrip:

ImageButton imageButton1 = new ImageButton();
toolstrip.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
            imageButton1});

crash error

Error 1 Cannot implicitly convert type
'lient.userControl.ImageButton' to
'System.Windows.Forms.ToolStripItem' E:\net_project\trunk\Client\Client\userControl\ToolBox.cs 29 15 Client

My ImageButton^

public partial class ImageButton : PictureBox, IButtonControl
    {
        #region Consturctor
        public ImageButton()
        {
            InitializeComponent();
        }

        public ImageButton(IContainer container)
        {
            container.Add(this);

            InitializeComponent();
        }
        #endregion

        private bool isDefault = false;
        private bool isHover = false;
        private bool isDown = false;


        #region IButtonControl Members

        private DialogResult m_DialogResult;
        public DialogResult DialogResult
        {
            get
            {
                return m_DialogResult;
            }
            set
            {
                m_DialogResult = value;
            }
        }

        public void NotifyDefault(bool value)
        {
            isDefault = value;
        }

        public void PerformClick()
        {
            base.OnClick(EventArgs.Empty);
        }

        #endregion

        #region ImageState
        private Image m_HoverImage;
        public Image HoverImage
        {
            get { return m_HoverImage; }
            set 
            { 
                m_HoverImage = value; 
                if (isHover) Image = value; 
            }
        }

        private Image m_DownImage;
        public Image DownImage
        {
            get { return m_DownImage; }
            set 
            { 
                m_DownImage = value; 
                if (isDown) Image = value; 
            }
        }

        private Image m_NormalImage;
        public Image NormalImage
        {
            get { return m_NormalImage; }
            set 
            { 
                m_NormalImage = value; 
                if (!(isHover || isDown)) Image = value; 
            }
        }

        #endregion

        private const int WM_KEYDOWN = 0x0100;
        private const int WM_KEYUP = 0x0101;
        #region Events
        protected override void OnMouseMove(MouseEventArgs e)
        {
            isHover = true;
            if (isDown)
            {
                if ((m_DownImage != null) && (Image != m_DownImage))
                    Image = m_DownImage;
            }
            else
                if (m_HoverImage != null)
                    Image = m_HoverImage;
                else
                    Image = m_NormalImage;
            base.OnMouseMove(e);
        }

        protected override void OnMouseLeave(EventArgs e)
        {
            isHover = false;
            Image = m_NormalImage;
            base.OnMouseLeave(e);
        }

        protected override void OnMouseDown(MouseEventArgs e)
        {
            base.Focus();
            OnMouseUp(null);
            isDown = true;
            if (m_DownImage != null)
                Image = m_DownImage;
            base.OnMouseDown(e);
        }

        protected override void OnMouseUp(MouseEventArgs e)
        {
            isDown = false;
            if (isHover)
            {
                if (m_HoverImage != null)
                    Image = m_HoverImage;
            }
            else
                Image = m_NormalImage;
            base.OnMouseUp(e);
        }

        private bool holdingSpace = false;

        public override bool PreProcessMessage(ref Message msg)
        {
            if (msg.Msg == WM_KEYUP)
            {
                if (holdingSpace)
                {
                    if ((int)msg.WParam == (int)Keys.Space)
                    {
                        OnMouseUp(null);
                        PerformClick();
                    }
                    else if ((int)msg.WParam == (int)Keys.Escape
                        || (int)msg.WParam == (int)Keys.Tab)
                    {
                        holdingSpace = false;
                        OnMouseUp(null);
                    }
                }
                return true;
            }
            else if (msg.Msg == WM_KEYDOWN)
            {
                if ((int)msg.WParam == (int)Keys.Space)
                {
                    holdingSpace = true;
                    OnMouseDown(null);
                }
                else if ((int)msg.WParam == (int)Keys.Enter)
                {
                    PerformClick();
                }
                return true;
            }
            else
                return base.PreProcessMessage(ref msg);
        }

        protected override void OnLostFocus(EventArgs e)
        {
            holdingSpace = false;
            OnMouseUp(null);
            base.OnLostFocus(e);
        }

        protected override void OnPaint(PaintEventArgs pe)
        {
            base.OnPaint(pe);
            if ((!string.IsNullOrEmpty(Text)) && (pe != null) && (base.Font != null))
            {
                SolidBrush drawBrush = new SolidBrush(base.ForeColor);
                SizeF drawStringSize = pe.Graphics.MeasureString(base.Text, base.Font);
                PointF drawPoint;
                if (base.Image != null)
                    drawPoint = new PointF(base.Image.Width / 2 - drawStringSize.Width / 2, base.Image.Height / 2 - drawStringSize.Height / 2);
                else
                    drawPoint = new PointF(base.Width / 2 - drawStringSize.Width / 2, base.Height / 2 - drawStringSize.Height / 2);
                pe.Graphics.DrawString(base.Text, base.Font, drawBrush, drawPoint);
            }
        }

        protected override void OnTextChanged(EventArgs e)
        {
            Refresh();
            base.OnTextChanged(e);
        }
        #endregion

    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

总以为 2024-10-24 10:43:33

您不能简单地将控件添加到 ToolStripContextStripStatusStrip

它需要继承自ToolStripItem

有一个简单的方法可以做到这一点:

ImageButton imageButton1 = new ImageButton();
var host = new ToolStripControlHost(imageButton1);
toolstrip.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {host});

或者更好的是,扩展 ToolStripControlHost 类,例如:

public class ImageButtonItem: ToolStripControlHost
{
    private ImageButton imgButton;

    public ImageButtonItem()
        : base(new ImageButton())
    {
        this.imgButton = this.Control as ImageButton;
    }

    // Add properties, events etc. you want to expose...
}

遵循此 MSDN howto 了解更多信息。

PS

您还可以在设计器中显示自定义 ImageButtonItem,将 ToolStripItemDesignerAvailability 属性添加到 ImageButtonItem 类中。

You can't simply add a control to a ToolStrip, ContextStrip or StatusStrip.

It needs to inherit from ToolStripItem.

There's a simple way to do it though:

ImageButton imageButton1 = new ImageButton();
var host = new ToolStripControlHost(imageButton1);
toolstrip.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {host});

or better, extend ToolStripControlHost class like:

public class ImageButtonItem: ToolStripControlHost
{
    private ImageButton imgButton;

    public ImageButtonItem()
        : base(new ImageButton())
    {
        this.imgButton = this.Control as ImageButton;
    }

    // Add properties, events etc. you want to expose...
}

follow this MSDN howto to further infos.

P.S.

You can also show your custom ImageButtonItem in the designer adding the ToolStripItemDesignerAvailability attribute onto your ImageButtonItem class.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文