Sign in to follow this  

Need help to draw cursor properly

This topic is 1373 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I noticed a small bug in my remote desktop application related to drawing the cursor of the remote machine and im not sure how to solve it. The problem is that sometime, the cursor is not drawn at the proper location depending on the cursor. Also im using a weird hack to make it work for windows 7 and above and i don't think that's the right way to do it.

 

Here's the code im using right now:

void CScreenshot::DrawScreenCursor(HDC hDC)
{
    CURSORINFO CursorInfo;
    CursorInfo.cbSize = sizeof(CURSORINFO);
    GetCursorInfo(&CursorInfo);

    static DWORD Version = WinVer.DetectWindowsVersion();
    //static HCURSOR hCur = LoadCursor(NULL, IDC_ARROW);

    DWORD CursorWidth = GetSystemMetrics(SM_CXCURSOR);
    DWORD CursorHeight = GetSystemMetrics(SM_CYCURSOR);

    POINT CursorPos;
    GetCursorPos(&CursorPos);

    // Needed for XP or older windows
    if(Version < _WIN_VISTA_){
        CursorPos.x -= CursorWidth >> 2;
        CursorPos.y -= CursorHeight >> 2;
    }

    DrawIconEx(hDC, CursorPos.x, CursorPos.y, CursorInfo.hCursor, CursorWidth, CursorHeight, 0, NULL, DI_NORMAL);
}

If the cursor is the default arrow, this work fine, but if the cursor change for, let say, the resize cursor, then it's offset a little.

 

Now, after reading this post, im pretty sure this function could solve my problem, but im not sure how to do it.

 

The question is, how do i get the icon from the current cursor (so i can call GetIconInfo() with the right cursor)?


And, why do i need to divide the cursor width by 4 in recent version of windows? It's like i have all the pieces of the puzzle but i don't know how to make them fit togeter... Maybe if i could debug this or something that could help a lot.

 

Note: The mouse click always happen at the right spot, it's just the cursor that's offset a little.

 

EDIT: I just realised im already getting the cursor from GetCursorInfo(), but im still lost on how to solve the problem. Maybe the width and height im getting are wrong?

 

Any help appreciated.

Edited by Vortez

Share this post


Link to post
Share on other sites

The GetCursor (or GetCursorInfo, as you already pointed out) API(s) will give you the cursor's HCURSOR handle, and you can pass that handle to GetIconInfo (cast it to HICON).

Edited by tonemgub

Share this post


Link to post
Share on other sites

Humm, after reading a bit about the DI_DEFAULTSIZE parameter of DrawIconEx(), that should solve the size issue i think, but it's still not working. For example, i commented the lines

 

if(Version < _WIN_VISTA_){
CursorPos.x -= CursorWidth >> 2;
CursorPos.y -= CursorHeight >> 2;
}

 

and put the real cursor at (0, 0) on the screen, here's what i got. The cursor is offset by 10 pixels on the x and y axis... It's like i need to offset the cursor by half it's width and height, using something like this (the last reply).

 

Is there a way i could open the system cursors in a cursor editor program? Nvm, found them smile.png

 

Oh well, guess im gonna have to make a small app and do some testing... and get some sleep.

Edited by Vortez

Share this post


Link to post
Share on other sites

Seem like i solved it, i just had to substract the hotspot from the cursor location, and remove the width and height from the DrawIconEx() call

void CScreenshot::DrawScreenCursor(HDC hDC)
{
	POINT CursorPos;
	GetCursorPos(&CursorPos);
	
	CURSORINFO CursorInfo;
	CursorInfo.cbSize = sizeof(CURSORINFO);
	GetCursorInfo(&CursorInfo);

	ICONINFO IconInfo;
	ZeroMemory(&IconInfo, sizeof(ICONINFO));
	GetIconInfo((HICON)CursorInfo.hCursor, &IconInfo);

	int x = CursorPos.x - IconInfo.xHotspot;
	int y = CursorPos.y - IconInfo.yHotspot;

	DrawIconEx(hDC, x, y, CursorInfo.hCursor, 0, 0, 0, NULL, DI_NORMAL);
}

Share this post


Link to post
Share on other sites

This sounds like you need to get the cursor hotspot. Funnily you need to use GetIconInfo to retrieve that info.

 

 

Edit: Ninja'd

Edited by Endurion

Share this post


Link to post
Share on other sites

Yea, i was rather confused at first, then it hit me when i changed the hotspot of a cursor i modified. Heh, this bug been lurking in my code for 3 years, funny how just writting about a problem help to solve it.

Share this post


Link to post
Share on other sites
Sign in to follow this