Código PHP:
LRESULT CALLBACK WindowFunc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
OPENFILENAME fname;
char filename[64];
switch(message) {
case WM_SIZE:
{
coords.cx = LOWORD(lParam);
coords.cy = HIWORD(lParam);
if(hBitmap){
if (memDC) {
SelectObject(memDC, memBitmapOld);
DeleteObject(memBitmap);
DeleteDC(memDC);
}
tempDC = GetDC(hwnd);
memDC = CreateCompatibleDC(tempDC);
memBitmap = CreateCompatibleBitmap(tempDC, coords.cx, coords.cy);
SelectObject(memDC, memBitmap);
ReleaseDC(hwnd, tempDC);
RepintarImagen(hwnd);
}
break;
}
case WM_PAINT:
hdc = BeginPaint(hwnd, &ps);
BitBlt(hdc, 0, 0, coords.cx, coords.cy, memDC, 0, 0, SRCCOPY); //@@@ mejorar repintando solo el area invalidada
EndPaint(hwnd, &ps);
break;
case WM_COMMAND:
switch(LOWORD(wParam)) {
case IDM_OPEN:
memset(&fname, 0, sizeof(OPENFILENAME));
fname.lStructSize = sizeof(OPENFILENAME);
fname.hwndOwner = hwnd;
fname.lpstrFilter = ("Bitmap Files (*.bmp)\0*.bmp\0\0");
fname.nFilterIndex = 1;
fname.lpstrFile = fn;
fname.nMaxFile = sizeof(fn);
fname.lpstrFileTitle = filename;
fname.nMaxFileTitle = sizeof(filename)-1;
fname.Flags = OFN_FILEMUSTEXIST | OFN_HIDEREADONLY;
if(!GetOpenFileName(&fname))
break;
hBitmap = (HBITMAP)LoadImage(NULL, fn, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
GetObject(hBitmap, sizeof(BITMAP), &bm);
RECT rect;
GetClientRect(hwnd, &rect);
RepintarImagen(hwnd);
InvalidateRect(hwnd, &rect, TRUE);
break;
}
break;
case WM_DESTROY: //terminar el programa
if (memDC) {
SelectObject(memDC, memBitmapOld);
DeleteObject(memBitmap);
DeleteDC(memDC);
}
if (hBitmap)
DeleteObject(hBitmap);
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd, message, wParam, lParam);
}
return 0;
}
/******************************************************************************
Método: RepintarImagen
Función: Actualiza el memDC con la imagen cargada, teniendo en cuenta que la imagen debe estar centrada en la ventana. Tambien se tiene en cuenta el scroll y el zoom.
******************************************************************************/
void RepintarImagen(HWND hwnd){
HDC tempDC;
HBITMAP tempBitmapOrig;
if((bm.bmWidth > (coords.cx)) || (bm.bmHeight > (coords.cy))){
xCenter = 0;
yCenter = 0;
}else{
xCenter = (coords.cx/2) - (bm.bmWidth/2);
yCenter = (coords.cy/2) - (bm.bmHeight/2);
}
tempDC = GetDC(hwnd);
tempBitmapOrig = SelectObject(tempDC, hBitmap);
StretchBlt(memDC, xCenter, yCenter, 0, 0, tempDC, 0, 0, bm.bmWidth,bm.bmHeight,SRCCOPY);
SelectObject(tempDC, tempBitmapOrig);
ReleaseDC(hwnd,tempDC);
}