本文介绍了如何在我绘制C#的椭圆上使用鼠标单击事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


如何在我画的椭圆上使用鼠标点击事件

这是我的cosde:

HiHow can i use mouse click event on ellipse that i drew
this is my cosde:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Litlle_Game
{
    public partial class Form1 : Form
    {

        public int x1 = 50, y1 = 50,score;
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Paint(object sender, PaintEventArgs e)
        {

               e.Graphics.FillEllipse(Brushes.Red, x1, y1, 60, 60);
               e.Graphics.FillEllipse(Brushes.Yellow, x1+10, y1+10, 40, 40);
               e.Graphics.FillEllipse(Brushes.Red, x1+20, y1+20, 20, 20);
               e.Graphics.FillEllipse(Brushes.Yellow, x1 + 28, y1 + 28, 4, 4);
               //when click add 100 to score(score+=100;)

        }

    }
}

推荐答案



// required
using System.Drawing;
using System.Drawing.Drawing2D;

private GraphicsPath path;
private Pen bluePen = new Pen(Color.Blue);

private void Form1_Load(object sender, EventArgs e)
{
    path = new System.Drawing.Drawing2D.GraphicsPath();
    path.AddEllipse(100,100,400,300);
}

private void Form1_Paint(object sender, PaintEventArgs e)
{
    e.Graphics.DrawPath(bluePen, path);
}

private void Form1_MouseDown(object sender, MouseEventArgs e)
{
    if(path.IsVisible(PointToScreen(e.Location)))
    {
        // you clicked inside the ellipse
    }
    else
    {
        // you clicked outside the ellipse
    }
}

2。使用WinForms C#中的Visual Basic Lines和Shapes Library,使用以下说明:[]。



我建议你先使用Regions,然后尝试使用VS Shapes。

2. use the Visual Basic Lines and Shapes Library in WinForms C# using the instructions here: [^].

I suggest you start by using Regions, and then try using VS Shapes.


这篇关于如何在我绘制C#的椭圆上使用鼠标单击事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 13:24
查看更多