答案 0 :(得分:5)
如果不创建自定义按钮并自己绘制图像,我认为这是不可能的。
如果您正在寻找一种简单的“伪造”解决方法,那么最好的选择是使用BackgroundImage
属性而不是使用Image
:
在其上添加两个“图标”,并将它们左右对齐。它应该看起来像这样:
将图像另存为PNG,并将其用于按钮的BackgroundImage
属性。
BackgroundImageLayout
设置为Zoom
1 。结果如下:
1 如果您不想担心长宽比,可以使用Stretch
来代替,但这可能不会产生美观的图像。
答案 1 :(得分:3)
自定义控件继承了Button
类(和INotifyPropertyChanged,在很多情况下我都很方便)。
它为标准Button类添加了一些属性:
public Image ImageLeft
:左侧图片。
public Image ImageRight
:右侧图片。
都可以为null(使用默认值)。
public SizeMode ImageSizeMode
:枚举器,绘图模式的选择器:
public enum SizeMode : int
{
Stretch = 0,
FixedSize,
StretchMaxSize
}
public Size ImageFixedSize
:SizeMode = FixedSize
时的图像大小。
图片始终具有此处定义的相同自定义尺寸。
public Size ImageMaxSize
:SizeMode = StretchMaxSize
时的图像大小。
图片大小按比例增长/缩小,但永远不会超过此大小。
所有工程图都是在OnPaint()
事件中动态生成的(没有硬编码的行为),因此您可以根据需要进行更改。
看看是否喜欢(根据需要更改NameSpace
)。
注意,自定义控件与其特定的设计器分离(我无法发布),因此,当您更改图形属性时,必须单击父窗体以看到它的应用。为这些对象添加所需的Designer。
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;
}
}
}