Code:
// epfTakeScreenShot.cpp
#include <windows.h>
#include "engine.h"
HBITMAP epfTakeScreenShot(HWND hwnd, int xPos, int yPos, int cx, int cy)
{
HDC hdc, hdcMem;
HBITMAP hBitmap;
hdc = GetDC(hwnd);
hdcMem = CreateCompatibleDC(hdc);
hBitmap = CreateCompatibleBitmap(hdc, cx, cy);
SelectObject(hdcMem, hBitmap);
BitBlt(hdcMem, 0, 0, cx, cy, hdc, xPos, yPos, SRCCOPY);
DeleteDC(hdcMem);
ReleaseDC(hwnd, hdc);
return hBitmap;
}
Code:
// epfConvertDDBtoDIB.cpp
#include <windows.h>
#include "engine.h"
// Function epfConvertDDBtoDIB takes as "hBitmap" parameter only 24bpp bitmaps
BITMAPINFO * epfConvertDDBtoDIB(HWND hwnd, HBITMAP hBitmap)
{
if(hBitmap == NULL)
{
MessageBox(hwnd, TEXT("Brak bitmapy DDB do konwersji"), NULL, MB_OK | MB_ICONERROR);
return NULL;
}
HDC hdc;
BITMAP bm;
BITMAPINFO * pBmi;
BYTE * pBits;
WORD wRowLength;
// Initialize the BITMAPINFOHEADER
GetObject(hBitmap, sizeof(BITMAP), &bm);
wRowLength = ((bm.bmWidth * 24 + 31) & ~31) >> 3;
pBmi = (BITMAPINFO *) malloc(sizeof(BITMAPINFO) + wRowLength * bm.bmHeight);
pBmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
pBmi->bmiHeader.biWidth = bm.bmWidth;
pBmi->bmiHeader.biHeight = bm.bmHeight;
pBmi->bmiHeader.biPlanes = 1;
pBmi->bmiHeader.biBitCount = 24;
pBmi->bmiHeader.biCompression = BI_RGB;
pBmi->bmiHeader.biSizeImage = 0;
pBmi->bmiHeader.biXPelsPerMeter = 0;
pBmi->bmiHeader.biYPelsPerMeter = 0;
pBmi->bmiHeader.biClrUsed = 0;
pBmi->bmiHeader.biClrImportant = 0;
// Take pointer to Bits of Pixels
pBits = (BYTE *) (pBmi + 1);
// Convert DDB to DIB
hdc = GetDC(hwnd);
GetDIBits(hdc, hBitmap, 0, bm.bmHeight, pBits, pBmi, DIB_RGB_COLORS);
ReleaseDC(hwnd, hdc);
free(pBmi);
return pBmi;
}
Code:
// epulsfoto.cpp
#include <windows.h>
#include "engine.h"
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
{
static TCHAR szAppName[] = TEXT("ePulsFoto");
HWND hwnd;
MSG msg;
WNDCLASS wndclass;
wndclass.style = CS_HREDRAW | CS_VREDRAW;
wndclass.lpfnWndProc = WndProc;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = hInstance;
wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
wndclass.hbrBackground = (HBRUSH) CreateSolidBrush(RGB(100, 200, 0));
wndclass.lpszMenuName = NULL;
wndclass.lpszClassName = szAppName;
if(!RegisterClass(&wndclass))
{
MessageBox(NULL, TEXT("Ten program wymaga Windows NT / XP!"), szAppName, MB_ICONERROR);
return 0;
}
hwnd = CreateWindow(szAppName,
TEXT("ePulsFoto [engine]"),
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
NULL,
NULL,
hInstance,
NULL);
ShowWindow(hwnd, iCmdShow);
UpdateWindow(hwnd);
while(GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
static HDC hdc, hdcMem;
static PAINTSTRUCT ps;
static HBITMAP hBitmap;
static BITMAPINFO * pBmi;
static BYTE * pBits;
switch(message)
{
case WM_CREATE:
return 0;
case WM_PAINT:
hdc = BeginPaint(hwnd, &ps);
TextOut(hdc, 0, 0, TEXT("dupa"), 4);
hBitmap = epfTakeScreenShot(hwnd, 0, 0, 100, 100);
pBmi = epfConvertDDBtoDIB(hwnd, hBitmap);
pBits = (BYTE *) (pBmi + 1);
SetDIBitsToDevice(hdc, 0, 0, 100, 100, 0, 0, 0, 100, pBits, pBmi, DIB_RGB_COLORS);
EndPaint(hwnd, &ps);
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd, message, wParam, lParam);
}
+ header files which I don't include.
Thanks X PaYnE X for code. Basing on your code I wrote function epfConvertDDBtoDIB, but it doesn't work :/. I think I'v done everyting correctly. Function epfTakeScreenShot works good... What is wrong in converting function... ?