【问题标题】:How to add an image icon before the text of a WinForms button?如何在 WinForms 按钮的文本之前添加图像图标?
【发布时间】:2021-01-25 13:54:52
【问题描述】:

我正在尝试在 WinForms 按钮 87x30 大小的文本之前添加 .ico 48x48 图像:

  button1.BackgroundImageLayout = ImageLayout.Stretch;  
  button1.BackgroundImageLayout = ImageLayout.None;                 
  button1.BackgroundImageLayout = ImageLayout.Zoom;  
  button1.ImageAlign = ContentAlignment.MiddleLeft;
  button1.TextImageRelation = TextImageRelation.ImageBeforeText;
  button1.TextAlign = ContentAlignment.MiddleRight;

结果是:

我想弄清楚,如何将左侧的图像与相关距离上的文本对齐,如下所示:

编辑:

    button1.TextImageRelation = TextImageRelation.ImageBeforeText;
    button1.TextAlign = ContentAlignment.MiddleLeft; /// MiddleRight; // MiddleCenter; 
    button1.ImageAlign = ContentAlignment.MiddleRight; /// MiddleLeft;

结果:

【问题讨论】:

  • 看起来您正在混合 Image 和 BackgroundImage?
  • 你应该将Image属性设置为一个图像,然后ImageAlignTextAlign都应该设置为MiddleLeftTextImageRelation应该是ImageBeforeText
  • 我想知道 Image before the text 应该是什么意思:z.order 或水平放置..
  • 水平/垂直对齐。

标签: c# winforms button ico iconbutton


【解决方案1】:

背景图片属性就像操作系统的桌面背景一样,是一张壁纸,可以拉伸、调整、重复……

因此,您不需要在此处使用BackgroundImage 作为与其文本关联的按钮图标样式图像。

如果您使用 Image 属性并将对齐方式设置为左对齐,文本右对齐,一切正常:

然后您可以根据图像大小和/或文本大小和长度调整这些对齐方式以及宽度和高度以达到所需的结果。


另外,正如我终于找到的duplicate 所指出的那样,要简单地居中,您可以使用TextImageRelation 并将其设置为TextImageRelation.ImageBeforeText 而无需更改对齐方式,并在必要时根据大小增加高度显示的图像有一个干净的结果:

【讨论】:

  • 您好,如果我没看错的话,我已经在编辑中添加了结果,请检查
  • 您需要移除背景图片,并且只使用Image 属性。在属性可视化编辑器中单击BackgroundImage 右侧的...,然后单击Local Resource 部分中的Erase/Remove。如果出现问题,请移除按钮并放下一个新按钮以重置属性。
  • 对不起,我错过了这一点,正确的。但后来我必须调整图像大小
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-07-10
  • 1970-01-01
  • 2021-05-01
相关资源
最近更新 更多