代码之家  ›  专栏  ›  技术社区  ›  Smith

如何在按钮两侧放置图标

  •  0
  • Smith  · 技术社区  · 7 年前

    我需要把图标放在一个按钮上,就像图片上显示的那样。

    我有段时间一直在想办法,但似乎不能。

    我在红门Mysql比较中看到了下面的例子。

    我怎么能假装呢?

    enter image description here

    2 回复  |  直到 7 年前
        1
  •  5
  •   41686d6564    7 年前

    如果不创建自定义按钮并自己绘制图像,我认为这是不可能的。

    如果你想找一个简单的方法来“伪装”,你最好的办法就是使用 BackgroundImage 属性而不是使用 Image :

    • 创建一个与按钮具有相同长宽比的透明画布。
    • 在上面加上两个“图标”,然后左右对齐应该是这样的:

      Transparent canvas

    • 将图像保存为PNG并将其用于 背景图像 按钮的属性。

    • 设置 BackgroundImageLayout Zoom .
    • 你很适合去:)

    结果如下:

    Buttons


    你可以用 Stretch 相反,如果你不想担心纵横比,但这可能不会导致一个好看的图像。

        2
  •  3
  •   Jimi    6 年前

    继承 Button 类(和 INotifyPropertyChanged ,我发现它在很多情况下都很方便)。

    它为标准添加了一些属性 Button

    public Image ImageLeft :左侧图像。
    public Image ImageRight :右侧图像。

    两者都可以为空(使用默认值)。

    public SizeMode ImageSizeMode :枚举器,绘图模式的选择器:

    public enum SizeMode : int
    {
        Stretch = 0,
        FixedSize,
        StretchMaxSize
    }
    

    public Size ImageFixedSize :图像大小 SizeMode = FixedSize .
    图像总是具有此处定义的相同自定义大小。
    public Size ImageMaxSize :图像大小 SizeMode = StretchMaxSize .
    图像大小按比例增大/缩小,但决不能超过此大小。

    所有图纸均在 OnPaint() 事件是动态的(没有硬编码的行为),因此您可以根据需要对其进行更改。

    看看你是否喜欢 NameSpace 根据需要)。

    enter image description here

    此处使用的图像: Left Right .

    作为注释 ,自定义控件将从其特定的设计器中分离(我无法发布),因此,当更改图形属性时,必须单击父窗体才能看到它已应用为此类对象添加您喜欢的设计器。

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Drawing;
    using System.Drawing.Drawing2D;
    using System.Windows.Forms;
    
    namespace GraphicsTests
    {
        class DoubleGButton : Button, INotifyPropertyChanged
        {
            public event PropertyChangedEventHandler PropertyChanged;
            Image m_ImageLeft = null;
            Image m_ImageRight = null;
            SizeMode m_ImageSizeMode = SizeMode.Stretch;
            Size m_ImageFixedSize = new Size(24, 24);
            Size m_ImageMaxSize = new Size(24, 24);
    
            public enum SizeMode : int
            {
                Stretch = 0,
                FixedSize,
                StretchMaxSize
            }
    
            public DoubleGButton() => InitializeComponent();
    
            private void InitializeComponent()
            {
                this.m_ImageLeft = default;
                this.m_ImageRight = default;
                base.MinimumSize = new Size(32, 24);
                this.TextAlign = ContentAlignment.MiddleCenter;
            }
    
            public Image ImageLeft {
                get { return this.m_ImageLeft; }
                set { this.m_ImageLeft = value; this.Invalidate(); } }
            public Image ImageRight {
                get { return this.m_ImageRight; }
                set { this.m_ImageRight = value; this.Invalidate(); } }
    
            public SizeMode ImageSizeMode {
                get { return this.m_ImageSizeMode; }
                set { this.m_ImageSizeMode = value;
                      NotifyPropertyChanged(nameof(this.ImageSizeMode)); } }
    
            public Size ImageFixedSize {
                get { return this.m_ImageFixedSize; }
                set { this.m_ImageFixedSize = value;
                      NotifyPropertyChanged(nameof(this.ImageFixedSize)); } }
    
            public Size ImageMaxSize {
                get { return this.m_ImageMaxSize; }
                set { this.m_ImageMaxSize = value;
                      NotifyPropertyChanged(nameof(this.ImageMaxSize)); } }
    
            private void NotifyPropertyChanged(string PropertyName)
            {
                this.Invalidate();
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(PropertyName));
            }
    
    
            protected override void OnPaint(PaintEventArgs e)
            {
                base.OnPaint(e);
                List<RectangleF> ImageBoxes = GetImageBoxes();
                if (this.m_ImageLeft != null)
                {
                    e.Graphics.DrawImage(this.ImageLeft, ImageBoxes[0]);
                }
                if (this.m_ImageRight != null)
                {
                    e.Graphics.DrawImage(this.ImageRight, ImageBoxes[1]);
                }
            }
    
            private List<RectangleF> GetImageBoxes()
            {
                List<RectangleF> rects = new List<RectangleF>();
                RectangleF rectImageLeft = RectangleF.Empty;
                RectangleF rectImageRight = RectangleF.Empty;
                switch (ImageSizeMode)
                {
                    case SizeMode.Stretch:
                        rectImageLeft = new RectangleF(new PointF(6, 6), new SizeF(this.Width / 10, this.Height - 12));
                        rectImageRight = new RectangleF(new PointF((this.Width - (this.Width / 10)) - 6, 6), 
                                                        new SizeF(this.Width / 10, this.Height - 12));
                        break;
                    case SizeMode.FixedSize:
                        float TopPosition = (this.Height - this.ImageFixedSize.Height) / 2;
                        rectImageLeft = new RectangleF(new PointF(6, TopPosition), 
                                                       new SizeF(this.ImageFixedSize.Width, this.ImageFixedSize.Height));
                        rectImageRight = new RectangleF(new PointF(this.Width - this.ImageFixedSize.Width - 6, TopPosition), 
                                                        new SizeF(this.ImageFixedSize.Width, this.ImageFixedSize.Height));
                        break;
                    case SizeMode.StretchMaxSize:
                        float BoxHeight = (this.Height - 12 > this.ImageMaxSize.Height) ? this.ImageMaxSize.Height : this.Height - 12;
                        float TopBoxPosition = (this.Height - BoxHeight) / 2;
                        float imageHeight = (BoxHeight > this.ImageMaxSize.Height) ? this.ImageMaxSize.Height : BoxHeight;
                        float imageWidth = this.ImageLeft.Width / (this.ImageLeft.Height / imageHeight);
                        imageWidth = (imageWidth > this.ImageMaxSize.Width) ? this.ImageLeft.Width : imageWidth;
                        rectImageLeft = new RectangleF(new PointF(6, TopBoxPosition), 
                                                       new SizeF(imageWidth, imageHeight));
                        rectImageRight = new RectangleF(new PointF(this.Width - imageWidth - 6, TopBoxPosition), 
                                                        new SizeF(imageWidth, imageHeight));
                        break;
                    default:
                        break;
                }
                rects.AddRange(new[] { rectImageLeft,  rectImageRight });
                return rects;
            }
        }
    }