MiralKong
MiralKong

Reputation: 29

C++ GetCursorPos always returns NULL

I'm writing a MFC program and I want to get the selected tree item by using HitTest(). Below is part of my code:

POINT ptMouse;
GetCursorPos(&ptMouse);
TRACE("x = %f, y = %f\r\n", ptMouse.x, ptMouse.y);
TRACE("Error Code %s\r\n", GetLastError());
m_Tree.ScreenToClient(&ptMouse);
TRACE("x = %f, y = %f\r\n", ptMouse.x, ptMouse.y);
HTREEITEM hTreeSelected = m_Tree.HitTest(ptMouse, 0);

It should return a point and map it to the client window. But all I get is NULL. The TRACE information is as following:

image

Is there anyone can tell me what I'm doing wrong?

Upvotes: 0

Views: 372

Answers (1)

Alex
Alex

Reputation: 877

The problem is that ptMouse.x is not a float! But %f parses a float. Replace %f with %d and it should print a proper value.

For example, this program:

#include <Windows.h>
#include <cstdio>

int main() {
    POINT p;
    GetCursorPos(&p);
    printf("\nx: %f\n", p.x);
    printf("\nx: %d\n", p.x);

    return 0;
}

Gives this output:

x: 0.00000
x: 1325

The real value is 1325, of course.

Upvotes: 4

Related Questions