Click here to Skip to main content
15,889,992 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more: , +
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());
Posted
Comments
Sergey Alexandrovich Kryukov 30-Mar-15 0:36am    
It all looks like a big misuse of technology...
—SA

1 solution

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
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900