Jump to content

  • Log In with Google      Sign In   
  • Create Account

Need help to draw cursor properly


Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.


  • You cannot reply to this topic
6 replies to this topic

#1   Crossbones+   -  Reputation: 2705

Like
0Likes
Like

Posted 15 April 2014 - 09:34 AM

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, 15 April 2014 - 09:44 AM.


#2 slalrbalr   Banned   -  Reputation: 2005

Like
1Likes
Like

Posted 15 April 2014 - 09:53 AM

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, 15 April 2014 - 09:58 AM.


#3   Crossbones+   -  Reputation: 2705

Like
0Likes
Like

Posted 15 April 2014 - 10:19 AM

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, 15 April 2014 - 11:02 AM.


#4   Crossbones+   -  Reputation: 2705

Like
1Likes
Like

Posted 15 April 2014 - 12:39 PM

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);
}



#5   Crossbones+   -  Reputation: 5027

Like
1Likes
Like

Posted 15 April 2014 - 12:46 PM

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, 15 April 2014 - 12:46 PM.

Fruny: Ftagn! Ia! Ia! std::time_put_byname! Mglui naflftagn std::codecvt eY'ha-nthlei!,char,mbstate_t>

#6 slalrbalr   Banned   -  Reputation: 2005

Like
0Likes
Like

Posted 16 April 2014 - 12:08 AM

Yeah, I thought you already knew that you needed to get the hotspot coordinates from GetIconInfo. :)



#7   Crossbones+   -  Reputation: 2705

Like
0Likes
Like

Posted 16 April 2014 - 03:00 AM

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.






Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.




PARTNERS