js如何获取鼠标在某元素移动时~鼠标指针在元素中的坐标

js如何获取鼠标在某元素移动时~鼠标指针在元素中的坐标,第1张

稍等上代码!!

<html>

<head>

<script type="text/javascript">

function show_coords(event){

 var x = eventclientX;

 var y = eventclientY;

 var say = documentall("coords");

 sayinnerHTML = "X:"+x+" Y:"+y;

 saystyleposition = "absolute";

 saystyleleft = x + 30;

 saystyletop = y;

}

</script>

</head>

<body onmousemove="show_coords(event)">

<p id="coords"></p>

</body>

<html>

希望我的回答对你有用,有用就采纳!!!谢谢!

附上代码:

<!DOCTYPE html>

<html>

<head>

<script>

function mousePosition(evt){

evt = evt || windowevent;

var xPos = evtpageX || evtclientX || evtoffsetX || evtx;

var yPos = evtpageY || evtclientY || evtoffsetY || evty;

return [xPos, yPos];

}

function moveWindow (e) {

documentonmousemove = function(e){

resultinnerHTML = mousePosition(e);

};

}

onload = moveWindow;

</script>

</head>

<body>

<div id="result">

<!-- 展示结果 -->

</div>

</body>

</html>

OnMouseMove的后一个参数就是鼠标的当前位置

void CTest1View::OnMouseMove(UINT nFlags, CPoint point)

{

// TODO: Add your message handler code here and/or call default

CView::OnMouseMove(nFlags, point);

CPoint MousePosition = point;

}

在OnMouseMove函数里设定字符变量str,定义为鼠标的x和y。然后在状态栏输出。CString str; strFormat( "%d,%d ",pointx,pointy); CStatusBar pStatus;pStatus=(CStatusBar)AfxGetApp()->m_pMainWnd ->GetDescendantWindow (ID_VIEW_STATUS_BAR);pStatus->SetPaneText (1,str);

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

试试吧

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

你是要随着鼠标移动,获取鼠标坐标值吧?

那函数要写在OnMouseMove里面。

OnDraw是绘图函数。

CWnd::OnMouseMove

afx_msg void OnMouseMove( UINT

nFlags, CPoint point );

CPoint就是你要的坐标值。然后调用Invalidate或者UpdateWindow来刷新窗口,系统会自动调用OnDraw

封装在windowevent这个类下,你可以去看看,另提供一份参考代码~

<html >

<head>

<title>JavaScript获取鼠标坐标</title>

<meta >

以上就是关于js如何获取鼠标在某元素移动时~鼠标指针在元素中的坐标全部的内容,包括:js如何获取鼠标在某元素移动时~鼠标指针在元素中的坐标、如何在javascript中获取鼠标的位置坐标,然后再在body里面做一个文本框获取、C++里用什么函数获取鼠标位置等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

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

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

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

发表评论

登录后才能评论

评论列表(0条)

    保存