在自定义按钮中模仿TextImageRelation的行为

时间:2018-05-18 15:55:18

标签: c# winforms gdi+

我想使用GDI +在WinForms中创建一个自定义按钮。我继承自System.Windows.Forms.Button并重写OnPaint方法以呈现自定义按钮。

我想渲染图片和文字,了解原始System.Windows.Forms.Button的效果,包括尊重TextImageRelationTextAlignImageAlign属性的能力。

我该怎么做?是否有内置方法可以做到这一点?

1 个答案:

答案 0 :(得分:0)

根据您的需要,您可能需要重新创建绘制按钮的逻辑。

您需要使用Graphics方法来执行任务。

public class FirstControl : Control{  
   public FirstControl() {}  
   protected override void OnPaint(PaintEventArgs e) {  
      // Call the OnPaint method of the base class.  
      base.OnPaint(e);  
      // Call methods of the System.Drawing.Graphics object.  
      e.Graphics.DrawString(Text, Font, new SolidBrush(ForeColor), ClientRectangle);  
   }   
}   

您可能需要测量字符串: https://msdn.microsoft.com/en-us/library/6xe5hazb(v=vs.110).aspx

还有这个: https://msdn.microsoft.com/en-us/library/system.windows.forms.buttonrenderer(v=vs.110).aspx