Code:
LRESULT CALLBACK WindowProcedure(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
static HDC hdcMem;
HRSRC hRsrc;
HGLOBAL hGlobal;
byte* lpBits;
BITMAPINFO bmi;
HDC hdc;
int iRet;
PAINTSTRUCT ps;
switch (message)
{
case WM_CREATE:
{
hRsrc = FindResource(NULL, "RCDATA_0", RT_RCDATA);
if (hRsrc==NULL) {
MessageBox(NULL, "hRsrc==NULL", "Error", MB_OK | MB_ICONERROR);
return 0;
}
hGlobal = LoadResource(NULL, hRsrc);
if (hGlobal==NULL) {
MessageBox(NULL, "hGlobal==NULL", "Error", MB_OK | MB_ICONERROR);
return 0;
}
lpBits = LockResource(hGlobal);
if (lpBits==NULL) {
MessageBox(NULL, "lpBits==NULL", "Error", MB_OK | MB_ICONERROR);
return 0;
}
memset(&bmi, 0, sizeof(bmi));
bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bmi.bmiHeader.biWidth = 400;
bmi.bmiHeader.biHeight = -300;
bmi.bmiHeader.biPlanes = 1;
bmi.bmiHeader.biBitCount = 0;
bmi.bmiHeader.biCompression = BI_JPEG;
bmi.bmiHeader.biSizeImage = SizeofResource(NULL, hRsrc);
hdcMem = CreateCompatibleDC(NULL);
iRet = StretchDIBits
(
hdcMem,
0, 0, 400, 300,
0, 0, 400, 300,
lpBits,
&bmi,
DIB_RGB_COLORS,
SRCCOPY
);
FreeResource(hGlobal);
if (iRet==GDI_ERROR) {
MessageBox(NULL, "iRet==GDI_ERROR", "Error", MB_OK | MB_ICONERROR);
}
break;
}
case WM_PAINT:
{
hdc = BeginPaint(hwnd, &ps);
iRet = StretchBlt
(
hdc,
0, 0, 400, 300,
hdcMem,
0, 0, 400, 300,
SRCCOPY
);
EndPaint(hwnd, &ps);
break;
}
case WM_DESTROY:
{
DeleteDC(hdcMem);
PostQuitMessage (0);
break;
}
default:
return DefWindowProc (hwnd, message, wParam, lParam);
}
return 0;
}
If StretchDIBits fails, the return value is GDI_ERROR, but I never get it. If StretchDIBits succeeds, the return value is the number of scan lines copied, and I always get 0.