Code
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
namespace DecoratorGiven
{
/// <summary>
/// 一个组件类。
/// </summary>
public class Photo : Form
{
Image image;
public Photo()
{
image = new Bitmap(@"L:\我的文档\My Pictures\http_imgload2.jpg");
this.Size = image.Size;
this.Text = "Baby Wife";
this.Paint += new PaintEventHandler(Drawer);
}
public virtual void Drawer(object sender, PaintEventArgs e)
{
e.Graphics.DrawImage(image, e.ClipRectangle);//将图片绘画在整个窗体
}
static void Main()
{
Photo p = new Photo();
TaggedPhoto foodTaggedPhoto, colorTagedPhoto, tag;
BorderedPhoto composition;
Hominid h;
Application.Run(p);//显示原始图像
foodTaggedPhoto = new TaggedPhoto(p, "Baby");//添加一个文字标签
colorTagedPhoto = new TaggedPhoto(foodTaggedPhoto, "Love");//再次添加一个文字标签
composition = new BorderedPhoto(colorTagedPhoto, Color.Blue);
//合成标签1、标签2、边框的Photo,最后显示的是一个边框Photo。
h = new Hominid(composition, Color.Green);//Hominid,绿色弧线。
Application.Run(composition);
Console.WriteLine(colorTagedPhoto.ListTaggedPhotos());
Application.Run(h);
p = new Photo();
tag = new TaggedPhoto(p, "Sandy");
composition = new BorderedPhoto(tag, Color.Yellow);
Application.Run(composition);
Console.WriteLine(tag.ListTaggedPhotos());
Console.ReadKey();
}
}
/// <summary>
/// 画了红线。
/// </summary>
public class Hominid : Photo
{
Photo photo;
Color color;
public Hominid(Photo p, Color c)
{
photo = p;
color = c;
}
public override void Drawer(object sender, PaintEventArgs e)
{
photo.Drawer(sender, e);
e.Graphics.DrawEllipse(new Pen(color), new RectangleF(new PointF(this.Location.X / 2, this.Location.Y / 2), new SizeF(this.Width, this.Height)));
}
}
/// <summary>
/// 边框窗体。
/// </summary>
public class BorderedPhoto : Photo
{
Photo photo;
Color color;
public BorderedPhoto(Photo p, Color c)
{
photo = p;
color = c;
this.Size = p.Size;
}
public override void Drawer(object sender, PaintEventArgs e)
{
photo.Drawer(sender, e);//将当前窗体进行重绘(基类窗体)
e.Graphics.DrawRectangle(new Pen(color, 10), e.ClipRectangle);//绘画边框
}
}
/// <summary>
/// 添加文字标签的窗体。
/// </summary>
public class TaggedPhoto : Photo
{
Photo photo;
string tag;
int number;
//static int count;//当前标签总和
static List<string> tags = new List<string>();//当前标签,静态
public TaggedPhoto(Photo p, string t)
{
photo = p;
tag = t;
tags.Add(t);
number = tags.Count;//当前标签位置
}
public override void Drawer(object sender, PaintEventArgs e)
{
photo.Drawer(sender, e);
e.Graphics.DrawString(tag, new Font("宋体", 16), new SolidBrush(Color.Black), new PointF(80, 10 + number * 20));
}
public string ListTaggedPhotos()
{
string s = "所有标签:";
tags.ForEach(delegate(string ss)
{
s += ss;
});
return s;
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
namespace DecoratorGiven
{
/// <summary>
/// 一个组件类。
/// </summary>
public class Photo : Form
{
Image image;
public Photo()
{
image = new Bitmap(@"L:\我的文档\My Pictures\http_imgload2.jpg");
this.Size = image.Size;
this.Text = "Baby Wife";
this.Paint += new PaintEventHandler(Drawer);
}
public virtual void Drawer(object sender, PaintEventArgs e)
{
e.Graphics.DrawImage(image, e.ClipRectangle);//将图片绘画在整个窗体
}
static void Main()
{
Photo p = new Photo();
TaggedPhoto foodTaggedPhoto, colorTagedPhoto, tag;
BorderedPhoto composition;
Hominid h;
Application.Run(p);//显示原始图像
foodTaggedPhoto = new TaggedPhoto(p, "Baby");//添加一个文字标签
colorTagedPhoto = new TaggedPhoto(foodTaggedPhoto, "Love");//再次添加一个文字标签
composition = new BorderedPhoto(colorTagedPhoto, Color.Blue);
//合成标签1、标签2、边框的Photo,最后显示的是一个边框Photo。
h = new Hominid(composition, Color.Green);//Hominid,绿色弧线。
Application.Run(composition);
Console.WriteLine(colorTagedPhoto.ListTaggedPhotos());
Application.Run(h);
p = new Photo();
tag = new TaggedPhoto(p, "Sandy");
composition = new BorderedPhoto(tag, Color.Yellow);
Application.Run(composition);
Console.WriteLine(tag.ListTaggedPhotos());
Console.ReadKey();
}
}
/// <summary>
/// 画了红线。
/// </summary>
public class Hominid : Photo
{
Photo photo;
Color color;
public Hominid(Photo p, Color c)
{
photo = p;
color = c;
}
public override void Drawer(object sender, PaintEventArgs e)
{
photo.Drawer(sender, e);
e.Graphics.DrawEllipse(new Pen(color), new RectangleF(new PointF(this.Location.X / 2, this.Location.Y / 2), new SizeF(this.Width, this.Height)));
}
}
/// <summary>
/// 边框窗体。
/// </summary>
public class BorderedPhoto : Photo
{
Photo photo;
Color color;
public BorderedPhoto(Photo p, Color c)
{
photo = p;
color = c;
this.Size = p.Size;
}
public override void Drawer(object sender, PaintEventArgs e)
{
photo.Drawer(sender, e);//将当前窗体进行重绘(基类窗体)
e.Graphics.DrawRectangle(new Pen(color, 10), e.ClipRectangle);//绘画边框
}
}
/// <summary>
/// 添加文字标签的窗体。
/// </summary>
public class TaggedPhoto : Photo
{
Photo photo;
string tag;
int number;
//static int count;//当前标签总和
static List<string> tags = new List<string>();//当前标签,静态
public TaggedPhoto(Photo p, string t)
{
photo = p;
tag = t;
tags.Add(t);
number = tags.Count;//当前标签位置
}
public override void Drawer(object sender, PaintEventArgs e)
{
photo.Drawer(sender, e);
e.Graphics.DrawString(tag, new Font("宋体", 16), new SolidBrush(Color.Black), new PointF(80, 10 + number * 20));
}
public string ListTaggedPhotos()
{
string s = "所有标签:";
tags.ForEach(delegate(string ss)
{
s += ss;
});
return s;
}
}
}