DirectX 9 Get Cursor Position [duplicate] - c++

I want to get the current mouse position of the window, and assign it to 2 variables x and y (co-ordinates relative to the window, not to the screen as a whole).
I'm using Win32 and C++.
And a quick bonus question: how would you go about hiding the cursor/unhiding it?

You get the cursor position by calling GetCursorPos.
POINT p;
if (GetCursorPos(&p))
{
//cursor position now in p.x and p.y
}
This returns the cursor position relative to screen coordinates. Call ScreenToClient to map to window coordinates.
if (ScreenToClient(hwnd, &p))
{
//p.x and p.y are now relative to hwnd's client area
}
You hide and show the cursor with ShowCursor.
ShowCursor(FALSE);//hides the cursor
ShowCursor(TRUE);//shows it again
You must ensure that every call to hide the cursor is matched by one that shows it again.

GetCursorPos() will return to you the x/y if you pass in a pointer to a POINT structure.
Hiding the cursor can be done with ShowCursor().

Related

C++ Ncurses , how to check what the current mouse position is

How Do I Check What The Mouse Position is when I move the mouse in Ncurses. I have tried searching but couldnt find any answer . I have seen the use of getmouse() , but getting the mouse co-ordinates from that function requires enabling an event first. I want to get the position of the mouse without clicking the mouse.
You get the cursor position by calling GetCursorPos.
POINT p;
if (GetCursorPos(&p))
{
//cursor position now in p.x and p.y
}
This returns the cursor position relative to screen coordinates. Call ScreenToClient to map to window coordinates.
if (ScreenToClient(hwnd, &p))
{
//p.x and p.y are now relative to hwnd's client area
}

Determine the character on the console on witch the MouseCursor is on

