在C ++ / CLI中获取GetCursorPos之后的相对位置? [英] Get relative position after GetCursorPos in C++/CLI?

查看:147
本文介绍了在C ++ / CLI中获取GetCursorPos之后的相对位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

截至目前,我已成功获得x和y坐标。



在面板代码中。



POINT cursorPos;

GetCursorPos(& cursorPos);

int x;

int y;

x = cursorPos.x;

y = cursorPos.y;

cout<< x<< endl;

cout<< y<< endl;

然而,我得到的x和y坐标是全局的,也就是说,不在我希望得到坐标的面板内。



我明白需要ClientToScreen将x和y坐标更改为相对位置,但我如何在C ++ / CLR中这样做?



因为ClientToScreen需要一个句柄,这在C ++ / CLR中没有引入(请原谅我,如果这个点我错了)。谢谢:)



更新:我尝试将我的面板投射到hwnd中,但仍然无法正常工作。



HWND hwnd = static_cast< hwnd>(this-> panel1-> Handle.ToPointer());

So as of now, I've managed to successfully get the x and y coordinates.

Inside the panel code.

POINT cursorPos;
GetCursorPos(&cursorPos);
int x;
int y;
x = cursorPos.x;
y = cursorPos.y;
cout << x << endl;
cout << y << endl;
However, the x and y coordinate that I got is global, aka, not within a panel that I want to get my coordinates from.

I do understand that ClientToScreen is required to change the x and y coordinate to its relative placing, but how do i do that in C++/CLR?

Because ClientToScreen requires a handle, which is not introduced in C++/CLR (Pardon me if im wrong about this point). Thanks :)

Update: I tried casting my panel into a hwnd, but still its not working.

HWND hwnd = static_cast<hwnd>(this->panel1->Handle.ToPointer());

推荐答案

这个演员阵容没有什么不对的。 System.Windows.Forms (太糟糕了,你没有说明你正在使用什么UI库)窗口控件包装HWND;它原则上不能作为参考,HWND是本机(非托管)代码句柄。在这个库中,HWND当然可用:

https://msdn.microsoft.com/en-us/library/system.windows.forms.control.handle(v = vs.110)的.aspx 。<无线电通信/>


但你不需要它:

https://msdn.microsoft.com/en-us/library/system.windows.forms.control.pointtoclient(v=vs .110).aspx

https://msdn.microsoft.com/en-us/library/system.windows.forms.control.pointtoscreen(v = vs.110)的.aspx



以防万一,对于WPF:

https://msdn.microsoft.com/en-us/library/system.windows.media.visual.pointfromscreen(v = vs.110).aspx

https://msdn.microsoft.com/en-us/library/system.windows.media.visual.pointtoscreen(v = vs.110)的.aspx



看到你的问题后,我无法确定你是否需要。您始终可以从鼠标事件中获取鼠标光标位置;在这种情况下,事件参数总是在控件( UIElement )坐标系中给出坐标,而不是屏幕。您调用此Windows API函数的想法可能是错误的。通常,在.NET应用程序中使用原始Windows API是不好的,它可能会损害您的平台兼容性。



-SA
There is nothing more wrong that this cast. System.Windows.Forms (too bad you did not indicate what UI library you are using) windowed control wrap HWND; it cannot be a reference in principle, HWND is a native (unmanaged) code handle. In this library, HWND is of course available:
https://msdn.microsoft.com/en-us/library/system.windows.forms.control.handle(v=vs.110).aspx.

But you don't need it:
https://msdn.microsoft.com/en-us/library/system.windows.forms.control.pointtoclient(v=vs.110).aspx,
https://msdn.microsoft.com/en-us/library/system.windows.forms.control.pointtoscreen(v=vs.110).aspx.

Just in case, for WPF:
https://msdn.microsoft.com/en-us/library/system.windows.media.visual.pointfromscreen(v=vs.110).aspx,
https://msdn.microsoft.com/en-us/library/system.windows.media.visual.pointtoscreen(v=vs.110).aspx.

After seeing your question, I cannot be sure that you need even that. You can always get mouse cursor position from the mouse events; in such cases, event arguments always give you coordinates in a control (UIElement) coordinate system, not screen. It's possible that your idea of calling this Windows API function is wrong. Generally, it's bad to use raw Windows API in .NET applications, it can compromise your platform compatibility.

—SA


这篇关于在C ++ / CLI中获取GetCursorPos之后的相对位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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