检查鼠标是否在特定区域被点击 [英] checking if a mouse was clicked in a specific area

查看:41
本文介绍了检查鼠标是否在特定区域被点击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何检查是否在表单的特定坐标平面上单击了鼠标?

how do i check if a mouse was clicked in a specific coordinate plane on the form"?

推荐答案

您可以使用 MouseEventArgs.X 和 MouseEventArgs.Y 来查看它们是否在坐标平面内.

You use the MouseEventArgs.X and MouseEventArgs.Y to see if they are within the coordinate plane.

此答案与我在您上一个问题的回答中发布的链接只需单击一下即可.

This answer was one click away from the link I posted in my answer to on your previous question.

http://msdn.microsoft.com/en-us/library/system.windows.forms.mouseeventargs_members.aspx

已添加

场景:我有一个矩形区域,我想处理点击.

Scenario: I have a Rectangle shaped area I want to handle clicks in.

形状的左上角位于位置 28,83(左、上)

The top left corner of the shape is at location 28,83 (Left, Top)

大小为225、52(宽、高)

The size is 225, 52 (width, height)

因此,如果位置 X(左侧介于 28 和 28 + 225 (253) 之间且位置 Y 介于 83 和 83 + 52 (135) 之间)在边界内.

So if the location X (Left is between 28 and 28 + 225 (253) AND the location Y is between 83 and 83 + 52 (135) is within the bounds.

代码示例:

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 WindowsFormsApplication4
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            this.MouseClick += new MouseEventHandler(Form1_MouseClick);
        }

        void Form1_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.X >= 28 && e.X <= 253 && e.Y >= 83 && e.Y <= 135)
            {
                MessageBox.Show("Clicked within the rectangle");
            }
            else
            {
                MessageBox.Show("Clicked outside the rectangle");
            }
        }
    }
}

这篇关于检查鼠标是否在特定区域被点击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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