- Graphics.DrawString:作法:在 Windows Form 上繪製文字
- TextRenderer.DrawText:作法:使用 GDI 繪製文字
該篇是上述兩篇筆記
using System;
using System.Drawing;
using System.Windows.Forms;
namespace GraphicsSample
{
public partial class FrmText : Form
{
private Font font = new Font("微軟正黑體" , 30);
public FrmText()
{
InitializeComponent();
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
DrawString();
RenderText(e);
}
public void DrawString()
{
using (Graphics g = this.CreateGraphics())
using (SolidBrush b = new SolidBrush(Color.Black))
{
string drawString = "Graphics.DrawString:Sample Text";
float x = 10.0F;
float y = 10.0F;
g.DrawString(drawString, font, b, x, y);
}
}
private void RenderText(PaintEventArgs e)
{
// TextFormatFlags 列舉說明
// Bottom:文字對齊底部
// EndEllipsis:移除已修剪字行的結尾,並以省略符號取代
TextFormatFlags flags = TextFormatFlags.Bottom | TextFormatFlags.EndEllipsis;
TextRenderer.DrawText(e.Graphics, "TextRenderer.DrawText:Sample Text", font,
new Rectangle(10, 40, 700, 100), SystemColors.ControlText, flags);
}
}
}
文章重點
The DrawText methods of the TextRenderer class are not supported for printing. When printing, always use the DrawString methods of the Graphics class.
沒有留言:
張貼留言