2010年4月1日 星期四

EventHandler (加掛 Class 的 event 處理函數)

加掛 Class 的 event 處理函數

public CustomColorCheckBox()
{
this.checkColor = this.ForeColor;
this.Paint += new PaintEventHandler(this.PaintHandler);
this.Click += new EventHandler(this.OnClick1);
this.KeyUp += new KeyEventHandler(OnKeypress);
}

public void OnKeypress(object sender, KeyEventArgs e)
{
MessageBox.Show(e.KeyCode.ToString(), "Your input");
}


public void OnClick1(object sender, EventArgs e)
{
MessageBox.Show("Hello C#");
}

private void PaintHandler (object sender, PaintEventArgs pe)
{
if (this.Checked)
{
Point pt = new Point();

if (this.CheckAlign == ContentAlignment.BottomCenter)
{
pt.X = (this.Width / 2) - 4;
pt.Y = this.Height - 11;
}
if (this.CheckAlign == ContentAlignment.BottomLeft)
{
pt.X = 3;
pt.Y = this.Height - 11;
}
if (this.CheckAlign == ContentAlignment.BottomRight)
{
pt.X = this.Width - 11;
pt.Y = this.Height - 11;
}
if (this.CheckAlign == ContentAlignment.MiddleCenter)
{
pt.X = (this.Width / 2) - 4;;
pt.Y = (this.Height / 2) - 4;
}
if (this.CheckAlign == ContentAlignment.MiddleLeft)
{
pt.X = 3;
pt.Y = (this.Height / 2) - 4;
}
if (this.CheckAlign == ContentAlignment.MiddleRight)
{
pt.X = this.Width - 11;
pt.Y = (this.Height / 2) - 4;
}
if (this.CheckAlign == ContentAlignment.TopCenter)
{
pt.X = (this.Width / 2) - 4;
pt.Y = 3;
}
if (this.CheckAlign == ContentAlignment.TopLeft)
{
pt.X = 3;
pt.Y = 3;
}
if (this.CheckAlign == ContentAlignment.TopRight)
{
pt.X = this.Width - 11;
pt.Y = 3;
}
DrawCheck(pe.Graphics, this.checkColor,pt);
}
}


沒有留言: