【发布时间】: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_x和img_y=img2_y,那么您将只会看到第二张图片,因为它会完全覆盖在第一张图片之上。除此之外,您泄漏了太多资源(请参阅上面乔纳森的评论)。 -
另外,当你说“它不工作”时,你看到的究竟是什么?