[C con Clase] guardar las imagenes de pantalla
Francisco Mota
frcostrong en hotmail.com
Vie Oct 19 05:06:30 CEST 2007
Saludos amigos!!.
Veran sigo aprendiendo API y ahora se me ah presentado el siguiente problema.
Quiero guardar lo que se muestre en pantalla, estoy juntando varias imagenes y las quiero guardar como una sola, es decir guardar lo que muestra la pantalla como una imagen ya sea bmp o cualquier formato. El codigo que es el que tengo muestra y guarda una sola imagen. Yo lo que quiero es guardar la pantalla completa. Espero sirva de algo el codigo.
#include <windows.h> #include <commdlg.h> #include <stdlib.h> #include <stdio.h> #include "viewbmp.h"
#define UNTITLED "(sin título)"
LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM);
void DibujaBitmap (HDC hdc, HBITMAP hBitmap, int xInicio, int yInicio);
void PopFileInitialize (HWND); BOOL PopFileOpenDlg (HWND, PSTR, PSTR); BOOL PopFileSaveDlg (HWND, PSTR, PSTR); BOOL PopFileRead (HINSTANCE, PSTR); BOOL PopFileWrite (HWND, PSTR);
void DoCaption (HWND hwnd, char *szTitleName); void OkMessage (HWND hwnd, char *szMessage, char *szTitleName); short AskAboutSave (HWND hwnd, char *szTitleName); // Variables globales
static char szAppName[] = "ViewBMP"; static HWND hDlgModeless; static OPENFILENAME ofn; static HBITMAP hBitmap = NULL;
// ************************** Punto de entrada al programa *******
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow) { MSG msg; HWND hwnd; HACCEL hAccel; WNDCLASSEX wndclass;
wndclass.cbSize = sizeof (wndclass); wndclass.style = CS_HREDRAW | CS_VREDRAW; wndclass.lpfnWndProc = WndProc; wndclass.cbClsExtra = 0; wndclass.cbWndExtra = 0; wndclass.hInstance = hInstance; wndclass.hIcon = LoadIcon (hInstance, szAppName); wndclass.hCursor = LoadCursor (NULL, IDC_ARROW); wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH); wndclass.lpszMenuName = szAppName; wndclass.lpszClassName = szAppName; wndclass.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
RegisterClassEx (&wndclass);
hwnd = CreateWindow (szAppName, NULL, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, szCmdLine);
ShowWindow (hwnd, iCmdShow); UpdateWindow (hwnd);
hAccel = LoadAccelerators (hInstance, szAppName);
while (GetMessage (&msg, NULL, 0, 0)) { if (hDlgModeless == NULL || !IsDialogMessage (hDlgModeless, &msg)) { if (!TranslateAccelerator (hwnd, hAccel, &msg)) { TranslateMessage (&msg); DispatchMessage (&msg); } } } return msg.wParam; }
// ************************** Procedimiento de ventana *******
LRESULT CALLBACK WndProc (HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam) { static BOOL bNeedSave = FALSE; static char szFileName[_MAX_PATH]; static char szTitleName[_MAX_FNAME + _MAX_EXT]; static HINSTANCE hInst; HDC hDC; PAINTSTRUCT ps;
switch (iMsg) { case WM_CREATE : hInst = ((LPCREATESTRUCT) lParam) -> hInstance;
// Inicializar cuadro de diálogo común
PopFileInitialize (hwnd);
// Procesar línea de comandos
lstrcpy (szFileName, (PSTR) (((LPCREATESTRUCT) lParam)->lpCreateParams));
if (strlen (szFileName) > 0) { GetFileTitle (szFileName, szTitleName, sizeof (szTitleName));
if (!PopFileRead (hInst, szFileName)) OkMessage (hwnd, "No se puede leer el archivo %s.", szTitleName); }
DoCaption (hwnd, szTitleName); return 0;
case WM_COMMAND : switch (LOWORD (wParam)) { // Mensajes del menú Archivo case IDM_OPEN : if (bNeedSave && IDCANCEL == AskAboutSave (hwnd, szTitleName)) return 0;
if (PopFileOpenDlg (hwnd, szFileName, szTitleName)) { if (!PopFileRead (hInst, szFileName)) { OkMessage (hwnd, "No se puede leer el archivo %s.", szTitleName); szFileName[0] = '\0'; szTitleName[0] = '\0'; } else InvalidateRect (hwnd, NULL, FALSE); }
DoCaption (hwnd, szTitleName); bNeedSave = FALSE; return 0;
case IDM_SAVE : if (szFileName[0]) { if (PopFileWrite (hwnd, szFileName)) { bNeedSave = FALSE; return 1; } else OkMessage (hwnd, "No se puede escribir el archivo %s", szTitleName); return 0; } // sigue proceso case IDM_SAVEAS : if (PopFileSaveDlg (hwnd, szFileName, szTitleName)) { DoCaption (hwnd, szTitleName);
if (PopFileWrite (hwnd, szFileName)) { bNeedSave = FALSE; return 1; } else OkMessage (hwnd, "No se puede escribir el archivo %s", szTitleName); } return 0;
case IDM_EXIT : SendMessage (hwnd, WM_CLOSE, 0, 0); return 0;
} break;
case WM_PAINT: hDC = BeginPaint (hwnd, &ps); DibujaBitmap (hDC, hBitmap, 0, 0); EndPaint (hwnd, &ps); return 0;
case WM_CLOSE : if (!bNeedSave || IDCANCEL != AskAboutSave (hwnd, szTitleName)) DestroyWindow (hwnd);
return 0;
case WM_QUERYENDSESSION : if (!bNeedSave || IDCANCEL != AskAboutSave (hwnd, szTitleName)) return 1;
return 0;
case WM_DESTROY : if (hBitmap != NULL) DeleteObject (hBitmap); PostQuitMessage (0); return 0;
} return DefWindowProc (hwnd, iMsg, wParam, lParam); }
// ************************** función para dibujar bitmaps ******
void DibujaBitmap (HDC hdc, HBITMAP hBitmap, int xInicio, int yInicio) { BITMAP bm; HDC hdcMem; DWORD dwSize; POINT ptSize, ptOrg;
hdcMem = CreateCompatibleDC (hdc); SelectObject (hdcMem, hBitmap); SetMapMode (hdcMem, GetMapMode (hdc));
GetObject (hBitmap, sizeof (BITMAP), (LPVOID)&bm);
ptSize.x = bm.bmWidth; ptSize.y = bm.bmHeight; DPtoLP (hdc, &ptSize, 1);
ptOrg.x = 0; ptOrg.y = 0; DPtoLP (hdcMem, &ptOrg, 1);
BitBlt (hdc, xInicio, yInicio, ptSize.x, ptSize.y, hdcMem, ptOrg.x, ptOrg.y, SRCCOPY);
DeleteObject (hdcMem); }
// ***** funciones para manejar los diálogos abrir/guardar archivo ***
void PopFileInitialize (HWND hwnd) { static char szFilter[] = "Archivos de texto (*.BMP)\0*.bmp\0"; // "Todos los archivos (*.*)\0*.*\0\0";
ofn.lStructSize = sizeof (OPENFILENAME); ofn.hwndOwner = hwnd; ofn.hInstance = NULL; ofn.lpstrFilter = szFilter; ofn.lpstrCustomFilter = NULL; ofn.nMaxCustFilter = 0; ofn.nFilterIndex = 0; ofn.lpstrFile = NULL; // Definido en funciones Open y Close ofn.nMaxFile = _MAX_PATH; ofn.lpstrFileTitle = NULL; // Definido en funciones Open y Close ofn.nMaxFileTitle = _MAX_FNAME + _MAX_EXT; ofn.lpstrInitialDir = NULL; ofn.lpstrTitle = NULL; ofn.Flags = 0; // Definido en funciones Open y Close ofn.nFileOffset = 0; ofn.nFileExtension = 0; ofn.lpstrDefExt = "txt"; ofn.lCustData = 0L; ofn.lpfnHook = NULL; ofn.lpTemplateName = NULL; }
BOOL PopFileOpenDlg (HWND hwnd, PSTR pstrFileName, PSTR pstrTitleName) { ofn.hwndOwner = hwnd; ofn.lpstrFile = pstrFileName; ofn.lpstrFileTitle = pstrTitleName; ofn.Flags = OFN_HIDEREADONLY | OFN_CREATEPROMPT;
return GetOpenFileName (&ofn); }
BOOL PopFileSaveDlg (HWND hwnd, PSTR pstrFileName, PSTR pstrTitleName) { ofn.hwndOwner = hwnd; ofn.lpstrFile = pstrFileName; ofn.lpstrFileTitle = pstrTitleName; ofn.Flags = OFN_OVERWRITEPROMPT;
return GetSaveFileName (&ofn); }
static long PopFileLength (FILE *file) { int iCurrentPos, iFileLength;
iCurrentPos = ftell (file);
fseek (file, 0, SEEK_END);
iFileLength = ftell (file);
fseek (file, iCurrentPos, SEEK_SET);
return iFileLength; }
BOOL PopFileRead (HINSTANCE hInst, PSTR pstrFileName) { if (hBitmap != NULL) DeleteObject (hBitmap); hBitmap = (HBITMAP)LoadImage(hInst, pstrFileName, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
return hBitmap != NULL; }
BOOL PopFileWrite (HWND hwnd, PSTR pstrFileName) {
FILE *file; int iLength; PSTR pstrBuffer;
if (NULL == (file = fopen (pstrFileName, "wb"))) return FALSE;
iLength = GetWindowTextLength (hwnd);
if (NULL == (pstrBuffer = (PSTR) malloc (iLength + 1))) { fclose (file); return FALSE; }
GetWindowText (hwnd, pstrBuffer, iLength + 1);
if (iLength != (int) fwrite (pstrBuffer, 1, iLength, file)) { fclose (file); free (pstrBuffer); return FALSE; }
fclose (file); free (pstrBuffer); return TRUE; }
// ************************** funciones auxiliares ****************
void DoCaption (HWND hwnd, char *szTitleName) { char szCaption[64 + _MAX_FNAME + _MAX_EXT];
wsprintf (szCaption, "%s - %s", szAppName, szTitleName[0] ? szTitleName : UNTITLED);
SetWindowText (hwnd, szCaption); }
void OkMessage (HWND hwnd, char *szMessage, char *szTitleName) { char szBuffer[64 + _MAX_FNAME + _MAX_EXT];
wsprintf (szBuffer, szMessage, szTitleName[0] ? szTitleName : UNTITLED);
MessageBox (hwnd, szBuffer, szAppName, MB_OK | MB_ICONEXCLAMATION); }
short AskAboutSave (HWND hwnd, char *szTitleName) { char szBuffer[64 + _MAX_FNAME + _MAX_EXT]; int iReturn;
wsprintf (szBuffer, "¿Guardar cambios actuales en %s?", szTitleName[0] ? szTitleName : UNTITLED);
iReturn = MessageBox (hwnd, szBuffer, szAppName, MB_YESNOCANCEL | MB_ICONQUESTION);
if (iReturn == IDYES) if (!SendMessage (hwnd, WM_COMMAND, IDM_SAVE, 0L)) iReturn = IDCANCEL;
return iReturn; }
_________________________________________________________________
Express yourself instantly with MSN Messenger! Download today it's FREE!
http://messenger.msn.click-url.com/go/onm00200471ave/direct/01/
------------ próxima parte ------------
Se ha borrado un adjunto en formato HTML...
URL: <http://listas.conclase.net/pipermail/cconclase_listas.conclase.net/attachments/20071018/bfc01fe9/attachment.html>
Más información sobre la lista de distribución Cconclase