我使用 winapi
制作了一些屏幕捕获软件,并为其添加了截取光标所在位置的屏幕截图的功能。我创建了一个 POINT
结构来包含我从 GetCursorPos
函数中获得的 x 和 y 值,我尝试将成员 x
和 y
到 BitBlt
的 nXDest
和 nYDest
参数中。
唯一的问题是,因为 POINT
的成员是 LONG
,所以我得到非常长的随机数,例如 1188087969,其中 BitBlt
是期望逻辑单元和/或 int
。我可以做任何数学计算来将其转换为 BitBlt
的可用值吗?或者有其他方法可以解决这个问题吗?
HBITMAP GetScreenShot(HBITMAP hBitmap)
{
HDC hScreenDC = GetDC(NULL);
hMemoryDC = CreateCompatibleDC(hScreenDC);
int width = GetDeviceCaps(hScreenDC, HORZRES)/2;
int height = GetDeviceCaps(hScreenDC, VERTRES)/2;
LPPOINT mp;
GetCursorPos(mp);
cout << "X: " << mp->x << "\nY:" << mp->y << endl;
hBitmap = CreateCompatibleBitmap(hScreenDC, width, height);
HGDIOBJ hOldBitmap = SelectObject(hMemoryDC, hBitmap);
BitBlt(hMemoryDC, 0, 0, width, height, hScreenDC, 0, 0, SRCCOPY);
return hBitmap;
}
最佳答案
这段代码:
LPPOINT mp;
GetCursorPos(mp);
cout << "X: " << mp->x << "\nY:" << mp->y << endl;
正在使用未初始化的指针,这是未定义的行为。您需要这样做:
POINT mp;
GetCursorPos(&mp);
cout << "X: " << mp.x << "\nY:" << mp.y << endl;
关于c++ - 如何将游标的 x,y 的 LONG 转换为逻辑单位,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34191478/