溫馨提示×

C# Graphics文本繪制怎么實現

小億
216
2023-10-22 14:23:17
欄目: 編程語言

在C#中繪制文本可以使用System.Drawing命名空間中的Graphics類。下面是一個簡單的示例代碼,演示如何在窗體上繪制文本:

using System;
using System.Drawing;
using System.Windows.Forms;

public class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Paint(object sender, PaintEventArgs e)
    {
        Graphics g = e.Graphics;

        // 設置文本的字體和顏色
        Font font = new Font("Arial", 12);
        Brush brush = Brushes.Black;

        // 繪制文本
        string text = "Hello, World!";
        Point position = new Point(50, 50);
        g.DrawString(text, font, brush, position);
    }

    private void InitializeComponent()
    {
        this.SuspendLayout();
        // 
        // Form1
        // 
        this.ClientSize = new System.Drawing.Size(300, 200);
        this.Name = "Form1";
        this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form1_Paint);
        this.ResumeLayout(false);
    }

    [STAThread]
    static void Main()
    {
        Application.Run(new Form1());
    }
}

在上述示例中,我們在窗體的Paint事件中創建一個Graphics對象,并使用DrawString方法繪制文本??梢栽O置字體、顏色和位置來繪制不同樣式的文本。

運行該代碼,你將會在窗體上看到繪制的文本。

0
亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女