繼續上一篇文章,將Class改寫為.Net控制項
新增一個Event,如果USER點選到圓圈(Circle),會產生Identify_OK Event.
新增一個屬性SharpNumber,可以設定圖形的複雜度.
新增一個Method GetNext(),可以重新繪製新的認證圖片.
程式碼:
public partial class IdentifyPictureControl : UserControl
{
//Private Variable
private Random myRandom = new Random();
private int mySharpNo;
private Size myPictureSize;
private Bitmap myBitmap;
private Region myRegion;
private Point myMouseLocation;
public int SharpNumber
{
set { mySharpNo = value; }
get { return mySharpNo; }
}
public IdentifyPictureControl()
{
InitializeComponent();
mySharpNo = 20;
myRegion = new Region();
myMouseLocation = new Point(-1, -1);
}
//Event
public event EventHandler Identify_OK;
protected virtual void OnIdentify_OK(EventArgs e)
{
if (Identify_OK != null)
{
Identify_OK(this, e);
}
}
//Private Component Event
private void IdentifyPictureControl_Load(object sender, EventArgs e)
{
DrawANewPicture();
this.BackgroundImage = myBitmap;
}
private void IdentifyPictureControl_Click(object sender, EventArgs e)
{
if (myRegion.IsVisible(myMouseLocation))
{
OnIdentify_OK(EventArgs.Empty);
}
}
private void IdentifyPictureControl_MouseMove(object sender, MouseEventArgs e)
{
myMouseLocation = e.Location;
}
private void IdentifyPictureControl_MouseLeave(object sender, EventArgs e)
{
myMouseLocation = new Point(-1, -1);
}
//Private Function
private void DrawANewPicture()
{
myPictureSize = this.Size;
GraphicsPath myGP = new GraphicsPath();
myBitmap = new Bitmap(this.Width, this.Height);
Graphics myG = Graphics.FromImage(myBitmap);
myG.Clear(Color.White);
//DrawEllipse
int mySize = GetSize(myPictureSize.Height);
Point myPoint = GetLocation(myPictureSize, mySize);
Pen myPen = new Pen(GetColor(), 3);
myG.DrawEllipse(myPen, new Rectangle(myPoint, new Size(mySize, mySize)));
myGP.AddEllipse(new Rectangle(myPoint, new Size(mySize, mySize)));
myRegion = new Region(myGP);
//DrawRectangle
for (int i = 0; i < mySharpNo/2; i++)
{
mySize = GetSize(myPictureSize.Height);
myPoint = GetLocation(myPictureSize, mySize);
myPen = new Pen(GetColor(), 3);
myG.DrawRectangle(myPen, new Rectangle(myPoint, new Size(mySize, mySize)));
}
for (int i = 0; i < mySharpNo/2; i++)
{
mySize = GetSize(myPictureSize.Height);
myPoint = GetLocation(myPictureSize, mySize);
myPen = new Pen(GetColor(), 3);
myG.DrawPie(myPen, new Rectangle(myPoint, new Size(mySize, mySize)), GetAngle(), GetAngle());
}
}
private Point GetLocation(Size PictureSize, int SharpWidth)
{
int X = myRandom.Next(0, PictureSize.Width - SharpWidth - 3); //3 Pen Width
int Y = myRandom.Next(0, PictureSize.Height - SharpWidth - 3); //3 Pen Width
return new Point(X, Y);
}
private int GetSize(int PictureWidth)
{
return (int)(PictureWidth / myRandom.Next(2, 6));
}
private Color GetColor()
{
switch (myRandom.Next(0, 10))
{
case 0:
return Color.Yellow;
case 1:
return Color.Violet;
case 2:
return Color.Green;
case 3:
return Color.DeepPink;
case 4:
return Color.DarkSlateBlue;
case 5:
return Color.DarkRed;
case 6:
return Color.CornflowerBlue;
case 7:
return Color.DarkSalmon;
case 8:
return Color.Gainsboro;
case 9:
return Color.Gold;
case 10:
return Color.Indigo;
default:
return Color.Black;
}
}
private float GetAngle()
{
return (float)(myRandom.Next(10, 350));
}
//Public method
public void GetNext()
{
DrawANewPicture();
this.BackgroundImage = myBitmap;
}
}
範例引用程式碼:
private void button1_Click(object sender, EventArgs e)
{
identifyPictureControl1.SharpNumber = 30;
identifyPictureControl1.GetNext();
}
private void identifyPictureControl1_Identify_OK(object sender, EventArgs e)
{
MessageBox.Show("OK");
}
沒有留言:
張貼留言