如何在我绘制C#的椭圆上使用鼠标单击事件 [英] How Can I Use Mouse Click Event On Ellipse That I Drew C#

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

问题描述


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

这是我的cosde:

Hi How 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;)

        }

    }
}

推荐答案

这应该对你有帮助 - http://www.java2s.com/Tutorial/CSharp/0480__2D/BasicPaintFormclickmousebuttontoaddtheanEllipse.htm [<一个href =http://www.java2s.com/Tutorial/CSharp/0480__2D/BasicPaintFormclickmousebuttontoaddtheanEllipse.htmtarget =_ blanktitle =New Window> ^ ]。
This should help you - http://www.java2s.com/Tutorial/CSharp/0480__2D/BasicPaintFormclickmousebuttontoaddtheanEllipse.htm[^].


你需要深入研究椭圆的基本数学。你需要使用椭圆的一个基本属性:它有两个焦点椭圆曲线的任意点之间的距离之和,每个焦点都是常数



现在,这个距离之和等于椭圆的直径,对于水平/垂直方向的椭圆,你draw是它的宽度或高度,以较大者为准。请注意,您可以查看找到点是椭圆内点的问题,这是圆的同一问题的自然概括。



所以,你的问题现在减少到找到这些本地点的问题。同样,它们水平或垂直放置,取决于椭圆的大小,宽度或高度。您将在此处找到所需的一切: http://en.wikipedia.org/wiki/Ellipse [ ^ ]。



您可以找到给定椭圆的两个焦点的坐标,并使用所有其他渲染数据记住它们。在命中测试中,您在控件的坐标系中具有鼠标点击坐标。采用此坐标并比较距离之和( http://en.wikipedia.org/wiki/Norm_%28mathematics %29 [ ^ ])之间这一点和那些焦点。



-SA
You need to dig into the elementary mathematics of ellipse. You need to use one fundamental property of ellipse: it has two focal points, and the sum of distances between any point of an elliptic curve and each of the foci is the constant.

Now, this sum of distances is equal to the diameter of the ellipse, which is, for the horizontally/vertically oriented ellipses you draw is its width or height, whichever is greater. Note that you can look at the problem of finding if the point is an inner point of an ellipse a natural generalization of the same problem for a circle.

So, your problem is now reduced to the problem of finding these local points. Again, they lie horizontally or vertically, depending on what is greater, the width or the height of the ellipse. You will find all you need here: http://en.wikipedia.org/wiki/Ellipse[^].

You find coordinates of two focal points of a given ellipse and remember them with all other rendering data. In a hit test, you have the mouse hit coordinates in the coordinate system of your control. Takes this coordinate and compare the sum of the distances (http://en.wikipedia.org/wiki/Norm_%28mathematics%29[^]) between this point and the those focal points.

—SA


我想你想要什么是否能够创建保留状态:的图形对象,换句话说,您不必计算每个鼠标单击是在椭圆内部还是外部,您只需使用对象的功能找出它是否包含。



在Windows窗体中有两种方法:



1.使用区域。这是一个简单的WinForms示例:
I think what you want here is the ability to create a graphic object that "retains state:" in other words you do not have to calculate whether each Mouse Click is inside or outside the ellipse, you can just use a function of the object to find out if it contained.

There are two ways to go about this in Windows Forms:

1. Use Regions. Here's a simple WinForms example:
// 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#的椭圆上使用鼠标单击事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