从桌面获取鼠标位置而不是表单 [英] Get mouse position from desktop not form

查看:114
本文介绍了从桌面获取鼠标位置而不是表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个窗体,当我点击一个按钮时,它会显示鼠标位置和左键单击或右键单击标签的类型。我使用此代码:

I want to create a windows form that when i click on a button it show mouse position and type of left click or right click in a label.I use this code:

this.Cursor = new Cursor(Cursor.Current.Handle);
label1.Text = Cursor.Position.X;
label1.Text+= Cursor.Position.Y;





但这只显示鼠标位置,而不是来自应用程序表单,如何更改它从桌面返回鼠标位置没有表格?



i查看这篇文章:

从桌面获取鼠标位置而非形式 [ ^ ]

但很难对我来说。我很想到c#。

提前谢谢。



我尝试过的事情:



从桌面返回鼠标位置不仅仅是形式。



but this show mouse position only from application form not anywhere,how to change it that return mouse position from desktop no a form?

i view this article:
Get mouse position from desktop not form[^]
but it is hard to me.i am begginer to c#.
thank you in advance.

What I have tried:

return mouse position from desktop not only form.

推荐答案

参见Control.MousePosition属性(System.Windows.Forms) [ ^ ]。


以下代码策略经常使用:



(此处的代码假定您在MouseDown或MouseUp EventHandler中执行它)



1.提供给MouseDown和MouseUp事件的e.Location和eX和eY参数总是为您提供鼠标按下/向上和顶部的当前位置,控件的左侧位置点击...



坐标相对于包含控件的控件(或Form)。



2.根据桌面坐标系统找出mousedown / up发生的位置:



点screenLoc = this.PointToScreen(e.Location);



其中'这是当前你使用的控制/表单上下文。



3.假设一个控件可能嵌套在一个另一个控件,你想知道在包含它的表格(最外面的容器)中发生点击的位置:



Point formLoc = this .TopLevelControl.PointToClient(this.PointToScreen(e.Location));



TopLevelControl ...见:[ ^ ]。
There are frequent uses for the following code strategies:

(code here assumes you execute it inside a MouseDown or MouseUp EventHandler)

1. the e.Location and e.X and e.Y parameters supplied to the MouseDown and MouseUp Events always give you the current location of the mouse-down/up and the top,left location of the Control clicked on ...

in co-ordinates relative to the control (or Form) that contains the control.

2. to find out where the mousedown/up occurred in terms of the desktop co-ordinate system:

Point screenLoc = this.PointToScreen(e.Location);

where 'this is the current control/form context you work with.

3. assuming a control may be nested inside another Control, and you want to know where the click occurred in the Form (the outermost container) that contains it:

Point formLoc = this.TopLevelControl.PointToClient(this.PointToScreen(e.Location));

TopLevelControl ... see: [^].


你很接近。在您的表单中,创建一个MouseUp事件。然后为活动添加一些代码。



You're close. In your form, create a MouseUp Event. Then add some code to the event.

private void Form1_MouseUp(object sender, MouseEventArgs e)
{
    label1.Text = (e.X + Location.X) + ", " + (e.Y + Location.Y) + " ClickType:" + e.Button;
}





在代码中,MouseEventArgs e是一个对象,其中包含鼠标在表单中的位置。

MouseEventArgs也通过e.Button知道鼠标点击的类型。

位置或this.Location指的是您所在的Form类。因此Location.X将是相对于您正在查看的屏幕的表单的X位置。与Location.Y相同



所以整个程序





In the code, MouseEventArgs e is an object with the position of your mouse inside your form.
MouseEventArgs also knows the type of mouse click via the e.Button.
Location or this.Location refers to the Form class that you're in. So Location.X will be the X location of the form relative to the screen you're looking at. Same with Location.Y

So the whole program

using System.Windows.Forms;

namespace MousePosition
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_MouseUp(object sender, MouseEventArgs e)
        {
            label1.Text = (e.X + Location.X) + ", " + (e.Y + Location.Y) + " ClickType:" + e.Button;
        }
    }
}





玩得开心!



Hogan



Have fun coding!

Hogan


这篇关于从桌面获取鼠标位置而不是表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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