【问题标题】:how to display two bitmap image on window in C如何在C中的窗口上显示两个位图图像
【发布时间】:2013-07-01 18:05:57
【问题描述】:

我可以在窗口上成功显示单个图像,我不确定如何在窗口上显示两个图像。我为不同的图像重复了相同的代码,但它不起作用。这是显示单个图像的代码。

 static HBITMAP bmpSource = NULL;
 static HDC hdcSource = NULL;
 PAINTSTRUCT ps;
 HDC hdcDestination;

 //* inside the WndProc()

    case WM_PAINT:

    bmpSource = (HBITMAP)LoadImage(NULL,file_path,IMAGE_BITMAP,0,0,LR_LOADFROMFILE);
    hdcSource = CreateCompatibleDC(GetDC(0));
    SelectObject(hdcSource, bmpSource);
    hdcDestination = BeginPaint(hwnd, &ps);
    BitBlt(hdcDestination,img_x, img_y, 300, 300, hdcSource, 0, 0, SRCCOPY);
    EndPaint(hwnd, &ps);

    breaks;
   //**

这就是我正在做的事情,我在窗口 gui 方面有经验。

     static HBITMAP bmpSource = NULL,bmpSource2 = NULL;
 static HDC hdcSource = NULL,hdcSource2 = NULL;
 PAINTSTRUCT ps;
 HDC hdcDestination;

 //* inside the WndProc()

    case WM_PAINT:

    bmpSource = (HBITMAP)LoadImage(NULL,file_path,IMAGE_BITMAP,0,0,LR_LOADFROMFILE);
    hdcSource = CreateCompatibleDC(GetDC(0));
    SelectObject(hdcSource, bmpSource);
     bmpSource2 = (HBITMAP)LoadImage(NULL,file2_path,IMAGE_BITMAP,0,0,LR_LOADFROMFILE);
    hdcSource2 = CreateCompatibleDC(GetDC(0));
    SelectObject(hdcSource2, bmpSource2);
    hdcDestination = BeginPaint(hwnd, &ps);
    BitBlt(hdcDestination,img_x, img_y, 300, 300, hdcSource, 0, 0, SRCCOPY);
    BitBlt(hdcDestination,img2_x, img2_y, 300, 300, hdcSource2, 0, 0, SRCCOPY);
    EndPaint(hwnd, &ps);

    breaks;
   //**

【问题讨论】:

  • 您不会释放使用GetDC 获得的DC,不会在使用SelectObject 选择时保存/恢复旧位图,也不会删除使用CreateCompatibleDC 创建的DC。您很快就会用完 GDI 资源。
  • 如果img_x = img2_ximg_y = img2_y,那么您将只会看到第二张图片,因为它会完全覆盖在第一张图片之上。除此之外,您泄漏了太多资源(请参阅上面乔纳森的评论)。
  • 另外,当你说“它不工作”时,你看到的究竟是什么

标签: c winapi bitmap win32gui


【解决方案1】:

您没有向我们展示无效的代码,因此我们无法告诉您您做错了什么。

在 BeginPaint 之后和 EndPaint 之前进行所有绘制(即 BitBlt 调用)。这些函数只能调用一次,所以不要重复它们。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-12-25
    • 1970-01-01
    • 2012-01-11
    • 2019-03-04
    • 2017-08-19
    • 2011-05-05
    • 2021-11-20
    • 2022-12-11
    相关资源
    最近更新 更多