我想使用GDI +在WinForms中创建一个自定义按钮。我继承自System.Windows.Forms.Button
并重写OnPaint
方法以呈现自定义按钮。
我想渲染图片和文字,了解原始System.Windows.Forms.Button
的效果,包括尊重TextImageRelation
,TextAlign
和ImageAlign
属性的能力。
我该怎么做?是否有内置方法可以做到这一点?
答案 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