以编程方式在固定宽度面板上创建按钮c#

时间:2017-09-19 06:21:39

标签: c# winforms

我在页面左侧创建了一个面板。我以编程方式在此面板上添加了按钮(图像顶部和文本底部)。面板的宽度固定,当我设置按钮的高度时,如果文本太长,图像和文本重叠。但我不知道增加按钮的高度。

我添加了这个按钮,如

btnFav.Size = new System.Drawing.Size(90, 50);

enter image description here

当我写这样的Autosize show时,

enter image description here

顺便说一下,

btnFav.ImageAlign = System.Drawing.ContentAlignment.TopCenter;
btnFav.TextAlign = ContentAlignment.BottomCenter;

1 个答案:

答案 0 :(得分:0)

为什么不自定义控件? 很容易做到,只需指定需要放置文本的矩形,然后将图像放在旁边或顶部或下面。

EG。

覆盖onpaint()方法

Rectangle textRect = new Rectangle(0, Height / 2, Width, Height / 2);
e.Graphics.DrawString(Text, Font, Brush, textRect, StringAlignment);
e.Graphics.DrawImage(MyImage, 0, 0);

这会将图像置于文本上方,也是一种更清晰的操作方式