C#怎样获取鼠标位置

C#怎样获取鼠标位置,第1张

// SystemWindowsFormsControl

int x = ControlMousePositionX;

int y = ControlMousePositionY;

// SystemWindowsFormsCursor

int x = CursorPositionX;

int y = CursorPositionY;

由于鼠标位置一般是在鼠标移动事件中获得,可以用迂回的办法,类似如下:

    public partial class Form1 : Form

    {

        //用全局变量中转

        Point mouseLocation;

        public Form1()

        {

            InitializeComponent();

        }

        //在鼠标移动事件中实时更新全局变量

        private void Form1_MouseMove(object sender, MouseEventArgs e)

        {

            thismouseLocation = eLocation;

        }

        //鼠标点击事件中显示此变量

        private void Form1_Click(object sender, EventArgs e)

        {

            thisText = thismouseLocationToString();

        }

    }

用API函数GetCursorPos来获取位置;;

鼠标x位置,

Shift

As

Integer;

&

P,

GetCursorPos

P

Print

"

Y

End

If

End

Sub

方法二,

"

(ByVal

vKey

As

Long)

As

Integer

Private

Declare

Function

GetCursorPos

Lib

&quot,

X

As

Single:"右键按下&quot,哪怕鼠标不在应用程序内也行

在窗体上添加一个计时器;

&

P,最高位为1,用GetAsyncKeyState来获取是否按下左键

这种方法在任何时候都可以获取X

&

&quot,其坐标值为所在容器的相对坐标值)

Private

Sub

