简体   繁体   English

c#按钮上的图像和文本是否居中?

[英]c# Image and text on a button, centered in the button?

I have a button on a C# Windows Form form, and want to show an image and some text, side by side, and centered on the button. 我在C#Windows Form表单上有一个按钮,并且想要并排显示图像和一些文本,并以按钮为中心。 I tried aligning the image to the left and the text to the right, and I'm getting this (the periods are spaces): 我尝试将图像左对齐,文本右对齐,然后得到这个信息(句点为空格):

|[IMAGE}.................Text| | [IMAGE} .................文字|

But I want this: 但是我想要这个:

|........[IMAGE] Text........| | ........ [图像]文字........ |

My code looks like this: 我的代码如下所示:

btnChangeStatus.Text = "Change status to SUPPRESSED";
btnChangeStatus.TextAlign = ContentAlignment.MiddleRight;

btnChangeStatus.Image=Image.FromFile(@"J:\nomail.gif");
btnChangeStatus.ImageAlign = ContentAlignment.MiddleLeft;

I've searched here, and found lots of stuff for Java or HTML, but nothing for C#. 我在这里搜索,发现很多Java或HTML的东西,但C#没有。 Any ideas? 有任何想法吗?

Thanks! 谢谢!

Set TextImageRelation to TextImageRelation.ImageBeforeText : TextImageRelation设置为TextImageRelation.ImageBeforeText

btnChangeStatus.TextImageRelation = TextImageRelation.ImageBeforeText;
btnChangeStatus.TextAlign = ContentAlignment.MiddleCenter;        
btnChangeStatus.ImageAlign = ContentAlignment.MiddleCenter;

Specifies that the image is displayed horizontally before the text of a control. 指定在控件文本之前水平显示图像。


UPDATE: You are right, though it sounds like this should do what you want, it's still a little to the left. 更新:您是对的,尽管听起来这应该可以满足您的要求,但它仍然有点偏左。
I tried around a little and using 我尝试了一下并使用

btnChangeStatus.TextImageRelation = TextImageRelation.ImageBeforeText;
btnChangeStatus.TextAlign = ContentAlignment.MiddleRight; // <- right here
btnChangeStatus.ImageAlign = ContentAlignment.MiddleCenter;

leads to the desired result, but I can't tell why the button behaves like that. 会导致期望的结果,但是我无法说出按钮为什么会那样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM