c++ - 如何将游标的 x,y 的 LONG 转换为逻辑单位

标签 c++ winapi bitmap

我使用 winapi 制作了一些屏幕捕获软件,并为其添加了截取光标所在位置的屏幕截图的功能。我创建了一个 POINT 结构来包含我从 GetCursorPos 函数中获得的 x 和 y 值,我尝试将成员 xyBitBltnXDestnYDest 参数中。

唯一的问题是,因为 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/

相关文章:

c++ - 构造函数在错误的时间调用

c++ - 删除 vector 或指针编译器错误

winapi - 为什么这个程序有时会崩溃,有时不会?

android - 在 Android 中清除位图

android - 从字节数组 (byte[]) 转换位图 - 位图仍然为空

c++ - 2D isometric engine - 数学题 - 立方体选择 - 菱形图

c++ - 链接器想要我已经链接的库

python - 如何将非英语 Windows 时区名称映射到 Python 中的 Olsen 名称?

c++ - 如何使用 WriteFile 写入 stderr

objective-c - 段错误 11 Objective-C,但 C 中没有