L
Leo
I am writing a customized control to draw a ellipse. I want it to
response key events and mouse events. However only mouse events got
fired. Key events doesn't response. Any idea? Thanks very much in
advance.
Jingcheng
public class GraphNode : Control
{
protected override void OnPaint(PaintEventArgs e)
{
using (Pen blackPen = new Pen(Color.Black, 1))
{
using( Brush blueBrush = new SolidBrush(Color.LightSkyBlue),
blackBrush = new SolidBrush(this.ForeColor))
{
string test = "this is a test";
SizeF stringSize = e.Graphics.MeasureString(test, this.Font);
float xText = (width - stringSize.Width) / 2.0F;
float yText = height / 2.0F - 5.0F;
e.Graphics.DrawEllipse(blackPen, 5, 5, width, height);
e.Graphics.FillEllipse(blueBrush, 5, 5, width, height);
e.Graphics.DrawString(test, this.Font, blackBrush, xText, yText);
}
}
base.OnPaint(e);
}
protected override void OnMouseDown(MouseEventArgs e)
{
...
}
protected override void OnKeyDown(KeyEventArgs e)
{
int i = 0;
if (e.KeyCode == Keys.Delete)
{
this.Dispose();
}
}
response key events and mouse events. However only mouse events got
fired. Key events doesn't response. Any idea? Thanks very much in
advance.
Jingcheng
public class GraphNode : Control
{
protected override void OnPaint(PaintEventArgs e)
{
using (Pen blackPen = new Pen(Color.Black, 1))
{
using( Brush blueBrush = new SolidBrush(Color.LightSkyBlue),
blackBrush = new SolidBrush(this.ForeColor))
{
string test = "this is a test";
SizeF stringSize = e.Graphics.MeasureString(test, this.Font);
float xText = (width - stringSize.Width) / 2.0F;
float yText = height / 2.0F - 5.0F;
e.Graphics.DrawEllipse(blackPen, 5, 5, width, height);
e.Graphics.FillEllipse(blueBrush, 5, 5, width, height);
e.Graphics.DrawString(test, this.Font, blackBrush, xText, yText);
}
}
base.OnPaint(e);
}
protected override void OnMouseDown(MouseEventArgs e)
{
...
}
protected override void OnKeyDown(KeyEventArgs e)
{
int i = 0;
if (e.KeyCode == Keys.Delete)
{
this.Dispose();
}
}