通过点击获取鼠标的x,y [英] get x,y of mouse by click

查看:62
本文介绍了通过点击获取鼠标的x,y的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

欢迎所有对编程感兴趣的人:



如何获得鼠标的x,y当鼠标点击页面的任何位置时(桌面) )或显示它





请帮助我现在我没有使用Hook

解决方案

请参阅我对该问题的评论。答案可能会有所不同,具体取决于UI库,但想法是相同的。



此信息在鼠标事件的偶数参数中传递,例如单击事件。它可能如下所示:

 myControlOrFrameworkElement.Click + =(sender,eventArgs)= >  { 
点clickLocation = new Point(eventArgs.X,eventArgs.Y);
ClickHandler(eventArgs.Button,clickLocation);
};



您将能够在您正在使用的UI库的文档中找到相关类型和帮助。







由于OP需要更详细的代码并澄清所使用的UI库,我将向其解释结束:



 使用 System.Windows.Forms; 
使用 Point = System.Drawing.Point; // 见下文
// ...

partial public class MyForm:Form {

Panel myPanel = new Panel();


public MyForm(){ // 构造函数
// ...
// 假设您有一些想要点击的控件(比方说,Panel)
// 通常,人们使用设计师生成此代码,
// 但是从这一点开始调用它:
// 设置myPanel的属性,然后:
.Controls。加入(myPanel);
// 将事件处理程序添加到事件实例myPanel.Click的调用列表中:
myPanel.Click + =(sender,eventArgs)= > {
点clickLocation = new Point(eventArgs.X,eventArgs.Y);
ClickHandler(eventArgs.Button,clickLocation);
}; myPanel.Click
} // MyWindow

// 其中:
void ClickHander( MouseButtons whatIsClicked,指向whereClicked){
// 例如:
MessageBox。显示(
string .Format( Clicked :{0} at({1},{2}),whatIsClicked,whereClicked);
this .Text);
} // ClickHander

} // 类MyForm





我希望现在很清楚。原则上,您只能在此类中使用一个文件,只需添加入口点 Main 。它应该与您从头创建Forms项目时已有的相同;会在自动生成的代码中看到它:

  class 计划{

static void Main(){
// ...
Application.Start( new MyForm());
} // 主要

} // 课程





-SA


如果要在WinForm应用程序的窗口(表单)之外检测鼠标单击,例如:桌面(你特别提到):



必须使用Global Event Hook。 CodeProject上有许多用于实现Global Mouse Hooks的资源;多年来多次更新的经典之作是George Mamaladze:[ ^ ]。



WinForm应用程序的窗口中(表格):



1.每个具有直观表示的对象(技术上,DisplayRectange和ClientRectangle ......换句话说就是一个窗口的形式)在运行时的UI中(Control,UserControl,Form本身)可以为任何各种MouseDown / MouseUp / MouseMove事件实现Click EventHandler或EventHandlers。



如果在A Form的范围内单击鼠标,或者在Form中包含Control / UserControl,那么 鼠标相关事件连线到您已定义的EventHandler:那些(那些)EventHandler将拥有你执行的代码。



2.在Click EventHandler中你做获取infor关于在处理程序接收的EventArgs结构中单击鼠标的位置;但是,在MouseDown / Up / Move事件中,你会获得传递给EventHandler的MouseEventArgs结构中的信息。



