无效到HBITMAP转换问题

时间:2014-11-18 23:13:53

标签: 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 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);
}

1 个答案:

答案 0 :(得分:2)

LoadImage()返回HANDLE。在将结果分配给变量时需要演员:

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

另外,在DeleteDC()之前,您需要选择原来的HBITMAP重新加入hdcMem - 您需要在之前致电SelectObject()时保存它。