Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在按钮的两侧放置图标_C#_.net_Winforms_Button_Icons - Fatal编程技术网

C# 如何在按钮的两侧放置图标

C# 如何在按钮的两侧放置图标,c#,.net,winforms,button,icons,C#,.net,Winforms,Button,Icons,我需要将图标放在按钮中文本的两侧,如图所示 我一直想弄明白这一点,但似乎不能 我在Red gate Mysql compare中看到了以下示例 我怎么能假装或者做这个 如果不创建自定义按钮并自己绘制图像,我认为这是不可能的 如果您正在寻找一种简单的“伪造”方法,那么最好使用该属性,而不是使用Image: 创建与按钮具有相同纵横比的透明画布 在上面添加两个“图标”,并将它们左右对齐。它应该是这样的: 将图像另存为PNG,并将其用于按钮的BackgroundImage属性 将缩放设置为Zoom1

我需要将图标放在按钮中文本的两侧,如图所示

我一直想弄明白这一点,但似乎不能

我在Red gate Mysql compare中看到了以下示例

我怎么能假装或者做这个


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

如果您正在寻找一种简单的“伪造”方法,那么最好使用该属性,而不是使用
Image

  • 创建与按钮具有相同纵横比的透明画布
  • 在上面添加两个“图标”,并将它们左右对齐。它应该是这样的:

  • 将图像另存为PNG,并将其用于按钮的
    BackgroundImage
    属性

  • 将缩放设置为
    Zoom
    1
  • 你可以走了:)
结果如下:



1如果您不想担心纵横比,可以使用
Stretch
,但这可能不会产生好看的图像。

一个继承
按钮类的自定义控件(我发现在许多情况下它很方便)。

它向标准类添加了一些属性:

公共图像左侧
:左侧图像。
公共图像右侧
:右侧图像。

可以同时为null(使用默认值)。

公共SizeMode ImageSizeMode
:枚举器,绘图模式选择器:

public enum SizeMode : int
{
    Stretch = 0,
    FixedSize,
    StretchMaxSize
}
公共大小ImageFixedSize
:当
SizeMode=FixedSize
时的图像大小
图像始终具有此处定义的相同自定义大小。
公共大小ImageMaxSize
:当
SizeMode=StretchMaxSize
时的图像大小
图像大小按比例增长/收缩,但决不能超过此大小。

所有图形都是在
OnPaint()
事件中动态绘制的(无硬编码行为),因此您可以根据需要对其进行更改。

查看您是否喜欢它(根据需要更改
命名空间)。


此处使用的图像:和。

注意,自定义控件与其特定设计器分离(我无法发布),因此,当您更改图形属性时,必须单击父窗体以查看其应用情况。为此类对象添加您喜欢的设计器。

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;
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用系统组件模型;
使用系统图;
使用System.Drawing.Drawing2D;
使用System.Windows.Forms;
名称空间图形测试
{
类DoubleGButton:按钮,INotifyPropertyChanged
{
公共事件属性更改事件处理程序属性更改;
图像m_ImageLeft=null;
图像m_ImageRight=null;
SizeMode m_ImageSizeMode=SizeMode.Stretch;
尺寸m_ImageFixedSize=新尺寸(24,24);
尺寸m_ImageMaxSize=新尺寸(24,24);
公共枚举SizeMode:int
{
拉伸=0,
固定大小,
StretchMaxSize
}
public DoubleGButton()=>InitializeComponent();
私有void InitializeComponent()
{
this.m_ImageLeft=默认值;
this.m_ImageRight=默认值;
base.MinimumSize=新尺寸(32,24);
this.TextAlign=ContentAlignment.MiddleCenter;
}
公众形象左{
获取{返回this.m_ImageLeft;}
设置{this.m_ImageLeft=value;this.Invalidate();}
公众形象权{
获取{返回this.m_ImageRight;}
设置{this.m_ImageRight=value;this.Invalidate();}
公共SizeMode图像SizeMode{
获取{返回this.m_ImageSizeMode;}
设置{this.m_ImageSizeMode=value;
NotifyPropertyChanged(nameof(this.ImageSizeMode));}
公共大小ImageFixedSize{
获取{返回this.m_ImageFixedSize;}
设置{this.m_ImageFixedSize=值;
NotifyPropertyChanged(nameof(this.ImageFixedSize));}
公共大小ImageMaxSize{
获取{返回this.m_ImageMaxSize;}
设置{this.m_ImageMaxSize=值;
NotifyPropertyChanged(nameof(this.ImageMaxSize));}
私有void NotifyPropertyChanged(字符串PropertyName)
{
这个。使无效();
PropertyChanged?.Invoke(这是新的PropertyChangedEventArgs(PropertyName));
}
受保护的覆盖无效OnPaint(PaintEventArgs e)
{
基础漆(e);
列出ImageBox=GetImageBox();
if(this.m_ImageLeft!=null)
{
e、 Graphics.DrawImage(this.ImageLeft,imagebox[0]);
}
if(this.m_ImageRight!=null)
{
e、 Graphics.DrawImage(this.ImageRight,imageBox[1]);
}
}
私有列表getImageBox()
{
List rects=新列表();
RectangleF rectmageleft=矩形f.空;
RectangleF rectImageRight=矩形F.Empty;
开关(ImageSizeMode)
{
case SizeMode.Stretch:
rectImageLeft=新矩形F(新点F(6,6),新尺寸F(this.Width/10,this.Height-12));
rectImageRight=新矩形F(新点F((this.Width-(this.Width/10))-6,6),
新尺寸(此宽度/10,此高度-12);
打破
案例大小Mode.FixedSize:
float TopPosition=(this.Height-this.ImageFixedSize.Height)/2;
rectImageLeft=新矩形F(新点F(6,顶部位置),