3.当你有一个MouseEventArgs实例时(在在一个用于鼠标/向下/向上/向移动的EventHandler中,变量总是名为'e),关于鼠标向下移动位置的信息包含在:e.Location,eX和eY:



4.这些坐标始终是控件或表格的坐标系统中鼠标事件的坐标,在其可见区域中执行Click或其他其他鼠标事件; so:location 0,0始终表示Control或Form的客户端矩形的左上角。换句话说,坐标总是相对到控件或表格的位置:它们不是屏幕坐标。



5.控件或表单的位置坐标始终相对于其容器:对于表单,容器是屏幕本身 (除非你犯了将表格放在另一个表格或控件中的可怕错误)。



对于Control / UserControl:容器可以是Form或另一个控制(你可以有嵌套控件)。



6.如果你想知道鼠标事件所在的屏幕坐标地点:



a。您可以在所有事件中使用无处不在(在所有表格,控件上)可用的MousePosition方法来获取当前时刻鼠标的屏幕坐标 [ ^ ]。



b。在控件或表格的坐标空间中占据一个位置的其他方法,并将其转换为包含表格或其他控件的坐标空间,或者转换为屏幕坐标,可以在WinForms中通过PointToScreen和PointToClient方法可以单独使用,也可以作为方法使用所有控件[ ^ ]。



我建议你试试以下实验:



1.创建一个Windows窗体应用程序。



2.将一个Panel放在主窗体,'panel1



3.连接用于'panel1的MouseDown EventHandler,并使用此代码

 private void panel1_MouseDown(object sender,MouseEventArgs e)
{
Point formLocation = this.Location;

Point panelLocationInForm = panel1.Location;

Point mouseDownLocationInPanel = e.Location;

//?
Point Point0 = MousePosition;

//?
Point Point1 = panel1.PointToScreen(e.Location);

//?
Point Point2 = this.PointToScreen(e.Location);
}

现在,在MouseDown EventHandler的闭合开括号的代码中放置一个断点,并检查不同变量中的值:



当你能理解所涉及的原理,并预测每种情况下这些值的含义:你将了解鼠标的位置,你将会准备好了解PointToClient。



努力工作,此时掌握位置的基本方面,WinForms中的事件位置将使您能够在将来解决任何类型的问题:)


访问这里..



http://www.daniweb.com/software-development/csharp/threads/317766/mouse-coordinates-within-a-form [< a href =http://www.daniweb.com/software-development/csharp/threads/317766/mouse-coordinates-within-a-formtarget =_ blanktitle =New Window> ^ ]







http://stackoverflow.com/questions/3852427 / how-can-i-get-xy-positions-of-mouse-relative-to-form-even-when-click-on-ano [ ^

Hello to all those interested in programming :

How can i do get x,y of mouse When the mouse clicked anywhere on the page(desktop) or display its


Please help me now I do not worked with Hook

解决方案

Please see my comment to the question. The answer can be different depending on the UI library, but ideas are the same.

This information is passed in the even arguments of the mouse events, such as a click event. It may look like this:

myControlOrFrameworkElement.Click += (sender, eventArgs) => {
    Point clickLocation = new Point(eventArgs.X, eventArgs.Y);
    ClickHandler(eventArgs.Button, clickLocation);
};


You will be able to find the relevant types and help on them in the documentation of the UI library you are using.

[EDIT]

As OP wanted more detailed code and clarified the UI library used, I'll explain it to the very end:

using System.Windows.Forms;
using Point = System.Drawing.Point; // see below
//...

partial public class MyForm : Form {

   Panel myPanel = new Panel();


   public MyForm() { // constructor
       //...
       // suppose you have some control you want to click on (let's say, Panel)
       // usually, people generate this code with the designer, 
       // but it is called from this point anyway:
       // you set properties of myPanel and then:
       this.Controls.Add(myPanel);
       // add event handler to the Invocation List of the event instance myPanel.Click:
       myPanel.Click += (sender, eventArgs) => {
           Point clickLocation = new Point(eventArgs.X, eventArgs.Y);
           ClickHandler(eventArgs.Button, clickLocation);
       }; myPanel.Click
   } // MyWindow

   //where:
   void ClickHander(MouseButtons whatIsClicked, Point whereClicked) {
       // for example:
       MessageBox.Show(
           string.Format("Clicked: {0} at ({1},{2})", whatIsClicked, whereClicked);
           this.Text);
   } //ClickHander

} //class MyForm



