C 无效到HBITMAP转换问题

C 无效到HBITMAP转换问题,c,winapi,C,Winapi,我一直在第123行遇到一个错误: 123 C:\Dev-Cpp\Window_main_2.c invalid conversion from `void*' to `HBITMAP__*' 我不知道这是怎么回事,这让我发疯 void DrawBitmap(HDC hdcDest, char *filename, int x, int y) { HBITMAP image; BITMAP bm; HDC hdcMem; // This is the line

我一直在第123行遇到一个错误:

123 C:\Dev-Cpp\Window_main_2.c invalid conversion from `void*' to `HBITMAP__*'
我不知道这是怎么回事,这让我发疯

void DrawBitmap(HDC hdcDest, char *filename, int x, int y)
{
    HBITMAP image;
    BITMAP bm;
    HDC hdcMem;

    // This is the line that brings about the issue (just ask me if more code is required because
    // there is a lot more. Essentially this whole function points to a file and I call this
    // function in another function that will compile a windows screen filled with the following
    // image path. But I cant get this HBITMAP to agree with the image datatype. Please let me
    // know if more info is required and thank you.) The line is below.     
    image = LoadImage(0, "C:\\Users\\Lillian\\Pictures\\c.bmp", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);

    GetObject(image, sizeof(BITMAP), &bm);

    hdcMem = CreateCompatibleDC(global_hdc);
    SelectObject(hdcMem, image);

    BitBlt(
        global_hdc,
        x,
        y,
        bm.bmWidth,
        bm.bmHeight,
        hdcMem,
        0,
        0,
        SRCCOPY);

    DeleteDC(hdcMem);
    DeleteObject((HBITMAP)image);
}
LoadImage()
返回一个
句柄
。将结果分配给变量时需要强制转换:

image = (HBITMAP) LoadImage(0, "C:\\Users\\Lillian\\Pictures\\c.bmp", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);

另外,在
DeleteDC()
之前,您需要将原始的
HBITMAP
选择回
hdcMem
——您需要在前面调用
SelectObject()
时保存它。

因为
LoadImage
能够加载不同类型的图像(位图、图标和光标)它返回一个通用的
句柄,而不是更具体的
HBITMAP
HICON
等。