As we all know, the console buffer size is composed like a 2D array. I'm trying to implement on click buttons (drawn buttons NOT child windows) but im having an accuracy problem.
Because the Console Window is movable and resizable, i have to take the Mouse Cursor position relative to the Console Window TopLeft corner (I've found a way of accurately doing that in pixels). But now the problem arrives. When i try to find out on which character square the Mouse Cursor is on, it becomes inacurate (errors of about 3 ~ 5 pixels) and this is a problem when implementing on click buttons.
These are the functions i use. Also keep in mind that we need to previously have the GetCurrentConsoleFont() declared. (find it here)
For ease of testing, I have implemented a little "Draw my thing" game in the main (see full code).
/** This returns the cursor position relative to any window (not just the console).*/
POINT GetCursPosRelWin(HWND hWindow)
{
POINT rCoord;
RECT windowCoord;
HWND hConsole = GetConsoleWindow();
GetWindowRect(hConsole,&windowCoord);
POINT ptCursor;
GetCursorPos(&ptCursor);
rCoord.x = ptCursor.x - windowCoord.left;
rCoord.y = ptCursor.y - windowCoord.top;
return rCoord;
}
WORD GetCurrentFontHeight()
{
CONSOLE_FONT_INFO cfi;
GetCurrentConsoleFont(GetStdHandle(STD_OUTPUT_HANDLE), FALSE, &cfi);
return cfi.dwFontSize.Y;
}
WORD GetCurrentFontWidth()
{
CONSOLE_FONT_INFO cfi;
GetCurrentConsoleFont(GetStdHandle(STD_OUTPUT_HANDLE), FALSE, &cfi);
return cfi.dwFontSize.X;
}
So, is there any way of making this method be more accurate?
EDIT: This is the most accurate way i managed to find though it is still not very precise.
/** See the full code for a better understanding */
/** In the main function as parameters of MoveConsoleCursor() */
MoveConsoleCursor(
(SHORT)((double)(ptCursor.x/GetCurrentFontWidth() - ((ptCursor.x/GetCurrentFontWidth())%10)/10 )),
(SHORT)((double)(ptCursor.y/GetCurrentFontHeight() - 0.5))
);
You can change your GetCursPosRelWin to:
POINT GetCursPosRelWin(HWND hWindow)
{
POINT ptCursor;
GetCursorPos(&ptCursor);
ScreenToClient(hWindow, &ptCursor);
return ptCursor;
}
And MoveConsoleCursor call to:
MoveConsoleCursor(ptCursor.x / GetCurrentFontWidth(), ptCursor.y / GetCurrentFontHeight());
This puts cursor in the center of a square, provided the scroll bars are not moved. Otherwise you have to account for the scrollbar offsets.

How to determine which way the mouse is being dragged (Win32, C++)

I am currently working on a project which requires me to know when the mouse is being dragged to the left, or to the right.
What I would like to do with this information, is drag an object in 3d space (with OpenGL/Win32), left, or right, depending on which way the mouse was dragged.
The problem is, I have no idea how to determine which way the mouse is being dragged.
I tried finding the previous x coordinate, and subtracting it from the current x coordinate, but I do not know how to determine the previous x coordinate!
Can someone please help me?
(P.S. I already know that the current x mouse location is found with LOWORD(lParam), but what I do not know is how to determine the previous x mouse location. Thanks for your time.)
You could do it yourself by storing the current mouse position as the 'last' mouse position, and then after two mouse position updates, subtract.
onMouseMove(mouseX, mouseY):
if previousX and previousY are set:
deltaX = previousX - mouseX
deltaY = previousY - mouseY
actOnDrag(deltaX, deltaY)
previousX = mouseX
previousY = mouseY
You could set previousX and previousY to some crazy sentinel value (e.g. -1) to determine whether they have ever been set.
You could use the DragDetect function.
BOOL DragDetect(HWND hwnd, POINT pt);
https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-dragdetect

Relative mouse position of SDL_Surface

In my application I need to return the relative mouse position from an SDL_Surface, the problem is the mouse position that gets returned is relative to the SDL window and not the SDL_Surface. I guess my question is what is the easiest / most effective way of doing this. Any questions just ask. Thanks.
EDIT: Sorry I should have explained better, I have SDL_Surface* Surf_Display; on Surf_display there is an Image say its 1000 x 1000, So in order to see the image on a 600 x 600 window I have a camera that I can move around ( really its the surface that moves not the camera ) for instance to look right of the image I move the surface -1 left if that makes sense. So my problem is when I click my mouse on a part of the surface(image) my mouse returns the position that the mouse compared to where the cursor is in the window, what i'm wanting is so that it returns the position of the cursor compared to where it is on the surface(image)
I hope that better explains the situation. Thanks again
Just add(or subtract, depending on how you look at it) the offset to the mouse coordinates. So you're drawing the surface something like this:
SDL_Rect dest_rect = { -camera.x, -camera.y };
SDL_BlitSurface(image_surface, NULL, screen_surface, &dest_rect);
I don't know if you're using event based mouse handling, or if you're using SDL_GetMouseState, but either way, you would simply add camera.x and camera.y to the mouse position, for example:
int x, y;
SDL_GetMouseState(&x, &y);
x += camera.x;
y += camera.y;

Get current cursor position

I want to get the current mouse position of the window, and assign it to 2 variables x and y (co-ordinates relative to the window, not to the screen as a whole).
I'm using Win32 and C++.
And a quick bonus question: how would you go about hiding the cursor/unhiding it?
You get the cursor position by calling GetCursorPos.
POINT p;
if (GetCursorPos(&p))
{
//cursor position now in p.x and p.y
}
This returns the cursor position relative to screen coordinates. Call ScreenToClient to map to window coordinates.
if (ScreenToClient(hwnd, &p))
{
//p.x and p.y are now relative to hwnd's client area
}
You hide and show the cursor with ShowCursor.
ShowCursor(FALSE);//hides the cursor
ShowCursor(TRUE);//shows it again
You must ensure that every call to hide the cursor is matched by one that shows it again.
GetCursorPos() will return to you the x/y if you pass in a pointer to a POINT structure.
Hiding the cursor can be done with ShowCursor().