I hope now it's clear. In principle, you can use just one file with this class, only add the entry point Main. It should be the same as you already have when you create a Forms project from scratch; will see it in auto-generated code:

class Program {

    static void Main() {
        //...
        Application.Start(new MyForm());
    } //Main

} //class Program



—SA


If you want to detect a mouse-click outside your WinForm application's windows (Forms), for example: on the Desktop (which you mention specifically):

You must use a Global Event Hook. There are many resources on CodeProject for implementing Global Mouse Hooks; the "classic one," updated many times over years, is by George Mamaladze: [^].

Within your WinForm application's windows (Forms):

1. every object that has a visual representation (technically, a DisplayRectange, and a ClientRectangle ... in other words a form of a window) in the UI at run-time (Control, UserControl, the Form itself) can implement a Click EventHandler, or EventHandlers for any of the various MouseDown/MouseUp/MouseMove Events.

If the mouse is clicked inside the bounds of A Form, or Control/UserControl contained within the Form, and there is a mouse-related Event "wired-up" to an EventHandler you have defined: that(those) EventHandler(s) will have the code you wrote executed.

2. In a Click EventHandler you do not get information about where the mouse was clicked in the EventArgs structure the handler receives; in the MouseDown/Up/Move events, however, you do get information in the MouseEventArgs structure passed to the EventHandler.

3. When you have a MouseEventArgs instance (in the variable always named 'e) in an EventHandler for Mouse/Down/Up/Move, the information about where the mouse went down is contained in: e.Location, e.X, and e.Y:

4. Those co-ordinates are always the co-ordinates of the mouse Event in the co-ordinate system of the Control, or Form, in whose visible area you performed the Click or other other mouse event; so: location 0,0 always means the upper-left corner of the Control or Form's client rectangle. In other words, the co-ordinates are always relative to the Control or Form's location: they are not screen co-ordinates.

5. The Location co-ordinates of a Control or a Form are always relative to their container: for a Form, the container is the screen itself (unless you have made the terrible mistake of putting a Form inside another Form or Control).

For a Control/UserControl: the container could be a Form, or another Control (you can have nested Controls).

6. If you want to know the screen co-ordinates of where a mouse Event took place:

a. you can use the MousePosition method available "everywhere" (on all Forms, Controls), in all Events, to get the screen co-ordinates of where the Mouse is at the current moment [^].

b. other methods to take a position in the co-ordinate space of a Control, or Form, and translate it to the co-ordinate space of the containing Form, or other Control, or into screen co-ordinates are made available in WinForms by the PointToScreen and PointToClient methods which can be used stand-alone or can be used as a method all Controls [^].

I suggest you try the following experiment:

1. create a Windows Forms application.

2. put a single Panel on the main Form, 'panel1

3. "wire-up" the MouseDown EventHandler for 'panel1, and use this code

private void panel1_MouseDown(object sender, MouseEventArgs e)
{
    Point formLocation = this.Location;

    Point panelLocationInForm = panel1.Location;

    Point mouseDownLocationInPanel = e.Location;

    // ?
    Point Point0 = MousePosition;

    // ?
    Point Point1 = panel1.PointToScreen(e.Location);

    // ?
    Point Point2= this.PointToScreen(e.Location);
}

Now, put a breakpoint in the code on the closing open-brace of the MouseDown EventHandler, and examine the values in the different variables:

When you can understand the principles involved, and predict what those values will be in each case: you will understand mouse location in general, and you'll be ready to understand PointToClient.

Hard work by you, at this point in time to master the fundamental aspects of position, and Event "location" in WinForms will enable you to solve any type of problem in the future :)


visit here..

http://www.daniweb.com/software-development/csharp/threads/317766/mouse-coordinates-within-a-form[^]

or

http://stackoverflow.com/questions/3852427/how-can-i-get-x-y-positions-of-mouse-relative-to-form-even-when-clicking-on-ano[^]


这篇关于通过点击获取鼠标的x,y的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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