Form_MouseDown(Button

As

Integer;

(lpPoint

As

POINTAPI)

As

Long

Private

Type

POINTAPI

X

As

Long

Y

As

Long

End

Type

Private

Sub

Timer1_Timer()

Dim

P

As

POINTAPI

X

=

GetAsyncKeyState(1)

If

X

=

-32767

Then

':"

X;

&

P:&quot:

直接用

MouseDown事件

举例;;

鼠标y位置;,是屏幕中的位置(不是相对窗体的)

Private

Declare

Function

GetAsyncKeyState

Lib

&quotY

End

If

X

=

GetAsyncKeyState(2)

If

X

=

-32767

Then

Cls

Print

&quot:&quot,设置Interval属性为10

获取的位置的数字,

GetCursorPos

P

Print

&quot:(这种方法获取的是点的位置在窗体中;user32"

&

P;x返回的是16位整数:"user32";Y:&quot,却该对象有MouseDown事件才行;

鼠标y位置;

鼠标x位置,

Y

As

Single)

If

Button

=

1

Then

Cls

Print

&quot,表明按下

Cls

Print

&quotX

&

"X;左键按下&quot方法一

GetCursorPos不管鼠标在哪里都可以获取位置的

问题是

只有OnMouseMove的时候才获取鼠标位置

但是OnMouseMove又是你对话框的成员函数

所以说

只有鼠标在对话框内移动才会显示坐标的

但是并不是没办法实现的

首先

你可以在OnInitDialog里设置一个定时器

CDialog::OnInitDialog();

//

Add

"About"

menu

item

to

system

menu

//

IDM_ABOUTBOX

must

be

in

the

system

command

range

ASSERT((IDM_ABOUTBOX

&

0xFFF0)

==

IDM_ABOUTBOX);

ASSERT(IDM_ABOUTBOX

<

0xF000);

CMenu

pSysMenu

=

GetSystemMenu(FALSE);

if

(pSysMenu

!=

NULL)

{

CString

strAboutMenu;

strAboutMenuLoadString(IDS_ABOUTBOX);

if

(!strAboutMenuIsEmpty())

{

pSysMenu->AppendMenu(MF_SEPARATOR);

pSysMenu->AppendMenu(MF_STRING,

IDM_ABOUTBOX,

strAboutMenu);

}

}

//

Set

the

icon

for

this

dialog

The

framework

does

this

automatically

//

when

the

application's

main

window

is

not

a

dialog

SetIcon(m_hIcon,

TRUE);

//

Set

big

icon

SetIcon(m_hIcon,

FALSE);

//

Set

small

icon

//

TODO:

Add

extra

initialization

here

SetTimer(1,100,NULL);//时间设置短一点

显示的也快点

return

TRUE;

//

return

TRUE

unless

you

set

the

focus

to

a

control

然后为对话框添加WM_TIME消息

void

CAdcDlg::OnTimer(UINT

nIDEvent)

{

//

TODO:

Add

your

message

handler

code

here

and/or

call

default

POINT

pos;

GetCursorPos(&pos);

//取鼠标的坐标

CString

str;

strFormat("%d,%d",posx,posy);

m_dd=str;

UpdateData(FALSE);

CDialog::OnTimer(nIDEvent);

}

m_dd为绑定在一个静态label空间上的CString

试试吧

这样就可以获得鼠标在任何时候的坐标了

吸使用以下几种方式获取:

1PageX/PageX:鼠标在页面上的位置,从页面左上角开始,即是以页面为参考点,不随滑动条移动而变化

2clientX/clientY:鼠标在页面上可视区域的位置,从浏览器可视区域左上角开始,即是以浏览器滑动条此刻的滑动到的位置为参考点,随滑动条移动 而变化

可是悲剧的是,PageX只有FF特有,IE这个悲剧没有啊T_T,所以大牛们想出了一个办法

PageY=clientY+scrollTop-clientTop;(只讨论Y轴,X轴同理,下同)

3screenX/screenY:鼠标在屏幕上的位置,从屏幕左上角开始(w3c标准)

4offsetX/offsetY:IE特有,鼠标相比较于触发事件的元素的位置,以元素盒子模型的内容区域的左上角为参考点,如果有boder,可能出现负值

5

layerX/layerY:FF特有,鼠标相比较于当前坐标系的位置,即如果触发元素没有设置绝对定位或相对定位,以页面为参考点,如果有,将改变参考坐标系,从触发元素盒子模型的border区域的左上角为参考点也就是当触发元素设置了相对或者绝对定位后,layerX和offsetX就幸福地生活在一起^-^,几乎相等,唯一不同就是一个从border为参考点,一个以内容为参考点

chrome和safari一条龙通杀!完全支持所有属性其中(offsetX和layerX都是以border为参考点)

下面这个是获取相对于屏幕的坐标

documentonmousemove=function(e){e=e e:windowevent;documentwriteln("X:"+escreenX+"Y:"+escreenY);}

Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long\x0d\\x0d\Type POINTAPI\x0d\ X As Long\x0d\ Y As Long\x0d\End Type\x0d\\x0d\Public Function getmouse_x_y() As POINTAPI\x0d\GetCursorPos getmouse_x_y\x0d\\x0d\End Function\x0d\\x0d\sub test()\x0d\'call getmouse_x_y '调用“获取鼠标坐标值过程”(假定你们给的过程/程序,名叫getmouse_x_y)\x0d\if getmouse_x_yx>100 and getmouse_x_yy>100 then '根据返回当前鼠标的坐标值执行某过程/程序\x0d\\x0d\end sub

获取鼠标当前位置只需:pageX、pageY即可。

具体步骤如下:

1、新建一个html代码页面,在这个html页面找到<body>,然后在这个<body>里创建一个用来显示坐标位置的<div>,并给这个<div>添加一个id。

创建<div>的代码:<div id="point-loc"></div>

2、使用pageX、pageY获取鼠标当前位置。在<title>标签后面新建一个<script>,创建鼠标移动时获取鼠标当前的位置。

js代码:<script type="text/javascript">

documentonmousemove = function(e){

var loc = "当前位置 x:"+epageX+",y:"+epageY

documentgetElementById("point-loc")innerHTML = loc;

}

</script>

3、保存html后使用浏览器打开即可得到鼠标位置

使用以上方法即可获取鼠标位置哦。

以上就是关于C#怎样获取鼠标位置全部的内容,包括:C#怎样获取鼠标位置、c#如何在点击窗体时获取当前鼠标的位置、vb中如何获得鼠标位置 要在timer事件下获得等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/10037848.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-04
下一篇2023-05-04

发表评论

登录后才能评论

评论列表(0条)

    保存