开发者

Cursor Position c++ GetCursorPos method

In the following code, I want the position of the mouse cursor on the screen, but wherever I move the cursor, I get the same output from the second starred or bolded(not sure) part below(wherever the cursor is): -1957298293 343277548. If anyone has a better method of getting the cursor position or a fix for my code, please help. (Just by the way, the "HANDLE csbiHandle; CONSOLE_SCREEN_BUFFER_INFO csbi;" aren't necessary. They were used in my previous method which also failed)

#include <iostream>
#include <windows.h>
#include <cstdlib>
#include <cstdio>
#include <stdio.h>

#ifndef MOUSE_HWHEELED
#define MOUSE_HWHEELED 0x0008
#endif
using namespace std;

int main()
{
LPPOINT point;
HANDLE csbiHandle;
CONSOLE_SCREEN_BUFFER_INFO csbi;
int counter = 0;
DWORD cNumRead, i,fdwMode, fdwSaveOldMode;
INPUT_RECORD irInputBuffer[128];
HANDLE stdHandle;
stdHandle = GetStdHandle(STD_INPUT_HANDLE);
MOUSE_EVENT_RECORD mer;


cout << "|-------------|" << endl
     << "|      A      |" << endl
     << "|-------------|" << endl;
while(counter++<1000)
{
buttonpress:
ReadConsoleInput(stdHandle, irInputBuffer,128, &cNumRead);
**GetCursorPos(point);**
for(i=0; i<cNumRead; i++)
{
    switch(irInputBuffer[i].EventType)
    {
        case MOUSE_EVENT:
        {
            mer = irInputBuffer[i].Event.MouseEvent;

            if(mer.dwButtonState == FROM_LEFT_1ST_BUTTON_PRESSED)
            {
               开发者_运维技巧 cout << "left button press" << endl;
                **cout << point->x << " " << point->y << endl;**
            }
            else
            {
                goto buttonpress;
            }
            break;
       }
        default:{
            printf("unknown\n");
            break;}
    }
}
}


return 0;
}


You can be lucky that your program didn't crash right away. The GetCursorPos function gets an LPPOINT as parameter, but that doesn't mean you should declare a variable of that type. Instead, you should do this:

POINT point;
if (GetCursorPos(&point)) {
  cout << point.x << "," << point.y << "\n";
}

The reason is that your LPPOINT, at the time of the call, is a pointer that points "somewhere", and nobody can say where it points. So it could very well be that it points to read-only memory, and Windows is so nice to check this and don't write to that memory, but instead returns FALSE. You didn't check the return value of the function call, so you cannot know whether it was successful or not.


you're passing an uninitialized pointer to the GetCursorPos() method. This is the classic case of undefined behaviour. This usually causes crash, but in your case it's something different. There's no telling why you're reading the same value there all the time: the most important thing is that it should not be done this way.

The correct way is to pass a valid pointer into this function. It can be done either by passing pointer to a local or global POINT variable or by allocating POINT variable in heap.

Here's the case of proper local variable:

POINT cursor;
GetCursorPos(&cursor);
// examine (cursor) position here

and here's the case of the heap-allocated variable:

LPPOINT pCursor = new POINT;
if (pCursor != NULL) {
    GetCursorPos(pCursor);
    // examine and handle (*pCursor) position here
    delete pCursor;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