摘要:Simple Paint Object
前年用 C# 寫過九珠算盤,但這兩年來沒有再多作 C# 練習,連兩年前看一半的 Graphics Programming with GDI+ 又都還它了 語言就是這樣,一不說,就忘了如何說。
在寫九珠算盤時,使用自定物件,由一串珠到一個算盤。Simple Paint Object 是取自九珠算盤內的自定物件之最精簡的物件,只要把要畫的東西,寫在 MyPaint 內即可,原始碼如下:
- public class SimplePaintObject : System.Windows.Forms.Panel
- {
- public SimplePaintObject()
- {
- this.Paint += new PaintEventHandler(this.MyPaint);
- // DoubleBuffer 減少閃爍
- this.SetStyle(ControlStyles.AllPaintingInWmPaint
- | ControlStyles.UserPaint
- | ControlStyles.DoubleBuffer, true);
- }
- protected virtual void MyPaint(object sender, System.Windows.Forms.PaintEventArgs e)
- {
- // Obtain the Graphics object
- Graphics g = e.Graphics;
- // Set the smoothing mode of the surface
- g.SmoothingMode = SmoothingMode.AntiAlias;
- // Paint it
- Rectangle r = this.ClientRectangle;
- r.Height--;
- r.Width--;
- g.DrawRectangle(Pens.Blue, r);
- }
- }
public class SimplePaintObject : System.Windows.Forms.Panel
{
public SimplePaintObject()
{
this.Paint += new PaintEventHandler(this.MyPaint);
// DoubleBuffer 減少閃爍
this.SetStyle(ControlStyles.AllPaintingInWmPaint
| ControlStyles.UserPaint
| ControlStyles.DoubleBuffer, true);
}
protected virtual void MyPaint(object sender, System.Windows.Forms.PaintEventArgs e)
{
// Obtain the Graphics object
Graphics g = e.Graphics;
// Set the smoothing mode of the surface
g.SmoothingMode = SmoothingMode.AntiAlias;
// Paint it
Rectangle r = this.ClientRectangle;
r.Height--;
r.Width--;
g.DrawRectangle(Pens.Blue, r);
}
}
在 MyPaint 中利用 Graphics 去作畫,可讓元件在被要求重繪時,自動叫用,這樣繪製的東西就不會因其他視窗蓋掉再移開而清除了。
最近,想寫一個注音練習的程式給兒子作練習,加強ㄥㄣ的辨別,將使用此 Simple Paint Object 作出繪製國字加注音的字,等有成果再貼圖來看看吧。