This node is in response to a question wharfinger asked me in the chatterbox. Here is the solution to his question. It is being left here as a general tutorial on how to do this, if anyone else is ever interested. If no one cares after wharfinger gets to it, then I'll take it down.
Instructions: Put in a blank
VS 6.0
project and hit
build. End of story.
/* For Wharfinger, this should solve your problem hopefully
What this program does is create a fully functional 30 x 30 bitmap onto a black brushed window.
There are no menus and no window commands, although it will shutdown properly.
You cannot put RGBQUADs into a bitmap, that is how you describe a palette, I think.
If you already have a program , you can use the function below to translate them into
an appropirate 32-bit DWORD.
You must::
CreateCompatibleBitmap
CreateCompatibleDC
SelectObject
BitBlt (or your other transfer mechanism of your choice).
The asserts are in there to see if anything fails due to memory, and to help me try to track
down a really dumb bug I had.
Any questions? email me jay@manifestresearch.com
By default this program creates a red bitmap, although you can change the values by playing
with the default in the items below. The procedure applies to any well-behaved array or
DWORDS containing bitmap data.
*/
#include <windows.h>
#include <assert.h>
LRESULT CALLBACK MyWndProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam);
DWORD* dwArray;
BITMAPINFO bmInfo;
HBITMAP myBitmap;
HDC myCompatibleDC;
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 = "MYWNDCLASSNAME";
wc.hbrBackground = (HBRUSH) BLACK_BRUSH + 1 ;
RegisterClass(&wc);
hwnd = CreateWindow("MYWNDCLASSNAME", "This is just a test for wharfinger",
WS_SYSMENU | WS_MINIMIZEBOX | WS_VISIBLE,
CW_USEDEFAULT, CW_USEDEFAULT, 300, 300, NULL, NULL, hInstance, NULL);
while(GetMessage(&msg, hwnd, 0,0))
{
DispatchMessage(&msg);
}
return 0;
}
DWORD MakeDwordColor(int r, int g, int b)
{
DWORD dwStuff = ((r << 16) | (g << 8) | (b));
return dwStuff;
}
LRESULT CALLBACK MyWndProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
{
PAINTSTRUCT ps;
int i;
int nResult;
switch(msg)
{
case WM_CREATE:
dwArray = (DWORD*) malloc(30*30*sizeof(DWORD));
assert(dwArray);
for(i=0; i<30*30; i++)
dwArray[i] = MakeDwordColor(0xff, 0x00, 0x00);
//Change this color here to change the bmp color
ZeroMemory(&bmInfo, sizeof(BITMAPINFO));
bmInfo.bmiHeader.biBitCount = 32;
bmInfo.bmiHeader.biHeight = 30;
bmInfo.bmiHeader.biPlanes = 1;
bmInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bmInfo.bmiHeader.biWidth = 30;
bmInfo.bmiHeader.biCompression = BI_RGB;
myBitmap = CreateCompatibleBitmap(GetDC(hwnd), 30, 30);
assert(myBitmap);
myCompatibleDC = CreateCompatibleDC(GetDC(hwnd));
assert(myCompatibleDC);
SelectObject(myCompatibleDC, myBitmap);
nResult = SetDIBits(myCompatibleDC, myBitmap, 0, 30, dwArray, &bmInfo, 0);
assert(nResult != 0);
break;
case WM_PAINT:
BeginPaint(hwnd, &ps);
nResult = BitBlt(ps.hdc, 0, 0, 30, 30, myCompatibleDC, 0, 0, SRCCOPY);
assert(nResult != NULL);
EndPaint(hwnd, &ps);
break;
case WM_DESTROY:
PostQuitMessage(0);
exit(0);
break;
default:
return DefWindowProc(hwnd, msg, wparam, lparam);
}
return lparam;
}