Code:
#include <windows.h>
#define BITMAPSIZE 120
LRESULT CALLBACK MyWndProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam);
DWORD* dwBitmapArray;
BITMAPINFO bmInfo;
HBITMAP theBitmapFromHDC;
HDC theCompatibleDC;
BYTE bRed = 0xff;
BYTE bGreen = 0x00;
BYTE bBlue = 0x00;
char szClassName[ ] = "BMPfromHDC";
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
WNDCLASS wc;
HWND hwnd;
MSG msg;
ZeroMemory(&wc, sizeof(WNDCLASS));
wc.hInstance = hInstance;
wc.lpfnWndProc = MyWndProc;
wc.lpszClassName = szClassName;
wc.hbrBackground = (HBRUSH) COLOR_BACKGROUND;
RegisterClass(&wc);
hwnd = CreateWindowEx (
0,
szClassName,
"BMP from HDC Application",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
450,
450,
HWND_DESKTOP,
NULL,
hInstance,
NULL
);
ShowWindow (hwnd, nShowCmd);
while(GetMessage(&msg, hwnd, 0,0))
DispatchMessage(&msg);
return 0;
}
LRESULT CALLBACK MyWndProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
{
PAINTSTRUCT ps;
int iIndex;
switch(msg)
{
case WM_CREATE:
dwBitmapArray = (DWORD*) malloc(BITMAPSIZE*BITMAPSIZE*sizeof(DWORD));
for(iIndex = 0; iIndex < BITMAPSIZE * BITMAPSIZE; iIndex++)
dwBitmapArray[iIndex] = (bRed << 16) | (bGreen << 8) | (bBlue);
ZeroMemory(&bmInfo, sizeof(BITMAPINFO));
bmInfo.bmiHeader.biBitCount = 32;
bmInfo.bmiHeader.biHeight = BITMAPSIZE;
bmInfo.bmiHeader.biPlanes = 1;
bmInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bmInfo.bmiHeader.biWidth = BITMAPSIZE;
bmInfo.bmiHeader.biCompression = BI_RGB;
theBitmapFromHDC = CreateCompatibleBitmap(GetDC(hwnd), BITMAPSIZE, BITMAPSIZE);
theCompatibleDC = CreateCompatibleDC(GetDC(hwnd));
SelectObject(theCompatibleDC, theBitmapFromHDC);
SetDIBits(theCompatibleDC, theBitmapFromHDC, 0, BITMAPSIZE, dwBitmapArray, &bmInfo, 0);
break;
case WM_PAINT:
BeginPaint(hwnd, &ps);
BitBlt(ps.hdc, 0, 0, BITMAPSIZE, BITMAPSIZE, theCompatibleDC, 0, 0, SRCCOPY);
EndPaint(hwnd, &ps);
break;
case WM_DESTROY:
PostQuitMessage(0);
exit(0);
break;
default:
return DefWindowProc(hwnd, msg, wparam, lparam);
}
return lparam;
}