如何为绘制形状写事件?

时间:2012-08-26 14:22:13

标签: c# system.drawing

我使用此代码绘制了一个圆圈:

private void Form1_Paint(object sender, PaintEventArgs e)
        {
            // Create pen.
            Pen blackPen = new Pen(Color.Black, 3);
            // Create rectangle for ellipse.
            Rectangle rect = new Rectangle(0, 0, 200, 100);
            // Draw ellipse to screen.
            e.Graphics.DrawEllipse(blackPen, rect);
        }

如何为此圈子撰写mouse-overclick事件?

1 个答案:

答案 0 :(得分:1)

您可以在矩形上分配点击事件

public Form1()
        {
            InitializeComponent();
            Rectangle rect = new Rectangle(0, 0, 100, 200);
            Click += Form1_Click;
        }
//associate this method to Click event Form
     private void Form1_Click(object sender, EventArgs e)
        {
            Rectangle rect = new Rectangle(0, 0, 200, 100);
            Point cursorPos = this.PointToClient(Cursor.Position);
            //you are in rectangle so message display
            if (rect.Contains(cursorPos))
            {                
                MessageBox.Show("in");
            }

        }