using System;
using System.Drawing;
using System.Windows.Forms;
public class DrawStringExample : Form
{
    public DrawStringExample()
    {
        this.Text = "DrawString Example";
        this.Size = new Size(300, 200);
    }
    protected override void OnPaint(PaintEventArgs e)
    {
        base.OnPaint(e);
        // 创建一个Graphics对象,用于绘制
        Graphics g = e.Graphics;
        // 定义要绘制的字符串
        string text = "Hello, C# Drawing!";
        // 创建字体和画刷
        Font font = new Font("Arial", 16);
        SolidBrush brush = new SolidBrush(Color.Black);
        // 使用DrawString方法绘制字符串
        g.DrawString(text, font, brush, new PointF(50, 50));
        // 释放资源
        brush.Dispose();
        font.Dispose();
    }
    [STAThread]
    public static void Main()
    {
        Application.Run(new DrawStringExample());
    }
}Form 类,并设置窗体标题和大小。OnPaint 方法:这是在窗体需要重绘时调用的方法。我们在这里进行绘制操作。Graphics 对象:通过 PaintEventArgs 获取 Graphics 对象,用于绘制内容。DrawString 方法:使用 Graphics 对象的 DrawString 方法绘制文本到指定位置。希望这段代码能帮助你理解如何在 C# 中使用 DrawString 方法绘制文本。
上一篇:c# using()
下一篇:c#httpclient
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站