C# DrawString()方法實(shí)例
像Java一樣,C#提供了一整套相當(dāng)豐富的類庫、方法以及事件以供開發(fā)者使用。C#還引入了GDI+,它是由GDI演變而來的,具有比GDI更強(qiáng)大的功能而且簡(jiǎn)化了程序員的編程工作。所以開發(fā)者運(yùn)用這些,就可以很方便的開發(fā)出具有強(qiáng)大圖形圖像功能的應(yīng)用程序了。本文,筆者就通過一些實(shí)例像讀者介紹一下C#中的圖形編程的基本知識(shí)。
C# DrawString()方法簡(jiǎn)單實(shí)例:
首先,讓我們從例子開始,以下是一個(gè)最簡(jiǎn)單的實(shí)例:
- using System;
- using System.Windows.Forms;
- using System.Drawing;
- public class Hello:Form {
- public Hello() {
- this.Paint += new PaintEventHandler(f1_paint);
- }
- private void f1_paint(object sender,PaintEventArgs e) {
- Graphics g = e.Graphics;
- g.DrawString("你好,C#!",new Font("Verdana",20),
- new SolidBrush(Color.Tomato),40,40);
- g.DrawRectangle(new Pen(Color.Pink,3),20,20,150,100);
- }
- public static void Main() {
- Application.Run(new Hello());
- }
- }
在上面的實(shí)例中,我們用到了一個(gè)方法:DrawString(),它帶有5個(gè)參數(shù)。同時(shí),我們發(fā)現(xiàn)在運(yùn)用C# DrawString()方法以前,我們先創(chuàng)建了一個(gè)Graphics類型的對(duì)象g=e.Graphics,這就說明了在運(yùn)用任何圖形類的方法以前我們必須先創(chuàng)建該類的一個(gè)實(shí)例化對(duì)象。在C# DrawString()方法后,我們用到了DrawRectangle()方法,其實(shí)我們還可以運(yùn)用其他的方法來畫橢圓或是多邊形等等。***個(gè)實(shí)例還是相當(dāng)簡(jiǎn)單易懂的,不是嗎?
【編輯推薦】