win32如何获取窗口鼠标位置

win32如何获取窗口鼠标位置,第1张

函数原型:

BOOL

ClientToScreen(

_In_

HWND

hWnd,

_Inout_

LPPOINT

lpPoint

);

调用:

LRESULT

CALLBACK

WndProc(HWND

hwnd,UINT

message,WPARAM

wParam,LPARAM

lParam)

{

char

one_line[80];

CPoint

mousePos;

switch(message)

{

case

WM_LBUTTONDOWN:

GetCursorPos(&mousePos);

ClientToScreen(hwnd,&mousePos);

//

转屏幕座标

sprintf(one_line,"move

Posx=%u

Posy=%u",mousePosx

,mousePosy);

(void)

OnDisplayMessage(one_line);

//

自己写个显示函数

return

0;

//

};

}

显示函数:

void

OnDisplayMessage(char

str)

{

AfxMessageBox(str,MB_OK

|

MB_ICONINFORMATION,0);

}

子程序

__启动窗口_创建完毕

输出调试文本

(取鼠标水平位置()

-

_启动窗口左边

,取鼠标垂直位置()

-

_启动窗口顶边)

是这句吗

你的意思是在窗体中移动鼠标的时候

在鼠标的位置用一个ToolTip显示鼠标的坐标吗?

是的话就是这样:

首先、拖个ToolTip进来,比如它叫toolTip1

然后、在你窗体的MouseMove事件中,加下面一行

thistoolTip1SetToolTip(this, eX + "\t" + eY);

也就是当你的鼠标在窗体的范围内进行移动的时候,会在你的鼠标位置d出一个ToolTip,其中显示你鼠标的坐标。

就这样~

:)

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

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

鼠标在窗口上移动时,会产生mousemove消息,在这个消息里可以得到鼠标的位置,这个坐标是相对于客户区的。getcursorpos也可以获取鼠标当前位置,这个位置是相对于屏幕 坐标的。具体的请参考MSDN

看到你的问题补充就不知道你想要写成什么样子的了。

给你两段代码把。

一、获取鼠标在屏幕上的坐标

'坐标显示在Text1,Text2

'建一个Timer1,Text1,Text2

'代码如下。

'============

Private Type POINTAPI

x As Long

y As Long

End Type

Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long

Dim p As POINTAPI

Private Sub Form_Load()

Timer1Interval = 10

End Sub

Private Sub Timer1_Timer()

GetCursorPos p

Text1Text = px

Text2Text = py

End Sub

二、获取鼠标在窗体内的坐标

'坐标显示在标题上

'运行此程序需要再窗体添加控件Timer

Option Explicit

Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long

Private Declare Function ScreenToClient Lib "user32" (ByVal hwnd As Long, lpPoint As POINTAPI) As Long

Private Type POINTAPI

x As Long

y As Long

End Type

Private Sub Form_Load()

Timer1Interval = 10 '设置获取鼠标坐标间隔

End Sub

Private Sub Timer1_Timer()

Dim P As POINTAPI

GetCursorPos P '获取鼠标在屏幕中的位置

ScreenToClient Mehwnd, P '转换为本窗体的坐标

Dim t As Boolean

t = Px >= 0 And Py >= 0 And Px < MeWidth / ScreenTwipsPerPixelX And Py <= MeHeight / ScreenTwipsPerPixelY

If t Then MeCaption = "x=" & Px & "y=" & Py '按像素显示坐标

'If t Then MeCaption = "x=" & Px ScreenTwipsPerPixelX & "y=" & Py ScreenTwipsPerPixelY '按缇显示坐标

End Sub

Point

p

=

CursorPosition;

pX;

//当前X坐标

pY;

//当前Y坐标

以上代码在任意地方执行,就可取得鼠标在屏幕上的X

Y坐标。

以上就是关于win32如何获取窗口鼠标位置全部的内容,包括:win32如何获取窗口鼠标位置、易语言如何获取鼠标在窗口中的位置、C#怎么获得鼠标在屏幕上的坐标,并在鼠标位置输出等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

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

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

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

发表评论

登录后才能评论

评论列表(0条)

    保存