1

我用这段代码画了一个圆圈:

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-over或事件?click

4

1 回答 1

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");
            }

        }
于 2012-08-26T14:28:27.847 回答